<?php
/**
 * Admin assets management for Postomatic plugin.
 *
 * This file handles the loading of styles and scripts for the admin area of the plugin.
 *
 * @since   1.0.0
 * @package Postomatic
 */

namespace Postomatic\Admin;

use Postomatic\Constants;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class for handling admin scripts and styles.
 */
class Admin_Assets {


	/**
	 * List of plugin pages.
	 *
	 * @var array
	 */
	private $plugin_pages = array();

	/**
	 * List of hook pages.
	 *
	 * @var array
	 */
	private $hook_pages = array();

	/**
	 * Version of the plugin.
	 *
	 * @var string
	 */
	private $version;

	/**
	 * Constructor for Admin_Assets class.
	 *
	 * Sets up the plugin pages and hook pages arrays.
	 */
	public function __construct() {
		$this->version = ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ? time() : POSTOMATIC_VERSION;

		$this->plugin_pages = array(
			Constants::PAGE_MAIN,
			Constants::PAGE_MEMBERSHIP,
			Constants::PAGE_ADD_CREDIT,
			Constants::PAGE_SUPPORT,
			Constants::PAGE_AUTOPILOT,
		);

		$this->hook_pages = array(
			'main'       => 'toplevel_page_' . Constants::PAGE_MAIN,
			'membership' => 'postomatic_page_' . Constants::PAGE_MEMBERSHIP,
			'add_credit' => 'postomatic_page_' . Constants::PAGE_ADD_CREDIT,
			'support'    => 'postomatic_page_' . Constants::PAGE_SUPPORT,
			'autopilot'  => 'postomatic_page_' . Constants::PAGE_AUTOPILOT,
		);
	}

	/**
	 * Initialize the admin assets.
	 */
	public function init() {
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_public_styles' ) );

		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
	}

	/**
	 * Register and enqueue public-facing styles.
	 */
	public function enqueue_public_styles() {
		wp_enqueue_style(
			'postomatic-public',
			POSTOMATIC_PLUGIN_URL . 'includes/public/css/wp-postomatic-public.css',
			array(),
			$this->version,
			'all'
		);
	}

	/**
	 * Register and enqueue admin-specific styles.
	 *
	 * @param string $hook Current admin page hook.
	 */
	public function enqueue_styles( $hook ) {

		// Only load on plugin pages.
		if ( $this->is_plugin_page( $hook ) ) {
			/*
			 * Canonical checkbox/radio rendering. Loaded on every plugin page so
			 * the controls look and behave identically everywhere. Previously each
			 * page shipped its own copy of these rules and they drifted apart,
			 * leaving checkboxes with no visible tick on some pages.
			 */
			wp_enqueue_style(
				'postomatic-form-controls',
				POSTOMATIC_PLUGIN_URL . 'includes/admin/assets/css/wp-postomatic-form-controls.css',
				array(),
				$this->version
			);

			if ( $this->validate_page( $hook, Constants::PAGE_MAIN ) ) {

				wp_enqueue_style(
					'postomatic-admin',
					POSTOMATIC_PLUGIN_URL . 'includes/admin/assets/css/wp-postomatic-admin.css',
					array(),
					$this->version
				);

				wp_enqueue_style(
					'sweetalert2',
					POSTOMATIC_PLUGIN_URL . 'includes/admin/assets/css/vendor/sweetalert2.min.css',
					array(),
					$this->version
				);

				wp_enqueue_style(
					'postomatic-enhanced',
					POSTOMATIC_PLUGIN_URL . 'includes/admin/assets/css/wp-postomatic-enhanced.css',
					array( 'postomatic-admin' ),
					$this->version
				);

				// Enqueue Vue Select CSS.
				wp_enqueue_style(
					'vue-select',
					POSTOMATIC_PLUGIN_URL . 'includes/admin/assets/css/vendor/vue-select.css',
					array(),
					$this->version
				);

				// Enqueue Floating Credit Tracker CSS.
				wp_enqueue_style(
					'floating-credit-tracker',
					POSTOMATIC_PLUGIN_URL . 'includes/admin/assets/css/floating-credit-tracker.css',
					array(),
					$this->version
				);

			}

			if ( $this->validate_page( $hook, Constants::PAGE_SUPPORT ) ) {
				// Support Page.
				wp_enqueue_style(
					'tailwindcss',
					plugin_dir_url( __FILE__ ) . 'assets/css/vendor/tailwind.min.css',
					array(),
					$this->version,
					'all'
				);

				wp_enqueue_style(
					'postomatic-support-styles',
					plugin_dir_url( __FILE__ ) . 'assets/css/wp-postomatic-support-page.css',
					array(),
					$this->version,
					'all'
				);

				// Tailwind CSS.
				wp_enqueue_style(
					'tailwindcss',
					plugin_dir_url( __FILE__ ) . 'assets/css/vendor/tailwind.min.css',
					array(),
					$this->version,
					'all'
				);

			}

			if ( $this->validate_page( $hook, Constants::PAGE_ADD_CREDIT ) ) {
				wp_enqueue_style(
					'postomatic-add-credit-styles',
					plugin_dir_url( __FILE__ ) . 'assets/css/wp-postomatic-add-credit.css',
					array(),
					$this->version,
					'all'
				);
			}

			if ( $this->validate_page( $hook, Constants::PAGE_MEMBERSHIP ) ) {
				wp_enqueue_style(
					'postomatic-subscription-styles',
					plugin_dir_url( __FILE__ ) . 'assets/css/wp-postomatic-subscription.css',
					array(),
					$this->version,
					'all'
				);
			}

			if ( $this->validate_page( $hook, Constants::PAGE_AUTOPILOT ) ) {
				wp_enqueue_style(
					'postomatic-autopilot-styles',
					plugin_dir_url( __FILE__ ) . 'assets/css/wp-postomatic-autopilot.css',
					array(),
					$this->version,
					'all'
				);
			}
		}
	}

