<?php
namespace MosPress\StoreAddonsForWoocommerce\Hook;
if ( ! defined( 'ABSPATH' ) ) exit;
use MosPress\StoreAddonsForWoocommerce\Helpers\Utils;
class Action_Hook
{
	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.3
	 * @access   private
	 * @var      string    $plugin_name    The ID of this plugin.
	 */
	private $plugin_name;
    private static $instance = null;
    public static function get_instance()
    {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    public function __construct()
	{
        $this->plugin_name = 'store-addons-for-woocommerce';
        add_action('admin_init', [$this, 'do_activation_redirect']);
        add_action('admin_menu', [$this, 'admin_menu']);
		add_action('admin_enqueue_scripts', [$this, 'dashboard_scripts']);
        add_action('current_screen', [$this, 'hide_admin_notices']);
        add_action('admin_head', [$this, 'option_form_submit']);
				
		add_action('admin_enqueue_scripts', [$this, 'admin_enqueue_scripts']);
		add_action('wp_enqueue_scripts', [$this, 'wp_enqueue_scripts']);
        add_action('upgrader_process_complete', [$this, 'update_completed'], 10, 2);
		
    }

	/**
	 * Redirect to the welcome pages.
	 *
	 * @since    1.0.3
	 */
	public function do_activation_redirect()
	{
		if (get_option('store_addons_for_woocommerce_do_activation_redirect')) {
			delete_option('store_addons_for_woocommerce_do_activation_redirect');
			wp_safe_redirect(admin_url('admin.php?page=' . $this->plugin_name));
		}
	}


	/**
	 * Adding menu to admin menu.
	 *
	 * @since    1.0.3
	 */
	public function admin_menu()
	{
		add_menu_page(
			esc_html(STORE_ADDONS_FOR_WOOCOMMERCE_NAME),
			esc_html(STORE_ADDONS_FOR_WOOCOMMERCE_NAME),
			'manage_options',
			$this->plugin_name,
			array($this, 'store_addons_for_woocommerce_dashboard_html'),
			STORE_ADDONS_FOR_WOOCOMMERCE_URL . 'assets/images/menu-icon.svg',
			57
		);
	}
	/**
	 * Loading plugin Welcome page.
	 *
	 * @since    1.0.3
	 */
	public function store_addons_for_woocommerce_dashboard_html()
	{
		if (!current_user_can('manage_options')) {
			return;
		}
		// include_once(STORE_ADDONS_FOR_WOOCOMMERCE_PATH . 'admin/partials/' . $this->plugin_name . '-admin-display.php');
		echo '<div id="store-addons-for-woocommerce-settings-react-app" class="build-with-bootstrap"></div>';
	}
	public function dashboard_scripts($hook)
	{
		if ($hook !== 'toplevel_page_store-addons-for-woocommerce') {
			return;
		}

		// Automatically load dependencies and version generated by wp-scripts
		$asset_file = include(STORE_ADDONS_FOR_WOOCOMMERCE_PATH . 'build/index.asset.php');

		wp_enqueue_script(
			'store-addons-for-woocommerce-react-app',
			STORE_ADDONS_FOR_WOOCOMMERCE_URL . 'build/index.js',
			$asset_file['dependencies'],
			$asset_file['version'],
			true
		);

		// // NEW: Enqueue Compiled Tailwind CSS
		// wp_enqueue_style(
		// 	'store-addons-for-woocommerce-tailwind',
		// 	STORE_ADDONS_FOR_WOOCOMMERCE_URL . 'build/index.css',
		// 	array(),
		// 	$asset_file['version']
		// );

	}
	public function admin_enqueue_scripts($hook)
	{
		wp_enqueue_script('jquery');
		wp_enqueue_media();

		wp_enqueue_script('store-addons-for-woocommerce-admin-ajax', STORE_ADDONS_FOR_WOOCOMMERCE_URL . 'assets/js/admin-ajax.js', array('jquery'), STORE_ADDONS_FOR_WOOCOMMERCE_VERSION, false);
		wp_enqueue_script('store-addons-for-woocommerce-admin-script', STORE_ADDONS_FOR_WOOCOMMERCE_URL . 'assets/js/admin.js', array('jquery'), STORE_ADDONS_FOR_WOOCOMMERCE_VERSION, false);
		$ajax_params = [
			'admin_url' => admin_url(),
			'home_url' => home_url(),
			'ajax_url' => admin_url('admin-ajax.php'),
			'image_url' => STORE_ADDONS_FOR_WOOCOMMERCE_URL . 'assets/images/',
			'_admin_nonce' => esc_attr(wp_create_nonce('store_addons_for_woocommerce_admin_nonce')),
			'api_nonce' => esc_attr(wp_create_nonce('wp_rest')),
			'get_current_user_id' => get_current_user_id(),
			'root'  => esc_url_raw( rest_url() ),
    		'nonce' => wp_create_nonce('wp_rest'),
			// 'default_colors' => store_addons_for_woocommerce_get_default_colors(),
			// 'default_gradients' => store_addons_for_woocommerce_get_default_gradients(),
			'proURL' => 'https://mostak-shahid.github.io/plugins/store-addons-for-woocommerce-pro.html',
			// 'isPro' => is_plugin_active( 'store-addons-for-woocommerce-pro/store-addons-for-woocommerce-pro.php' ) ? true : false,
			// 'install_plugin_wpnonce' => esc_attr(wp_create_nonce('updates')),
		];
		if (is_plugin_active( 'store-addons-for-woocommerce-pro/store-addons-for-woocommerce-pro.php' )) {
			$plugins = get_plugins();
			$version = $plugins['store-addons-for-woocommerce-pro/store-addons-for-woocommerce-pro.php']['Version'];
			$ajax_params['isPro'] = true;
			$ajax_params['proVersion'] = $version;
		}
		wp_localize_script('store-addons-for-woocommerce-admin-ajax', 'store_addons_for_woocommerce_ajax_obj', $ajax_params);

		wp_enqueue_style( 'wp-components' );
		wp_enqueue_style('store-addons-for-woocommerce-admin', STORE_ADDONS_FOR_WOOCOMMERCE_URL . 'assets/css/admin.css', array(), STORE_ADDONS_FOR_WOOCOMMERCE_VERSION, 'all');

	}
	public function wp_enqueue_scripts($hook)
	{
		// wp_enqueue_script(
		// 	'store-addons-for-woocommerce-react-app',
		// 	STORE_ADDONS_FOR_WOOCOMMERCE_URL . 'build/index.js',
		// );
		
		wp_enqueue_style(
			'store-addons-for-woocommerce-admin-public',
			STORE_ADDONS_FOR_WOOCOMMERCE_URL . 'assets/css/public.css',
			array(),
		);

	}
	public function store_addons_for_woocommerce_dashboard_react_page_html()
	{
		if (!current_user_can('manage_options')) {
			return;
		}
		include_once(STORE_ADDONS_FOR_WOOCOMMERCE_PATH . 'admin/partials/' . $this->plugin_name . '-admin-display-react.php');
	}


	/**
	 * Removing all notieces from settings page.
	 *
	 * @since    1.0.3
	 */
	public function hide_admin_notices()
	{
		// $current_screen = get_current_screen();
		// var_dump($current_screen->id);
		if (Utils::store_addons_for_woocommerce_is_plugin_page()) {
			remove_all_actions('user_admin_notices');
			remove_all_actions('admin_notices');
		}
	}
	public function option_form_submit()
	{
		// $store_addons_for_woocommerce_options = array_replace_recursive(store_addons_for_woocommerce_get_option(), get_option('store_addons_for_woocommerce_options', []));
		if (isset($_POST['store_addons_for_woocommerce_options_form_field']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['store_addons_for_woocommerce_options_form_field'])), 'store_addons_for_woocommerce_options_form_action')) {

			$err = 0;

			if (!$err) {
				$_POST['settings-updated'] = true;
			}

			// var_dump($_POST);
		}
		// update_option('store_addons_for_woocommerce_options', $store_addons_for_woocommerce_options);
	}
	public function update_completed($upgrader_object, $options)
	{

		// If an update has taken place and the updated type is plugins and the plugins element exists
		if ($options['action'] == 'update' && $options['type'] == 'plugin' && isset($options['plugins'])) {
			foreach ($options['plugins'] as $plugin) {
				// Check to ensure it's my plugin
				if ($plugin == plugin_basename(STORE_ADDONS_FOR_WOOCOMMERCE_MAIN_FILE)) {
					// do stuff here
					// $store_addons_for_woocommerce_options = array_replace_recursive(store_addons_for_woocommerce_get_option(), get_option('store_addons_for_woocommerce_options', []));
					// update_option('store_addons_for_woocommerce_options', $store_addons_for_woocommerce_options);
				}
			}
		}
	}
}