<?php
namespace Wpcafe_Multivendor\Core\Enqueue;

defined( 'ABSPATH' ) || exit;

use WpCafe\Traits\Wpc_Singleton;
use Wpcafe_Multivendor;

/**
 * Enqueue all css and js file class
 */
class Enqueue {

	use Wpc_Singleton;

	/**
	 * Main calling function.
	 */
	public function init() {
		// backend asset.
		add_action( 'admin_enqueue_scripts', [$this, 'admin_enqueue_assets'] );
		// frontend asset.
		add_action( 'wp_enqueue_scripts', [$this, 'frontend_enqueue_assets'] );
		// Add date/time format strings to frontend localize data on vendor dashboard.
		add_filter( 'wpcafe_frontend_localize', [$this, 'add_vendor_dashboard_localize_data'] );
	}


	/**
	 * All js files function.
	 */
	public function admin_get_scripts() {
		$script_arr =  array(
			'wpcafe-multivendor'     => array(
				'src'     => \Wpcafe_Multivendor::assets_url() . 'js/admin.js',
				'version' => \Wpcafe_Multivendor::version(),
				'deps'    => ['jquery'],
			)
		);

		return $script_arr;
	}

	/**
	 * All css files function.
	 */
	public function admin_get_styles() {
		return array(
			'wpcafe-multivendor' => array(
				'src'     => \Wpcafe_Multivendor::assets_url() . 'css/admin.css',
				'version' => \Wpcafe_Multivendor::version(),
			)
		);
	}

	/**
	 * All js files function.
	 */
	public function frontend_get_scripts() {

		$script_arr = array(
			'wpcafe-multivendor-public'  => array(
				'src'     => \Wpcafe_Multivendor::assets_url() . 'js/public.js',
				'version' => \Wpcafe_Multivendor::version(),
				'deps'    => ['jquery','select2'],
			),
			// Multivendor dashboard (depends on cafe/pro dashboards where available).
			'wpcafe-multivendor-dashboard' => array(
				'src'     => \Wpcafe_Multivendor::assets_url() . 'build/js/dashboard.js',
				'version' => \Wpcafe_Multivendor::version(),
				// Base deps; additional deps appended below based on active plugins.
				'deps'    => ['jquery', 'wpcafe-dashboard-scripts', 'wpc-public'],
			),
			'wpcafe-multivendor-location-opener' => array(
				'src'     => \Wpcafe_Multivendor::assets_url() . 'js/location-selector-opener.js',
				'version' => \Wpcafe_Multivendor::version(),
				'deps'    => ['jquery', 'wpc-location-selector'],
			),
		);

		// Load Cafe (free) dashboard script when available and add as dependency.
		
			$script_arr['wpcafe-dashboard-scripts'] = [
				'src'       => wpcafe()->assets_url . '/build/js/dashboard.js',
				'deps'      => [
					'wp-api-fetch',
					'wp-components',
					'wp-compose',
					'wp-data',
					'wp-element',
					'wp-i18n'
				],
				'in_footer' => true,
			];
		

		// If Pro is active, register its dashboard script and add as dependency.
		if ( defined( 'WPCAFE_PRO_FILE' ) ) {
			$script_arr['wpcafe-pro-dashboard-scripts'] = [
				'src'       => wpcafe_pro()->assets_url . '/build/js/dashboard.js',
				'deps'      => [
					'wpcafe-dashboard-scripts',
					'wp-api-fetch',
					'wp-components',
					'wp-compose',
					'wp-data',
					'wp-element',
					'wp-i18n'
				],
				'in_footer' => true,
			];

			// Ensure multivendor dashboard waits for pro dashboard too.
			$script_arr['wpcafe-multivendor-dashboard']['deps'][] = 'wpcafe-pro-dashboard-scripts';
		}

		if(file_exists(\Wpcafe_Multivendor::core_dir() . "modules/product-addons/assets/js/admin.js")){
			$script_arr['wpcafe-multivendor-addons'] = array(
					'src'     => \Wpcafe_Multivendor::core_url() . 'modules/product-addons/assets/js/admin.js',
					'version' => \Wpcafe_Multivendor::version(),
					'deps'    => ['jquery'],
			);
		}

		return $script_arr;

	}

