<?php

/**
 * The public-facing functionality of the plugin.
 *
 * @link       https://profile.fraxzon.com
 * @since      1.0.0
 *
 * @package    Ffl_Dealers
 * @subpackage Ffl_Dealers/public
 */

/**
 * The public-facing functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the public-facing stylesheet and JavaScript.
 *
 * @package    Ffl_Dealers
 * @subpackage Ffl_Dealers/public
 * @author     shafiq <shafiq6171@gmail.com>
 */
class Ffl_Dealers_Public {

	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;

	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string    $version    The current version of this plugin.
	 */
	private $version;

	/**
	 * Initialize the class and set its properties.
	 *
	 * @since    1.0.0
	 * @param      string    $plugin_name       The name of the plugin.
	 * @param      string    $version    The version of this plugin.
	 */
	public function __construct( $plugin_name, $version ) {

		$this->plugin_name = $plugin_name;
		$this->version = $version;

	}

	/**
	 * Register the stylesheets for the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_styles() {

		/**
		 * This function is provided for demonstration purposes only.
		 *
		 * An instance of this class should be passed to the run() function
		 * defined in Ffl_Dealers_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The Ffl_Dealers_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */

		wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/ffl-dealers-public.css', array(), $this->version, 'all' );
		wp_add_inline_style( $this->plugin_name, ffl_dealer_locator_style());
	}

	/**
	 * Register the JavaScript for the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_scripts() {
		/**
		* This function is provided for demonstration purposes only.
		*
		* An instance of this class should be passed to the run() function
		* defined in Ffl_Dealers_Loader as all of the hooks are defined
		* in that particular class.
		*
		* The Ffl_Dealers_Loader will then create the relationship
		* between the defined hooks and the functions defined in this
		* class.
		*/
		if (is_checkout()) { 
			wp_enqueue_script('notify', FFL_DEALERS_DIR_URL.'public/js/notify.min.js', array('jquery'), $this->version, true);
			
			if(ffl_dealer_widget_settings()['map_enable'] ==1){
				wp_enqueue_script('ffl-widget', FFL_DEALERS_DIR_URL.'public/js/ffl-dealers-widget.js', array('jquery'), $this->version, true);
			}else{
				wp_enqueue_script('ffl-dealers-lists-widgets', FFL_DEALERS_DIR_URL . 'public/js/ffl-dealers-lists-widgets.js', array( 'jquery'), $this->version, true );
				wp_localize_script('ffl-dealers-lists-widgets', 'FFLDELAERSCRIPT', ffl_dealer_widget_settings());
			}
			
			wp_enqueue_script( $this->plugin_name, FFL_DEALERS_DIR_URL . 'public/js/ffl-dealers-public.js', array( 'jquery'), $this->version, true );
			wp_localize_script($this->plugin_name, 'FFLDELAERSCRIPT', ffl_dealer_widget_settings());
			 
		}
	}
	/*public function store_dealer_data_local_storage(){
		$expiration = 3600;
		$data 		= all_dealer_zipcodes_array();
		$json_data 	= json_encode($data);
		$json_data 	= get_transient('dealer_zipcodes');
		if (!$json_data) {
			set_transient('dealer_zipcodes', $json_data, $expiration);
		}
	}*/
	public function ffl_dealers_checkout($checkout){
		global $woocommerce;
		$items = $woocommerce->cart->get_cart();
		
		if(!is_admin()){
			$google_api_key = esc_attr(get_option('google_map_api_key'));
			if ($google_api_key === '') {
				return false;
			}
		
			foreach ($items as $item => $values) {
				$product_id =  $values['data']->get_id();
				$product_terms = get_option('map_display_by_product_category');
				if(is_array($product_terms) && !empty($product_terms)){
					$categories = array_map( 'esc_attr', $product_terms );
					$terms = get_the_terms ($product_id, 'product_cat' );
					if(!empty($terms) && !empty($categories)){
						foreach ( $terms as $term ) {
							$cat_id = $term->term_id;
							if(is_array($categories) && in_array($cat_id,$categories)){
								$hook = get_option('ffl_search_map_location')?esc_attr(get_option('ffl_search_map_location')):'woocommerce_checkout_order_review';
								do_action('dealer_locators_position',strtolower($hook));
							}
						}
					}
				}
			}
		}
	}
	public function checkout_position_dealer_list($hook){
		$shortcode_enable 	= esc_attr(get_option( 'locator_shortcode_enable')) ? 1 : 0;
		
		$modal_enable 	= esc_attr(get_option( 'locator_modal_enable')) ? 1 : 0;
		if($shortcode_enable ===0){
			if($modal_enable || !$hook){
				add_action('wp_footer', array($this, 'modal_map_display'));
				add_action('woocommerce_before_checkout_shipping_form',[$this,'add_modal_button'], 10);
			}else{
				add_action($hook, array($this, 'dealers_location_position'));
			}
		}
		?>
		<script>
			(function($){
				$(document).ready(function(){
					$("#ship-to-different-address-checkbox").prop("disabled", true);
					$("#ship-to-different-address-checkbox" ).prop('checked', true);
				});
			})(jQuery)
		</script>
	<?php
	
	}
	public function dealers_location_position(){
		echo '<style>.modal-reopen{display: none}</style>';
		echo '<div class="map-container"><div id="ffl-dealer-container"></div></div>';
	}
	public function modal_map_display(){
		echo '<div id="ffl-dealer-modal" class="modalFFL" style="display:block"><div class="modalFFL-content"><span class="modal-close">×</span><div class="modal-content-box"><div id="ffl-dealer-container"></div></div></div></div>';
	}
	public function add_modal_button(){
		$button_text  = get_option( 'reopen_modal_button_text')?esc_attr(get_option( 'reopen_modal_button_text')):'Select FFL Dealer';
		echo '<div class="modal-reopen"><button type="button" id="modal-reopen">'.$button_text.'</button></div>';
	}

}