	/**
	 * Register and enqueue admin-specific scripts.
	 *
	 * @param string $hook Current admin page hook.
	 */
	public function enqueue_scripts( $hook ) {

		// Only load on plugin pages.
		if ( $this->is_plugin_page( $hook ) ) {

			wp_enqueue_media();

			// error_log( $hook, $hookConstants::PAGE_AUTOPILOT);
			// Enqueue Vue.js for the autopilot page.
			if ( $this->validate_page( $hook, Constants::PAGE_AUTOPILOT ) ) {
				// We don't need to load main-page.js on the autopilot page.

				wp_enqueue_script(
					'axios',
					POSTOMATIC_PLUGIN_URL . 'includes/admin/assets/js/vendor/axios.min.js',
					array(),
					$this->version,
					true
				);
				wp_enqueue_script(
					'vue-js',
					POSTOMATIC_PLUGIN_URL . 'includes/admin/assets/js/vendor/vue.global.js',
					array(),
					$this->version,
					true
				);

				// Enqueue lodash for utility functions.
				wp_enqueue_script(
					'lodash-js',
					POSTOMATIC_PLUGIN_URL . 'includes/admin/assets/js/vendor/lodash.min.js',
					array(),
					$this->version,
					true
				);

				// Enqueue the autopilot script.
				wp_enqueue_script(
					'postomatic-autopilot',
					POSTOMATIC_PLUGIN_URL . 'includes/admin/assets/js/autopilot.js',
					array( 'vue-js', 'lodash-js' ),
					$this->version,
					true
				);
			}

			if ( $this->validate_page( $hook, Constants::PAGE_MAIN ) ) {
				// Enqueue select2 from WooCommerce for enhanced dropdowns.
				wp_enqueue_script( 'jquery' );

				// Check if WooCommerce is active and use its select2 files.
				if ( defined( 'WC_PLUGIN_FILE' ) && file_exists( WP_PLUGIN_DIR . '/woocommerce/assets/js/select2/select2.full.min.js' ) ) {
					$select2_js_path  = plugins_url( 'woocommerce/assets/js/select2/select2.full.min.js' );
					$select2_css_path = plugins_url( 'woocommerce/assets/css/select2.css' );
				} else {
					// Use local Select2 files instead of CDN for security.
					$select2_js_path  = POSTOMATIC_PLUGIN_URL . 'includes/admin/assets/js/vendor/select2.full.min.js';
					$select2_css_path = POSTOMATIC_PLUGIN_URL . 'includes/admin/assets/js/vendor/select2.min.css';
				}

				wp_enqueue_script( 'select2', $select2_js_path, array( 'jquery' ), '4.0.13', true );
				wp_enqueue_style( 'select2', $select2_css_path, array(), '4.0.13' );

				wp_enqueue_script(
					'sweetalert2',
					POSTOMATIC_PLUGIN_URL . 'includes/admin/assets/js/vendor/sweetalert2.all.min.js',
					array(),
					$this->version,
					true
				);

				wp_enqueue_script(
					'postomatic-admin_vue_before_styles',
					POSTOMATIC_PLUGIN_URL . 'includes/admin/assets/js/vendor/vue.js',
					array( 'underscore' ),
					$this->version,
					false
				);

				wp_enqueue_script(
					'postomatic-admin_credit_tracker',
					POSTOMATIC_PLUGIN_URL . 'includes/admin/assets/js/components/credit-tracker.js',
					array(),
					$this->version,
					false
				);

				wp_enqueue_script(
					'postomatic-admin_recent_posts',
					POSTOMATIC_PLUGIN_URL . 'includes/admin/assets/js/components/recent-posts-table.js',
					array(),
					$this->version,
					false
				);

				// Enqueue Vue Select.
				wp_enqueue_script(
					'vue-select',
					POSTOMATIC_PLUGIN_URL . 'includes/admin/assets/js/vendor/vue-select.min.js',
					array( 'postomatic-admin_vue_before_styles' ),
					$this->version,
					false
				);

				// Enqueue Floating Credit Tracker.
				wp_enqueue_script(
					'floating-credit-tracker',
					POSTOMATIC_PLUGIN_URL . 'includes/admin/assets/js/components/floating-credit-tracker.js',
					array( 'postomatic-admin_vue_before_styles', 'vue-select' ),
					$this->version,
					false
				);

				// Pass variables to JS.
				wp_localize_script(
					'postomatic-admin',
					'postomaticAdmin',
					array(
						'ajaxUrl'      => admin_url( 'admin-ajax.php' ),
						'nonce'        => wp_create_nonce( 'postomatic_admin_nonce' ),
						'create'       => wp_create_nonce( 'postomatic_generate_post' ),
						'nonce_search' => wp_create_nonce( 'wp_rest' ),
					)
				);

			}

			if ( $this->validate_page( $hook, Constants::PAGE_MEMBERSHIP ) ) {
				wp_enqueue_script(
					'vue-js',
					POSTOMATIC_PLUGIN_URL . 'includes/admin/assets/js/vendor/vue.global.prod.js',
					array(),
					'3.0.0',
					false
				);

				// Enqueue SweetAlert for notifications.
				wp_enqueue_script(
					'sweetalert2',
					POSTOMATIC_PLUGIN_URL . 'includes/admin/assets/css/vendor/sweetalert2.min.css',
					array(),
					'11.0.0',
					true
				);

				// Localize the script with the data needed.
				wp_localize_script(
					'postomatic-subscription',
					'postomatic_api_data',
					array(
						'plan_active'   => esc_url( admin_url( 'admin-ajax.php' ) ),
						'pricing_plans' => esc_url( admin_url( 'admin-ajax.php' ) ),
						'credit_url'    => esc_url( admin_url( 'admin-ajax.php' ) ),
						'site_url'      => esc_js( get_site_url() ),
						'admin_nonce'   => esc_js( wp_create_nonce( 'postomatic_admin_nonce' ) ),
					)
				);
			}

			if ( $this->validate_page( $hook, Constants::PAGE_SUPPORT ) ) {
				// Vue.js.
				wp_enqueue_script(
					'vue-js',
					POSTOMATIC_PLUGIN_URL . 'includes/admin/assets/js/vendor/vue.global.js',
					array(),
					$this->version,
					true
				);
				// Axios.
				wp_enqueue_script(
					'axios',
					POSTOMATIC_PLUGIN_URL . 'includes/admin/assets/js/vendor/axios.min.js',
					array(),
					$this->version,
					true
				);
			}

				wp_localize_script(
					'my-search',
					'WP_API',
					array(
						'root'  => esc_url_raw( rest_url() ),
						'nonce' => wp_create_nonce( 'wp_rest' ),
					)
				);

		}
	}

	/**
	 * Check if current page is a plugin page.
	 *
	 * @param string $hook Current admin page hook.
	 *
	 * @return bool Whether this is a plugin page.
	 */
	private function is_plugin_page( $hook ) {
		return in_array( $hook, $this->hook_pages, true );
	}

	/**
	 * Validates if a page matches an expected page.
	 *
	 * @param string $page Current page hook.
	 * @param string $expected_page Expected page to check against.
	 *
	 * @return bool True if page matches expected page, false otherwise.
	 */
	private function validate_page( $page, $expected_page = null ) {
		if ( false !== strpos( $page, $expected_page ) ) {
			return true;
		}

		return false;
	}

	/**
	 * Enqueue Vue.js script.
	 *
	 * This method can be called directly to enqueue Vue.js for specific templates.
	 */
	public function enqueue_vue_js() {
		wp_enqueue_script(
			'vue-js',
			POSTOMATIC_PLUGIN_URL . 'includes/admin/assets/js/vendor/vue.global.prod.js',
			array(),
			$this->version,
			false
		);
	}
}
