<?php
if ( ! defined( 'ABSPATH' ) ) {
	die( 'Direct access is not allowed.' );
}

/**
 * Class for managing admin assets (scripts and styles)
 */
class FinalPOS_Admin_Assets {
	/**
	 * Initialize the class
	 */
	public static function init() {
		add_action( 'admin_enqueue_scripts', array( __CLASS__, 'register_assets' ) );
		add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_assets' ) );
	}

	/**
	 * Register all scripts and styles
	 */
	public static function register_assets() {
		// Register Google Poppins font
		wp_register_style(
			FINALPOS_HANDLE_POPPINS_FONT,
			'https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap',
			array(),
			FINALPOS_VERSION
		);

		// Register styles
		wp_register_style(
			FINALPOS_HANDLE_COMMON,
			FINALPOS_PLUGIN_URL . 'assets/css/finalpos-common.css',
			array( FINALPOS_HANDLE_POPPINS_FONT ),
			FINALPOS_VERSION
		);

		wp_register_style(
			FINALPOS_HANDLE_WIZARD,
			FINALPOS_PLUGIN_URL . 'assets/css/finalpos-wizard.css',
			array( FINALPOS_HANDLE_COMMON ),
			FINALPOS_VERSION
		);

		wp_register_style(
			FINALPOS_HANDLE_MODALS,
			FINALPOS_PLUGIN_URL . 'assets/css/finalpos-modals.css',
			array( FINALPOS_HANDLE_COMMON ),
			FINALPOS_VERSION
		);

		wp_register_style(
			FINALPOS_HANDLE_LOADER,
			FINALPOS_PLUGIN_URL . 'assets/css/finalpos-loader.css',
			array(),
			FINALPOS_VERSION
		);

		wp_register_style(
			FINALPOS_HANDLE_DASHBOARD,
			FINALPOS_PLUGIN_URL . 'assets/css/finalpos-dashboard.css',
			array( FINALPOS_HANDLE_COMMON ),
			FINALPOS_VERSION
		);

		// Register scripts
		wp_register_script(
			FINALPOS_HANDLE_SETTINGS_FORM,
			FINALPOS_PLUGIN_URL . 'assets/js/settings-form.js',
			array( 'jquery' ),
			FINALPOS_VERSION,
			true
		);
	}

	/**
	 * Enqueue scripts and styles based on current screen
	 */
	public static function enqueue_assets( $hook ) {

		// Always load admin icon styles on all admin pages.
		wp_enqueue_style( FINALPOS_HANDLE_ADMIN_ICONS );

		// Always load the Poppins font and common styles on plugin pages
		if ( strpos( $hook, 'finalpos' ) !== false ) {
			wp_enqueue_style( FINALPOS_HANDLE_POPPINS_FONT );
			wp_enqueue_style( FINALPOS_HANDLE_COMMON );
		}

		// Load specific styles for wizard page.
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading page parameter for asset loading.
		$current_page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';

		if ( $current_page === FINALPOS_PAGE_SETUP ) {
			wp_enqueue_style( FINALPOS_HANDLE_WIZARD );

			// Check if we're on the settings stage.
			$current_stage = get_option( FINALPOS_OPTION_CURRENT_STAGE, FINALPOS_STAGE_AUTH_KEY );
			if ( $current_stage === FINALPOS_STAGE_SETTINGS ) {
				wp_enqueue_script( FINALPOS_HANDLE_SETTINGS_FORM );
				wp_enqueue_style( FINALPOS_HANDLE_MODALS );
			}
		}

		// Load specific styles for dashboard and logs pages.
		if ( $current_page === FINALPOS_PAGE_DASHBOARD || $current_page === FINALPOS_PAGE_LOGS ) {
			wp_enqueue_style( FINALPOS_HANDLE_DASHBOARD );
		}
	}
}
