<?php

namespace Bright_Deposits_For_WooCommerce;

// Container Manager
use Bright_Deposits_For_WooCommerce\Core\ContainerManager;

// Classes of the free plugin
use Bright_Deposits_For_WooCommerce\Setup\DepositSetup;
use Bright_Deposits_For_WooCommerce\Setup\Templates;
use Bright_Deposits_For_WooCommerce\Setup\AdminMenu;
use Bright_Deposits_For_WooCommerce\Helpers\Ajax;
use Bright_Deposits_For_WooCommerce\Emails\DepositEmail;
use Bright_Deposits_For_WooCommerce\Emails\ReminderManager;
use Bright_Deposits_For_WooCommerce\Setup\Settings;

/**
 * @package Bright_Deposits_For_WooCommerce
 * @since 1.0.0
 * @author Bright Plugins
 * Description: This class is responsible for bootstrapping the plugin and initializing its components.
 */
class Bootstrap {

	/**
	 * Singleton instance of the class.
	 * @var Bootstrap
	 */
	private static $instance = null;

	private function __construct() {

		//...
		if( !$this->attempt_loading_redux_framework() ) {
			return;
		}

		add_action( 'wp_enqueue_scripts', array( $this, 'bdfwbp_frontend_scripts' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'bdfwbp_admin_scripts' ) );

		new DepositSetup();
		new Templates();
		new AdminMenu();
		new Ajax();
		new DepositEmail();
		new ReminderManager();
		new Settings();

		/**
		 * Check if the PRO version of the plugin is installed.
		 * If it is, then initialize the PRO version.
		 * If not, then initialize the FREE version.
		 * This allows the plugin to work seamlessly with both versions.
		 */
		if ( class_exists( 'Bright_Deposits_For_WooCommerce_PRO\Premium\Bootstrap' ) ) {
			$this->try_to_join_pro_version();
			\Bright_Deposits_For_WooCommerce_PRO\Premium\Bootstrap::instance();
		}
		else {
			ContainerManager::register('helper', function () {
				return \Bright_Deposits_For_WooCommerce\Helpers\Helper::instance();
			});
	
			ContainerManager::register('exclusion_manager', function () {
				return \Bright_Deposits_For_WooCommerce\Core\ExclusionManager::instance();
			});
	
			\Bright_Deposits_For_WooCommerce\Pages\Product::instance();
			\Bright_Deposits_For_WooCommerce\Pages\Cart::instance();
			\Bright_Deposits_For_WooCommerce\Pages\Checkout::instance();
			\Bright_Deposits_For_WooCommerce\Pages\Order::instance();
		}
	}

	/**
	 * Get the singleton instance of the class.
	 * @since 1.0.0
	 * @return Bootstrap
	 */
	public static function instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Prevents cloning of the instance.
	 * @since 1.0.0
	 * @return void
	 */
	private function __clone() {
		// Cloning is forbidden.
	}

	/**
	 * Prevents unserializing of the instance.
	 * @since 1.0.0
	 * @return void
	 */
	public function __wakeup() {
		// Unserializing instances of this class is forbidden.
	}

	/**
	 * Admin js and css files
	 * @since 1.0.0
	 * @return void
	 */
	public function bdfwbp_admin_scripts() {
		wp_enqueue_script( 'bdfw-admin', BDFWBP_ASSETS . '/js/admin.js', ['jquery'], BDFWBP_VERSION, true );

		wp_enqueue_style(
			'bdfwbp-admin',
			BDFWBP_ASSETS . '/css/admin.css',
			array(),
			'1.0'
		);
	}

	/**
	 * Frontend js and css files
	 * @since 1.0.0
	 * @return void
	 */
	public function bdfwbp_frontend_scripts() {
		// Only load on single product pages
		if ( ! is_product() ) {
			return;
		}

		wp_enqueue_script( 'bdfw-front', BDFWBP_ASSETS . '/js/front.js', ['jquery'], BDFWBP_VERSION, true );
		wp_localize_script( 'bdfw-front', 'bdfw_ajax', [
			'ajax_url' => admin_url( 'admin-ajax.php' ),
			'nonce'    => wp_create_nonce( 'deposits_nonce' ),
		] );
	}

	/**
	 * Is the pro version activated ?
	 * @since 1.0.0
	 * @return bool
	 */
	public static function is_pro_version_activated() {
		return class_exists( '\Bright_Deposits_For_WooCommerce_PRO\Premium\Bootstrap' );
	}

	/**
	 * Try to join the pro version of the plugin.
	 * This method is called to ensure that the free version can interact with the pro version if available.
	 * @since 1.0.0
	 * @return void
	 */
	public function try_to_join_pro_version() {
		/**
		 * BDFWBP_CUSTOM_ACTION_HOOK: Allows developers to hook into the free version of the plugin.
		 * @since 1.0.0
		 * @return void
		 */
		do_action( 'bdfwbp_free_version_loaded' );
	}

	/**
     * It will try to load redux framwork without reproducing any errors.
     * 
     * @since 1.0
     * @access public
     * 
     * @return bool
     */
	public function attempt_loading_redux_framework() {

		/**
		 * If Redux has already been loaded by another plugin 
		 * And has a different version than the one required for this project
		 */
		if( class_exists( 'Redux' ) ) {

			$redux_version = class_exists( 'Redux_Core' ) && property_exists( 'Redux_Core', 'version' ) ? \Redux_Core::$version : '1.0';

			$redux_major_version = explode( '.', $redux_version );
			$redux_major_version_expected = explode( '.', BDFWBP_REDUX_VERSION );
			$same_major_version = isset( $redux_major_version[0] ) && isset( $redux_major_version_expected[0] ) && $redux_major_version[0] === $redux_major_version_expected[0];

			if( !$same_major_version ) {
				add_action( 'admin_notices', function(){
					$msg_full= 'Oops! There seems to be a conflict of the <b>' . esc_html( BDFWBP_PLUGIN_NAME ) . '</b> plugin with another plugin you have installed. Please contact <a target="_blank" href="' . esc_attr( Settings::SUPPORT_LINK ) . '">Bright Plugins team</a> so we can help you.';
					echo '<div class="notice notice-error"><p>'. wp_kses_post( $msg_full ) .'</p></div>';
				});
				return false;
			}
		}

		//...
		if ( ! class_exists( 'Redux' ) ) {

			if ( !file_exists( BDFWBP_PLUGIN_DIR . '/vendor/redux-framework/redux-framework.php' ) ) {
				add_action( 'admin_notices', function(){
					$msg_full= 'Oops! There seems to be a conflict of the <b>' . esc_html( BDFWBP_PLUGIN_NAME ) . '</b> plugin with another plugin you have installed. Please contact <a target="_blank" href="' . esc_attr( Settings::SUPPORT_LINK ) . '">Bright Plugins team</a> so we can help you.';
					echo '<div class="notice notice-error"><p>'. wp_kses_post( $msg_full ) .'</p></div>';
				});
				return false;
			}

			require_once BDFWBP_PLUGIN_DIR . '/vendor/redux-framework/redux-framework.php';
        }

		return true;
	}
}