	/**
	 * All css files function.
	 */
	public function frontend_get_styles() {
			$deps = [];

			$enqueue =  array(
				// Multivendor public styles.
				'wpcafe-multivendor-public' => array(
					'src'     => \Wpcafe_Multivendor::assets_url() . 'css/public.css',
					'version' => \Wpcafe_Multivendor::version(),
				),
			);

			// Cafe (free) admin styles to be loaded first if available.
			if ( function_exists( 'wpcafe' ) ) {
				$enqueue['wpcafe-admin-styles'] = array(
					'src'     => wpcafe()->assets_url . '/build/css/admin.css',
					'version' => \Wpcafe_Multivendor::version(),
				);
				$deps[] = 'wpcafe-admin-styles';
			}

			// If Pro is active, register its admin styles and set dependencies.
			if ( defined( 'WPCAFE_PRO_FILE' ) ) {
				$enqueue['wpcafe-pro-admin-styles'] = array(
					'src'     => plugin_dir_url( WPCAFE_PRO_FILE ) . 'assets/build/css/admin.css',
					'version' => \Wpcafe_Multivendor::version(),
				);
				$deps[] = 'wpcafe-pro-admin-styles';
			}

			// Build dependency list based on what we registered above.

			// Multivendor dashboard styles; depend on cafe/pro admin styles to ensure order.
			$enqueue['wpcafe-multivendor-dashboard'] = array(
				'src'     => \Wpcafe_Multivendor::assets_url() . 'build/css/admin.css',
				'version' => \Wpcafe_Multivendor::version(),
				'deps'    => $deps,
			);

			return $enqueue;
	}

	/**
	 * Enqueue admin js and css function
	 *
	 * @param  $var
	 */
	public function admin_enqueue_assets() {
			wp_enqueue_style( 'wpcafe-multivendor', \Wpcafe_Multivendor::assets_url() . 'css/admin.css', [], \Wpcafe_Multivendor::version(), 'all' );

			// js
			$scripts = $this->admin_get_scripts();

			foreach ( $scripts as $key => $value ) {
				$deps       = !empty( $value['deps'] ) ? $value['deps'] : false;
				$version    = !empty( $value['version'] ) ? $value['version'] : false;
				wp_enqueue_script( $key, $value['src'], $deps, $version, true );
			}

			// css
			$styles = $this->admin_get_styles();

			foreach ( $styles as $key => $value ) {
					$deps       = isset( $value['deps'] ) ? $value['deps'] : false;
					$version    = !empty( $value['version'] ) ? $value['version'] : false;
					wp_enqueue_style( $key, $value['src'], $deps, $version, 'all' );
			}
	}

	/**
	 * Enqueue admin js and css function.
	 */
	public function frontend_enqueue_assets() {

		if ( dokan_is_seller_dashboard() ) {
			// css.
			$styles = $this->frontend_get_styles();

			foreach ( $styles as $key => $value ) {
					$deps = isset( $value['deps'] ) ? $value['deps'] : false;
					$version    = !empty( $value['version'] ) ? $value['version'] : false;
					wp_enqueue_style( $key, $value['src'], $deps, $version, 'all' );
			}

			// js.
			$scripts = $this->frontend_get_scripts();

			foreach ( $scripts as $key => $value ) {
					$deps       = isset( $value['deps'] ) ? $value['deps'] : false;
					$version    = !empty( $value['version'] ) ? $value['version'] : false;

					wp_enqueue_script( $key, $value['src'], $deps, $version, true );
			}
			$translation_data = array(
					'ajax_url'              => admin_url( 'admin-ajax.php' ),
					'add_pao_nonce'         => wp_create_nonce( 'add_pao_nonce_value' ),
					'remove_pao_nonce'      => wp_create_nonce( 'remove_pao_nonce_value' ),
					'add_option_nonce'      => wp_create_nonce( 'add_option_nonce_value' ),
					'remove_option_nonce'   => wp_create_nonce( 'remove_option_nonce_value' ),
					'addons'                => array( 'option_name'=> esc_html__('Please fill the option field','wpcafe-multivendor') ,
					'pao_title'=> esc_html__('Please fill the title field','wpcafe-multivendor') ),
					'repeater_text'         => array('opt_name'=> esc_html__('Option name','wpcafe-multivendor'), 'def_select' => esc_html__('Default selected','wpcafe-multivendor'),
					'qty_based' => esc_html__('Quantity Based','wpcafe-multivendor'))
			);

			//Added active class in the wpcafe menu in dokan leftmenu.

			global $wp;
			$active_menu = '';
			if ( isset( $wp->query_vars['wpcafe'] ) ) {
				$active_menu = $wp->query_vars['wpcafe'];
			}
			wp_add_inline_script( 'wpcafe-multivendor-public', 'var wpcafe_active_menu = ' . wp_json_encode( $active_menu ), 'before' );
			wp_localize_script( 'wpcafe-multivendor-addons', 'wpc_pro_pao_obj', $translation_data );

		}
	}

	/**
	 * Add date and time format strings to frontend localize data for dokan vendor dashboard
	 *
	 * @param array $data Localize data array
	 * @return array Modified localize data
	 */
	public function add_vendor_dashboard_localize_data( $data ) {
		if ( function_exists( 'dokan_is_seller_dashboard' ) && dokan_is_seller_dashboard() ) {
			$data['date_format_string'] = date_i18n( get_option( 'date_format' ) );
			$data['time_format_string'] = date_i18n( get_option( 'time_format' ) );
		}

		return $data;
	}

}