<?php

/**
 * The file that defines the core plugin class
 *
 * A class definition that includes attributes and functions used across both the
 * public-facing side of the site and the admin area.
 *
 * @link       https://profile.fraxzon.com
 * @since      1.0.0
 *
 * @package    Ffl_Dealers
 * @subpackage Ffl_Dealers/includes
 */

/**
 * The core plugin class.
 *
 * This is used to define internationalization, admin-specific hooks, and
 * public-facing site hooks.
 *
 * Also maintains the unique identifier of this plugin as well as the current
 * version of the plugin.
 *
 * @since      1.0.0
 * @package    Ffl_Dealers
 * @subpackage Ffl_Dealers/includes
 * @author     shafiq <shafiq6171@gmail.com>
 */
class Ffl_Dealers {

	/**
	 * The loader that's responsible for maintaining and registering all hooks that power
	 * the plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      Ffl_Dealers_Loader    $loader    Maintains and registers all hooks for the plugin.
	 */
	protected $loader;

	/**
	 * The unique identifier of this plugin.
	 *
	 * @since    1.0.0
	 * @access   protected
	 * @var      string    $plugin_name    The string used to uniquely identify this plugin.
	 */
	protected $plugin_name;

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

	/**
	 * Define the core functionality of the plugin.
	 *
	 * Set the plugin name and the plugin version that can be used throughout the plugin.
	 * Load the dependencies, define the locale, and set the hooks for the admin area and
	 * the public-facing side of the site.
	 *
	 * @since    1.0.0
	 */
	public function __construct() {
		if ( defined( 'FFL_DEALERS_VERSION' ) ) {
			$this->version = FFL_DEALERS_VERSION;
		} else {
			$this->version = '1.0.0';
		}
		$this->plugin_name = 'ffl-dealers';

		$this->load_dependencies();
		$this->set_locale();
		$this->define_admin_hooks();
		$this->define_public_hooks();
		$this->define_rest_api_hooks();
	}

	/**
	 * Load the required dependencies for this plugin.
	 *
	 * Include the following files that make up the plugin:
	 *
	 * - Ffl_Dealers_Loader. Orchestrates the hooks of the plugin.
	 * - Ffl_Dealers_i18n. Defines internationalization functionality.
	 * - Ffl_Dealers_Admin. Defines all hooks for the admin area.
	 * - Ffl_Dealers_Public. Defines all hooks for the public side of the site.
	 *
	 * Create an instance of the loader which will be used to register the hooks
	 * with WordPress.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function load_dependencies() {

		/**
		 * The class responsible for orchestrating the actions and filters of the
		 * core plugin.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-ffl-dealers-loader.php';
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-ffl-dealers-validation.php';
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/functions.php';

		/**
		 * The class responsible for defining internationalization functionality
		 * of the plugin.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-ffl-dealers-i18n.php';
		
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'api/ffl-dealers-rest-api.php';
		/**
		 * The class responsible for defining all actions that occur in the admin area.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-ffl-dealers-admin.php';
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-ffl-dealers-lists.php';

		/**
		 * The class responsible for defining all actions that occur in the public-facing
		 * side of the site.
		 */
		require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-ffl-dealers-public.php';

