<?php

/**
 * This class manage all base functionality.
 *
 * @since 1.0 
 * 
 * @package SiteEase_Smart_Product_Filtering_Engine
 * @subpackage SiteEase_Smart_Product_Filtering_Engine/includes 
 */
// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class IFQPF_SiteEase_Smart_Product_Filtering_Engine {

	/**
	 * The instance of query class
	 * 
	 * @since 1.0
	 * 
	 * @var instance 	$query 	instance of query class
	 */
	public $query;

	/**
	 * The instance of model class
	 * 
	 * @since 1.0
	 * 
	 * @var instance 	$model 	instance of model class
	 */
	public $model;

	/**
	 * The instance of public class
	 * 
	 * @since 1.0
	 * 
	 * @var instance 	$public 	instance of public class
	 */
	public $public;

	/**
	 * The instance of admin class
	 * 
	 * @since 1.0
	 * 
	 * @var instance 	$admin 	instance of admin class
	 */
	public $admin;

	/**
	 * The instance of widgets class
	 * 
	 * @since 1.0
	 * 
	 * @var instance 	$widgets 	instance of widgets class
	 */
	public $widgets;

	/**
	 * The instance of main class
	 * 
	 * @since 1.0
	 * 
	 * @var instance 	$instance 	instance of main class
	 */
	private static  $instance;

	/**
	 * This return instance of main class
	 * 
	 * @since 1.0
	 * 
	 * @return instance of iFlair_Woo_Product_Filters
	 */
	public static function instance(){
        
        if ( !isset( self::$instance ) ) {
            self::$instance = new IFQPF_SiteEase_Smart_Product_Filtering_Engine(); 
        }
        
        return self::$instance;
    }

    /**
     * This code will runs whole plugin
     *
     * @since 1.0
     */
    public function __construct(){

    }

    /**
	 * Runs all code
	 *
	 * @since 1.0
	 */
	public function run(){
		$this->ifqpf_load_dependencies();
		$this->ifqpf_define_hooks();
	}

	/**
	 * Loades all files which are used by this plugin
	 * 
	 * @since 1.0
	 */
	private function ifqpf_load_dependencies(){
		
		global $ifqpf_settings,$ifqpf_default_settings,$ifqpf_query,$ifqpf_model,$ifqpf_admin,$ifqpf_public;

		/**
		 * This file is responsible for defining all core functions. 
		 */
		require_once( IFQPF_INC_DIR_PATH.'ifqpf-functions.php' );

		$ifqpf_settings = ifqpf_get_option();
		$ifqpf_default_settings = ifqpf_default_settings();


		/**
		 * This class is responsible for defining all methods filter query
		 */
		require_once( IFQPF_INC_DIR_PATH.'ifqpf-class-query.php' );
		$ifqpf_query = new IFQPF_SiteEase_Smart_Product_Filtering_Engine_Query();
		$this->query = $ifqpf_query;

		/**
		 * This class is responsible for defining all methods for logic
		 */
		require_once( IFQPF_INC_DIR_PATH.'ifqpf-class-model.php' );
		$ifqpf_model = new IFQPF_SiteEase_Smart_Product_Filtering_Engine_Model();
		$this->model = $ifqpf_model;

		/**
		 * This class is responsible for defining all functionality of admin
		 */
		
		if( is_admin() ){
			require_once( IFQPF_ADMIN_DIR_PATH.'ifqpf-class-admin.php' );
			$ifqpf_admin = new IFQPF_SiteEase_Smart_Product_Filtering_Engine_Admin();
			$this->admin = $ifqpf_admin;
		}

		/**
		 * This class is responsible for defining all functionality of front
		 */
		require_once( IFQPF_PUBLIC_DIR_PATH.'ifqpf-class-public.php' );
		$ifqpf_public = new IFQPF_SiteEase_Smart_Product_Filtering_Engine_Public();
		$this->public = $ifqpf_public;

		/**
		 * This class is responsible for defining all functionality of Widgets
		 */
		require_once( IFQPF_PUBLIC_DIR_PATH.'ifqpf-class-widgets.php' );
		$ifqpf_widgets = new IFQPF_SiteEase_Smart_Product_Filtering_Engine_Widgets();
		$this->widgets = $ifqpf_widgets;

	}

	/**
	 * Deactivate this plugin if woocommerce is not activate.
	 *
	 * @since 1.0
	 */
	public function ifqpf_deactivate() {

		if ( !function_exists('deactivate_plugins') ) {
			require_once(ABSPATH . 'wp-admin/includes/plugin.php');
		}

		add_action( 'admin_notices', array( $this, 'ifqpf_quick_product_check_notice' ) );

		if ( isset( $_GET['activate'], $_GET['_wpnonce'] ) ) {
		    $nonce = sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) );
		    if ( wp_verify_nonce( $nonce, 'ifqpf_toggle_plugin' ) ) {
		        unset( $_GET['activate'] );
		        $_GET['deactivate'] = true;
		        deactivate_plugins( IFQPF_FILE, true );
		    }
		}

	}

	/**
	 * Display notice if woocommerce is not activate.
	 *
	 * @since 1.0
	 */
	public function ifqpf_quick_product_check_notice() {

		$woourl = 'https://wordpress.org/plugins/woocommerce/';
			
		$plugin_name = '<strong>'.IFQPF_PLUGIN_NAME.'</strong>';

		?>
		<div class="error notice is-dismissible"> 
			<p>
			<?php
			printf(
			// translators: 1: opening <a> tag, 2: closing </a> tag
			    wp_kses_post( $plugin_name ) . ' ' . esc_html__(
			        'plugin requires %1$sWooCommerce%2$s plugin in order to work. So please ensure that WooCommerce is installed and activated.',
			        'siteease-smart-product-filtering-engine'
			    ),
			    '<a target="_blank" href="' . esc_url( $woourl ) . '">',
			    '</a>'
			);
			?>
			</p>
		</div>
	    <?php
	}

	/**
	 * Show action links on the plugin screen.
	 *
	 * @since 1.0
	 * 
	 * @param mixed $links Plugin Action links.
	 * @return array
	 */
	public function ifqpf_plugin_action_links( $links ) {

		$action_links = array(
			'settings' => '<a href="' . admin_url( 'admin.php?page=ifqpf-settings' ) . '" aria-label="' . sprintf(
			// translators: %s: Plugin view setting 
			esc_attr( esc_html__('View %s settings', 'siteease-smart-product-filtering-engine' ) ), IFQPF_PLUGIN_NAME ) . '">' . esc_html__( 'Settings', 'siteease-smart-product-filtering-engine' ) . '</a>',
		);

		return array_merge( $action_links, $links );
	}

	/**
	 * Define all hooks of plugins
	 *
	 * @since 1.0
	 */
	private function ifqpf_define_hooks(){
		add_filter( 'plugin_action_links_' . plugin_basename( IFQPF_FILE ), array( $this, 'ifqpf_plugin_action_links' ) );

		// Safely register admin hooks only when the admin instance is available.
		if ( is_admin() && isset( $this->admin ) && is_object( $this->admin ) && method_exists( $this->admin, 'ifqpf_add_hooks' ) ) {
			$this->admin->ifqpf_add_hooks();
		}

		// Public and widgets instances should exist, but guard them as well to avoid fatals.
		if ( isset( $this->public ) && is_object( $this->public ) && method_exists( $this->public, 'ifqpf_add_hooks' ) ) {
			$this->public->ifqpf_add_hooks();
		}

		if ( isset( $this->widgets ) && is_object( $this->widgets ) && method_exists( $this->widgets, 'ifqpf_add_hooks' ) ) {
			$this->widgets->ifqpf_add_hooks();
		}
	}
}