<?php
/**
 * Admin Assets
 *
 * @package SimpleCalendar\Admin
 */
namespace SimpleCalendar\Admin;

if (!defined('ABSPATH')) {
	exit();
}

/**
 * Admin scripts and styles.
 *
 * Handles the plugin scripts and styles for back end dashboard pages.
 *
 * @since 3.0.0
 */
class Assets
{
	/**
	 * Check for SC setting page.
	 *
	 * @since 3.4.1
	 */

	protected $current_page = '';

	/**
	 * Hook in tabs.
	 *
	 * @since 3.0.0
	 */
	public function __construct()
	{
		add_action('admin_enqueue_scripts', [$this, 'load']);

		$this->current_page = sanitize_text_field(wp_unslash($_GET['page'] ?? ''));
	}

	/**
	 * Enqueue scripts and styles.
	 *
	 * @since 3.0.0
	 */
	public function load()
	{
		$css_path = SIMPLE_CALENDAR_ASSETS . 'generated/';
		$css_path_vendor = $css_path . 'vendor/';
		$js_path = SIMPLE_CALENDAR_ASSETS . 'generated/';
		$js_path_vendor = $js_path . 'vendor/';
		$sc_screen = get_current_screen();

		/* ====================== *
		 * Register Admin Scripts *
		 * ====================== */

		// TipTip uses ".minified.js" filename ending.
		wp_register_script(
			'simcal-tiptip',
			$js_path_vendor . 'jquery.tipTip.minified.js',
			['jquery'],
			SIMPLE_CALENDAR_VERSION,
			true,
		);
		wp_register_script('simcal-select2', $js_path_vendor . 'select2.min.js', [], SIMPLE_CALENDAR_VERSION, true);
		wp_register_script(
			'simcal-admin',
			$js_path . 'admin.min.js',
			['jquery', 'jquery-ui-sortable', 'jquery-ui-datepicker', 'wp-color-picker', 'simcal-tiptip', 'simcal-select2'],
			SIMPLE_CALENDAR_VERSION,
			true,
		);
		wp_register_script(
			'simcal-admin-add-calendar',
			$js_path . 'admin-add-calendar.min.js',
			['simcal-select2'],
			SIMPLE_CALENDAR_VERSION,
			true,
		);
		wp_register_script(
			'simcal-oauth-helper-admin',
			$js_path . 'oauth-helper-admin.min.js',
			['jquery'],
			SIMPLE_CALENDAR_VERSION,
			true,
		);

		$connect_screen_ids = [
			'calendar_page_simple-calendar_settings',
			'index_page_simple-calendar_settings',
			'dashboard_page_simple-calendar_settings',
		];

		$is_connect_page = $this->current_page == 'simple-calendar_settings';

		// OAuth helper UI (deauthenticate + legacy auth tabs). Safe to enqueue on Connect/Settings
		// because it no-ops when its target elements are not present.
		if ($is_connect_page) {
			wp_enqueue_script('simcal-oauth-helper-admin');
			wp_localize_script('simcal-oauth-helper-admin', 'oauth_admin', simcal_common_scripts_variables());
		}
		/* ===================== *
		 * Register Admin Styles *
		 * ===================== */

		wp_register_style('simcal-select2', $css_path_vendor . 'select2.min.css', [], SIMPLE_CALENDAR_VERSION);
		wp_register_style(
			'simcal-admin',
			$css_path . 'admin.min.css',
			['wp-color-picker', 'simcal-select2'],
			SIMPLE_CALENDAR_VERSION,
		);
		wp_register_style('sc-design-system', $css_path . 'design-system.min.css', [], SIMPLE_CALENDAR_VERSION);
		wp_register_style('sc-connect', $css_path . 'connect.min.css', ['sc-design-system'], SIMPLE_CALENDAR_VERSION);
		wp_register_style('sc-add-ons', $css_path . 'add-ons.min.css', ['sc-design-system'], SIMPLE_CALENDAR_VERSION);
		wp_register_style(
			'sc-misc-settings',
			$css_path . 'misc-settings.min.css',
			['sc-design-system'],
			SIMPLE_CALENDAR_VERSION,
		);
		wp_register_style(
			'simcal-admin-add-calendar',
			$css_path . 'admin-add-calendar.min.css',
			['simcal-select2'],
			SIMPLE_CALENDAR_VERSION,
		);
		wp_register_style('sc-global-admin', $css_path . 'admin-global.min.css', [], SIMPLE_CALENDAR_VERSION);

		if (simcal_is_admin_screen() !== false) {
			// Global admin styles (e.g. menu badges) used outside plugin screens too.
			wp_enqueue_style('sc-global-admin');

			wp_enqueue_script('simcal-admin');
			wp_localize_script('simcal-admin', 'simcal_admin', simcal_common_scripts_variables());
			// Always expose simcal_connect when admin script loads (JS only uses it when #simcal-connect-page exists).
			wp_localize_script('simcal-admin', 'simcal_connect', [
				'ajax_url' => admin_url('admin-ajax.php'),
				'nonce' => wp_create_nonce('simcal_connect_validate_google_api_key'),
				'google_api_key_health_nonce' => wp_create_nonce('simcal_connect_google_api_key_health'),
				'oauth_check_nonce' => wp_create_nonce('simcal_connect_oauth_via_sc_check'),
				'mark_pro_connection_nonce' => wp_create_nonce('simcal_mark_pro_connection'),
				'check_icon_url' => SIMPLE_CALENDAR_ASSETS . 'images/admin/check.svg',
				'warning_icon_url' => SIMPLE_CALENDAR_ASSETS . 'images/admin/warning.svg',
				'strings' => [
					'show_api_key' => __('Show API key', 'google-calendar-events'),
					'hide_api_key' => __('Hide API key', 'google-calendar-events'),
					'please_enter_api_key' => __('Please enter API key', 'google-calendar-events'),
					'api_key_format_invalid' => __('API key format looks invalid', 'google-calendar-events'),
					'25_ready' => __('25% Ready', 'google-calendar-events'),
					'50_ready' => __('50% Ready', 'google-calendar-events'),
					'67_ready' => __('67% Ready', 'google-calendar-events'),
					'75_ready' => __('75% Ready', 'google-calendar-events'),
					'100_ready' => __('100% Ready', 'google-calendar-events'),
					'oauth_checking' => __('Checking…', 'google-calendar-events'),
					'oauth_connected' => __('Connected', 'google-calendar-events'),
					'oauth_error' => __('Error', 'google-calendar-events'),
					'oauth_not_comunicate' => __('Not able to communicate with Google.', 'google-calendar-events'),
					'oauth_ajax_url_not_found' => __('Ajax URL not found.', 'google-calendar-events'),
					'oauth_not_connected' => __('Not Connected', 'google-calendar-events'),
					'google_api_key_public_calendar_failed' => __(
						'Could not load public calendar data with this API key. Events may not display until this is fixed.',
						'google-calendar-events',
					),
				],
			]);

			wp_enqueue_style('simcal-admin');
			wp_enqueue_style('sc-design-system');

			// Connect page specific styles.
			if ($is_connect_page) {
				wp_enqueue_style('sc-connect');
			}
		} else {
			// Still enqueue global admin styles on non-plugin screens.
			wp_enqueue_style('sc-global-admin');

			global $post_type;
			$screen = get_current_screen();

			$post_types = [];
			$settings = get_option('simple-calendar_settings_calendars');
			if (isset($settings['general']['attach_calendars_posts'])) {
				$post_types = $settings['general']['attach_calendars_posts'];
			}

			$conditions = [in_array($post_type, (array) $post_types), $screen->id == 'widgets'];

			if (in_array(true, $conditions)) {
				wp_enqueue_script('simcal-admin-add-calendar');
				wp_localize_script('simcal-admin-add-calendar', 'simcal_admin', [
					'locale' => get_locale(),
					'text_dir' => is_rtl() ? 'rtl' : 'ltr',
				]);

				wp_enqueue_style('simcal-admin-add-calendar');
			}
		}

		// Misc Settings (Calendars + Advanced cards): design-system layout.
		if ($sc_screen && 'calendar_page_simple-calendar_misc_settings' === $sc_screen->id) {
			wp_enqueue_style('sc-misc-settings');
			wp_enqueue_style('sc-connect');
		}

		// Add-ons page: design-system layout + Connect progress sidebar styles.
		if ($sc_screen && 'calendar_page_simple-calendar_add_ons' === $sc_screen->id) {
			wp_enqueue_style('sc-add-ons');
			wp_enqueue_style('sc-connect');
		}
	}
}
