<?php

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * If Dokan is active, hook module functions in
 */
if ( function_exists( 'dokan' ) ) {
	add_action( 'wp_enqueue_scripts', 'shrk_dokan_scripts' );
	add_filter( 'dokan_settings_fields', 'shrk_dokan_filter_settings' );
	add_action( 'wp', 'shrk_dokan_template_mods' );
	add_filter( 'dokan_locate_template', 'shrk_dokan_theme_store_template', 10, 3 );
	add_filter( 'dokan_get_template_part', 'shrk_dokan_theme_template_overrides', 10, 3 );
	add_action( 'dokan_register_scripts', 'shrk_dokan_remove_scripts_styles' );
}

/**
 * Enqueue module scripts and styles
 */





function shrk_dokan_scripts() {
	global $shrk_theme_info;
	$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';

	wp_enqueue_style( 'shrk-dokan', SHRK_THEME_MODULES_URI . 'dokan/css/dokan' . $suffix . '.css' , array( 'shrk-style' ), $shrk_theme_info['Version']  );
}

/**
 * Remove Dokan's select2 style overrides
 */
function shrk_dokan_remove_scripts_styles() {
	wp_deregister_style( 'dokan-select2-css' );
}

/**
 * Add theme's store header template to Dokan's settings
 * 
 * @param  array $settings_fields the settings array
 * @return array                  the filtered settings array
 */
function shrk_dokan_filter_settings( $settings_fields ) {
	if ( $settings_fields['dokan_appearance']['store_header_template']['options'] ) {
		$settings_fields['dokan_appearance']['store_header_template']['options']['layout-theme'] = SHRK_THEME_MODULES_URI . 'dokan/img/store-header-layout-theme.jpg';
	}

	return $settings_fields;
}


/**
 * Hook into theme and WC functions to create the 
 * vendor store header in the theme's page cover area
 * 
 * @return none
 */
function shrk_dokan_template_mods() {
	if ( dokan_is_store_page() && dokan_get_option( 'enable_theme_store_sidebar', 'dokan_general', 'off' ) == 'off' ) {
		add_filter( 'shrk_show_sidebar', '__return_false' );
	}

	$profile_layout = shrk_dokan_get_profile_layout();

	if ( dokan_is_store_page() && $profile_layout !== 'layout-theme' ) {
		add_filter( 'shrk_woocommerce_page_cover', '__return_false' );
	}

	if ( dokan_is_store_page() && $profile_layout === 'layout-theme' ) {
		add_action( 'shrk-page-cover-header-after', 'shrk_dokan_get_header_profile', 10 ); 

		// Remove shop page menus
		remove_action( 'shrk-page-cover-header-after', 'woo_shoppage_afterheader_content' );

		add_filter( 'shrk-cover-image', 'shrk_dokan_cover_image', 20 );
		add_filter( 'shrk-cover-image-id', 'shrk_dokan_cover_image_id', 20 );
	}
}


/**
 * Gets the template for Dokan's store header
 * for use in theme's page cover
 * 
 * @return none
 */
function shrk_dokan_get_header_profile() {
	
	// Don't get the empty-store-header.php
	remove_filter( 'dokan_get_template_part', 'shrk_dokan_theme_template_overrides', 10 );

	dokan_get_template_part( 'store-header' );	

	// Re add filter
	add_filter( 'dokan_get_template_part', 'shrk_dokan_theme_template_overrides', 10, 3 );

}

/**
 * Override Dokan's store template
 * 
 * @param  string $template
 * @param  string $template_name
 * @param  string $template_path
 * @return string
 */
function shrk_dokan_theme_store_template( $template, $template_name, $template_path ) {
	$overriden_templates = array( 'store.php', 'store-toc.php', 'store-reviews.php' );

	if ( in_array( $template_name, $overriden_templates ) ) {
		$template = SHRK_THEME_MODULES . '/dokan/templates/' . $template_name;
	}

	return $template;
}

/**
 * Override Dokan's store header template to remove header
 * from default location
 * 
 * @param  string $template
 * @param  string $slug    
 * @param  string $name    
 * @return string          
 */
function shrk_dokan_theme_template_overrides( $template, $slug, $name ) {
	$profile_layout = shrk_dokan_get_profile_layout();

	if ( $slug == 'store-header' && $profile_layout === 'layout-theme' ) {
		$template = SHRK_THEME_MODULES . '/dokan/templates/empty-store-header.php';
	}

	return $template;
}

/**
 * Helper function to return the active profile_layout
 * 
 * @return string profile_layout's slug
 */
function shrk_dokan_get_profile_layout() {
	$dokan_appearance = get_option( 'dokan_appearance' );
	$profile_layout   = empty( $dokan_appearance['store_header_template'] ) ? 'default' : $dokan_appearance['store_header_template'];
	return $profile_layout;
}

/**
 * Get Vendor's cover image
 * @return string the cover image's url
 */
function shrk_dokan_cover_image( $cover ) {
	if ( function_exists( 'dokan_is_store_page' ) && dokan_is_store_page() ) {
		$cover = false;

		$image_id = shrk_dokan_get_banner_img_id();
		$cover    = $image_id ? wp_get_attachment_image_src( $image_id, 'full' ) : false;
		$cover    = $cover ? $cover[0] : false;
	}

	return $cover;
}

/**
 * Get Vendor's cover image id
 * @return integer the cover image's id
 */
function shrk_dokan_cover_image_id( $cover_id ) {
	if ( function_exists( 'dokan_is_store_page' ) && dokan_is_store_page() ) {
		$cover_id = shrk_dokan_get_banner_img_id();
	}

	return $cover_id;
}

/**
 * Get Dokan's Vendor's banner image
 * 
 * @return int banner image id
 */
function shrk_dokan_get_banner_img_id() {
	$store_user    = dokan()->vendor->get( get_query_var( 'author' ) );
	$store_banner_id = (int) $store_user->get_info_part( 'banner' );

	if ( ! $store_banner_id ) {
		return false;
	} else {
		return $store_banner_id;
	}
}
