<?php

/**
 * The admin-specific functionality of the plugin.
 *
 * @link       http://example.com
 * @since      1.0.0
 *
 * @package    Mds_Product_Bulk_Editor_For_Woocommerce
 * @subpackage Mds_Product_Bulk_Editor_For_Woocommerce/admin
 */

/**
 * The admin-specific functionality of the plugin.
 *
 * Defines the plugin name, version, and two examples hooks for how to
 * enqueue the admin-specific stylesheet and JavaScript.
 *
 * @package    Mds_Product_Bulk_Editor_For_Woocommerce
 * @subpackage Mds_Product_Bulk_Editor_For_Woocommerce/admin
 * @author     Your Name <email@example.com>
 */
class MDSPBE_Admin {

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

	/**
	 * 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    $mds_product_bulk_editor       The name of this plugin.
	 * @param      string    $version    The version of this plugin.
	 */
	public function __construct( $mds_product_bulk_editor, $version ) {

		$this->mds_product_bulk_editor = $mds_product_bulk_editor;
		$this->version = $version;

	}

	/**
	 * Register the stylesheets for the admin area.
	 *
	 * @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 MDSPBE_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The MDSPBE_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */

		$screen = get_current_screen();
		if ( str_contains( $screen->id,  'mds-product-bulk-editor' ) ) {

			/**
			 * Plugin style sheet
			 */
			wp_enqueue_style( 
				$this->mds_product_bulk_editor, 
				plugin_dir_url( __FILE__ ) . 'css/mds-product-bulk-editor-for-woocommerce-admin.css', 
				array(), 
				$this->version, 
				'all' 
			);
			
			/**
			 * Tom select
			 */
			if ( ! wp_style_is( 'tom-select-css', 'enqueued' ) ) {
				wp_enqueue_style( 
					'tom-select-css', 
					plugin_dir_url( __FILE__ ) . 'css/tom-select.default.min.css', 
					array(), 
					'2.4.3' 
				);
			}

			/**
			 * Bootstrap 5
			 */
			if ( ! wp_style_is( 'bootstrap_css', 'enqueued' ) ) {
				wp_enqueue_style( 
					'bootstrap_css', 
					plugin_dir_url( __FILE__ ) . 'bootstrap/css/bootstrap.min.css',
					array(),
					'5.3.5',
				);
			}
		}
	}

	/**
	 * Register the JavaScript for the admin area.
	 *
	 * @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 MDSPBE_Loader as all of the hooks are defined
		 * in that particular class.
		 *
		 * The MDSPBE_Loader will then create the relationship
		 * between the defined hooks and the functions defined in this
		 * class.
		 */
		$screen = get_current_screen();
		if ( $screen->id === 'toplevel_page_mds-product-bulk-editor-for-woocommerce' ) {

			wp_enqueue_editor();

			/**
			 * Tom select
			 */
			if ( ! wp_script_is( 'tom-select-js', 'enqueued' ) ) {
				wp_enqueue_script( 
					'tom-select-js', 
					plugin_dir_url( __FILE__ ) . 'js/tom-select.complete.min.js', 
					array(), 
					'2.4.3', 
					true 
				);
			}

			/**
			 * Bootstrap 5
			 */
			if ( ! wp_script_is( 'bootstrap_js', 'enqueued' ) ) {
				wp_enqueue_script( 
					'bootstrap_js', 
					plugin_dir_url( __FILE__ ) . 'bootstrap/js/bootstrap.bundle.min.js', 
					array( 'jquery' ), 
					'5.3.5', 
					false 
				);
			}

			/**
			 * DOM Purify
			 */
			if ( ! wp_script_is( 'dom_purify', 'enqueued' ) ) {
				wp_enqueue_script( 
					'dom_purify', 
					plugin_dir_url( __FILE__ ) . 'js/purify.min.js', 
					array('jquery'), 
					'3.2.5', 
					true 
				);
			}


			/**
			 *  Enqueue WP scripts
			 */ 
			wp_enqueue_media();
			wp_enqueue_editor();
			
			
			/**
			 * Main script
			 */
			$is_production = true;
			if ( $is_production ) {
				wp_enqueue_script( 
					$this->mds_product_bulk_editor, 
					plugin_dir_url( __FILE__ ) . 'js/dist/mds-product-bulk-editor-for-woocommerce-admin.min.js', 
					array( 'jquery', 'wp-editor', 'bootstrap_js', 'tom-select-js', 'dom_purify' ), 
					$this->version, 
					false 
				);
			} else {
				echo '<div class="text-center fs-3" style="color: red; background: yellow;">DEVELOPING</div>';

				wp_enqueue_script( 
					$this->mds_product_bulk_editor, 
					plugin_dir_url( __FILE__ ) . 'js/src/mds-product-bulk-editor-for-woocommerce-admin.js', 
					array( 'jquery', 'wp-editor', 'bootstrap_js', 'tom-select-js', 'dom_purify' ), 
					$this->version, 
					false 
				);
			}

			
			// Localize data for the main script
			$data = [
				'restUrl' 		=> esc_url( rest_url() ), 
				'adminUrl' 		=> esc_url( admin_url() ), 
				'nonce'			=> wp_create_nonce( 'wp_rest' ),
				'editOpts'	 	=> mdspbe_get_attr_edit_options(),
				'toggleFields'	=> mdspbe_get_attributes_statuses(),
				'conditionals'	=> mdspbe_get_attributes_transposed_conditionals(),
				'attributes'	=> mdspbe_get_products_attributes(),
				'productTypes'	=> mdspbe_get_supported_product_types( true, false ),
				'i18n'			=> mdspbe_get_js_translated_strings(),
				'atumActive'	=> class_exists( '\Atum\PurchaseOrders\PurchaseOrders' ),
			];
	
			wp_localize_script( $this->mds_product_bulk_editor, 'mdspbeVars', $data );

		}

	}

	/**
	 * Register admin menues and submenues
	 *
	 * @since    1.0.0
	 */
	public function add_options_menu() {
		$icon_svg = 'data:image/svg+xml;base64,' . base64_encode('
			<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor">
			<rect x="3" y="4" width="7" height="6" rx="1" />
			<rect x="14" y="4" width="7" height="6" rx="1" />
			<rect x="3" y="14" width="7" height="6" rx="1" />
			<path d="M14 14h2.5l4.5 4.5-2.5 2.5L14 16.5V14z" />
			</svg>
			');

		// Main page
		add_menu_page(
			esc_html__( 'Bulk products',  'mds-product-bulk-editor-for-woocommerce' ),
			esc_html__( 'Bulk products',  'mds-product-bulk-editor-for-woocommerce' ),
			'edit_products',
			 'mds-product-bulk-editor-for-woocommerce', 
			[ $this, 'bulk_product_editor' ], 
			$icon_svg,
			29.99
		);


		// About
		add_submenu_page(
			 'mds-product-bulk-editor-for-woocommerce', 
			esc_html__( 'About',  'mds-product-bulk-editor-for-woocommerce' ),
			esc_html__( 'About',  'mds-product-bulk-editor-for-woocommerce' ),
			'manage_options',
			'mds-product-bulk-editor-about', 
			[ $this, 'bulk_product_editor_about' ], 
		);

	}


	public function bulk_product_editor() {
		mdspbe_render_main_plugin_page();
	}

	public function bulk_product_editor_about() {
		include_once MDSPBE_PATH . 'templates/about.php';
	}

}