		$this->loader = new Ffl_Dealers_Loader();

	}

	/**
	 * Define the locale for this plugin for internationalization.
	 *
	 * Uses the Ffl_Dealers_i18n class in order to set the domain and to register the hook
	 * with WordPress.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function set_locale() {

		$plugin_i18n = new Ffl_Dealers_i18n();

		$this->loader->add_action( 'plugins_loaded', $plugin_i18n, 'load_plugin_textdomain' );

	}

	/**
	 * Register all of the hooks related to the admin area functionality
	 * of the plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function define_admin_hooks() {

		$plugin_admin = new Ffl_Dealers_Admin( $this->get_plugin_name(), $this->get_version() );

		$this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' );
		$this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' );
		$this->loader->add_action('admin_init',  $plugin_admin, 'check_ffl_dealers_installation_time' );
		$this->loader->add_action('admin_menu', $plugin_admin, 'ffl_dealers_admin_menu' );
		$this->loader->add_action( 'admin_post_ffl_dealers', $plugin_admin, 'ffl_dealers_add_new' );
		$this->loader->add_action( 'admin_post_nopriv_ffl_dealers', $plugin_admin, 'ffl_dealers_add_new' );
		$this->loader->add_filter('ffl_menu_settings_tabs', $plugin_admin, 'ffl_admin_menu_settings_tabs' );
		$this->loader->add_filter('ffl_menu_settings_fields', $plugin_admin, 'ffl_admin_menu_settings_fields' );
		$this->loader->add_action('admin_init', $plugin_admin, 'save_ffl_admin_menu_settings_fields');
		$this->loader->add_action('ffl_dealers_settings_page_notice', $plugin_admin, 'plugin_using_policy_notice');
		$this->loader->add_action('admin_init', $plugin_admin, 'ffl_dealers_installation_required');
		$this->loader->add_action('save_settings_ffl_fields', $plugin_admin, 'add_map_starting_location');
		$this->loader->add_action('save_settings_ffl_fields', $plugin_admin, 'enable_shortcode_or_modal');


	}

	/**
	 * Register all of the hooks related to the public-facing functionality
	 * of the plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 */
	private function define_public_hooks() {

		$plugin_public = new Ffl_Dealers_Public( $this->get_plugin_name(), $this->get_version() );

		$this->loader->add_action('wp_enqueue_scripts', $plugin_public, 'enqueue_styles' );
		$this->loader->add_action('wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' );
		//$this->loader->add_action('init', $plugin_public, 'store_dealer_data_local_storage' );
		
		$this->loader->add_action('woocommerce_before_checkout_billing_form', $plugin_public, 'ffl_dealers_checkout',99);
		$this->loader->add_action('dealer_locators_position', $plugin_public, 'checkout_position_dealer_list', 10);
		
	}
	
	private function define_rest_api_hooks() {
		$this->loader->add_action( 'rest_api_init',$this,'register_api');

	}
	public function register_api(){
		$dealers = new FFL_Rest_API();
		$dealers->register_routes();
	}
	/**
	 * Run the loader to execute all of the hooks with WordPress.
	 *
	 * @since    1.0.0
	 */
	public function run() {
		$this->loader->run();
	}

	/**
	 * The name of the plugin used to uniquely identify it within the context of
	 * WordPress and to define internationalization functionality.
	 *
	 * @since     1.0.0
	 * @return    string    The name of the plugin.
	 */
	public function get_plugin_name() {
		return $this->plugin_name;
	}

	/**
	 * The reference to the class that orchestrates the hooks with the plugin.
	 *
	 * @since     1.0.0
	 * @return    Ffl_Dealers_Loader    Orchestrates the hooks of the plugin.
	 */
	public function get_loader() {
		return $this->loader;
	}

	/**
	 * Retrieve the version number of the plugin.
	 *
	 * @since     1.0.0
	 * @return    string    The version number of the plugin.
	 */
	public function get_version() {
		return $this->version;
	}
	/**
	 * Show admin notices.
	 *
	 * @param  string $swa_message    Message to display.
	 * @param  string $type       notice type, accepted values - error/update/update-nag.
	 * @since  1.0.0
	 */
	public static function plugin_admin_notice( $tmr_message, $type = 'error' ) {

		$tmr_classes = 'notice ';

		switch ( $type ) {
			
			case 'update':
				$tmr_classes .= 'updated is-dismissible';
				break;

			case 'update-nag':
				$tmr_classes .= 'update-nag is-dismissible';
				break;

			case 'success':
				$tmr_classes .= 'notice-success is-dismissible';
				break;

			default:
				$tmr_classes .= 'notice-error is-dismissible';
		}

		$tmr_notice  = '<div class="' . esc_attr( $tmr_classes ) . ' wps-errorr-8">';
		$tmr_notice .= '<p>' . esc_html( $tmr_message ) . '</p>';
		$tmr_notice .= '</div>';

		echo wp_kses_post( $tmr_notice );
	}
	public function load_plugin_template( $content_path ) {
		if ( file_exists( $content_path ) ) {
			include $content_path;
		} else {
			/* translators: %s: file path */
			$tmr_notice = sprintf( esc_html__( 'Unable to locate file at location "%s". Some features may not work properly in this plugin. Please contact us!', 'ffl-dealers' ), $content_path );
			$this->plugin_admin_notice( $tmr_notice, 'error' );
		}
	}

}
