<?php
/**
 * Asset Manager Class
 *
 * @package ResponsiveMobileSelect\Core
 * @since 2.0.0
 */

namespace ResponsiveMobileSelect\Core;

// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Asset management class.
 *
 * @since 2.0.0
 */
class Asset_Manager {

	/**
	 * Plugin version.
	 *
	 * @since 2.0.0
	 * @var string
	 */
	private $version;

	/**
	 * Constructor.
	 *
	 * @since 2.0.0
	 */
	public function __construct() {
		$this->version = RESPONSIVE_MOBILE_SELECT_VERSION;
	}

	/**
	 * Enqueue frontend assets.
	 *
	 * @since 2.0.0
	 */
	public function enqueue_frontend_assets() {
		// Enqueue CSS.
		wp_enqueue_style(
			'responsive-mobile-select-menu',
			RESPONSIVE_MOBILE_SELECT_PLUGIN_URL . 'assets/css/responsive-menu.css',
			array(),
			$this->version,
			'all'
		);

		// Enqueue JavaScript.
		wp_enqueue_script(
			'responsive-mobile-select-menu',
			RESPONSIVE_MOBILE_SELECT_PLUGIN_URL . 'assets/js/responsive-menu.js',
			array( 'jquery' ),
			$this->version,
			true
		);

		// Localize script for AJAX and translations.
		wp_localize_script(
			'responsive-mobile-select-menu',
			'responsiveMobileSelect',
			array(
				'ajaxUrl' => admin_url( 'admin-ajax.php' ),
				'nonce'   => wp_create_nonce( 'responsive_mobile_select_nonce' ),
				'i18n'    => array(
					'selectPlaceholder' => __( 'Select a page...', 'responsive-mobile-select-menu' ),
				),
			)
		);
	}

	/**
	 * Enqueue admin assets.
	 *
	 * @since 2.0.0
	 * @param string $hook_suffix Current admin page.
	 */
	public function enqueue_admin_assets( $hook_suffix ) {
		// Only enqueue on our admin page.
		if ( 'appearance_page_responsive-menu' !== $hook_suffix ) {
			return;
		}

		// Enqueue admin CSS.
		wp_enqueue_style(
			'responsive-mobile-select-menu-admin',
			RESPONSIVE_MOBILE_SELECT_PLUGIN_URL . 'admin/css/admin.css',
			array(),
			$this->version,
			'all'
		);

		// Enqueue admin JavaScript.
		wp_enqueue_script(
			'responsive-mobile-select-menu-admin',
			RESPONSIVE_MOBILE_SELECT_PLUGIN_URL . 'admin/js/admin.js',
			array( 'jquery' ),
			$this->version,
			true
		);

		// Localize admin script.
		wp_localize_script(
			'responsive-mobile-select-menu-admin',
			'responsiveMobileSelectAdmin',
			array(
				'ajaxUrl' => admin_url( 'admin-ajax.php' ),
				'nonce'   => wp_create_nonce( 'responsive_mobile_select_admin_nonce' ),
				'i18n'    => array(
									'saveSuccess' => __( 'Settings saved successfully!', 'responsive-mobile-select-menu' ),
				'saveError'   => __( 'Error saving settings.', 'responsive-mobile-select-menu' ),
				'confirmReset' => __( 'Are you sure you want to reset all settings to defaults?', 'responsive-mobile-select-menu' ),
				),
			)
		);
	}

	/**
	 * Register block editor assets.
	 *
	 * @since 2.0.0
	 */
	public function register_block_assets() {
		// Register block styles.
		wp_register_style(
			'responsive-mobile-select-menu-blocks',
			RESPONSIVE_MOBILE_SELECT_PLUGIN_URL . 'assets/css/blocks.css',
			array(),
			$this->version
		);

		// Register block script.
		wp_register_script(
			'responsive-mobile-select-menu-blocks',
			RESPONSIVE_MOBILE_SELECT_PLUGIN_URL . 'assets/js/blocks.js',
			array( 'wp-blocks', 'wp-element', 'wp-editor', 'wp-components' ),
			$this->version,
			true
		);
	}

	/**
	 * Get asset URL.
	 *
	 * @since 2.0.0
	 * @param string $path Asset path relative to plugin directory.
	 * @return string Asset URL.
	 */
	public function get_asset_url( $path ) {
		return RESPONSIVE_MOBILE_SELECT_PLUGIN_URL . $path;
	}

	/**
	 * Get asset path.
	 *
	 * @since 2.0.0
	 * @param string $path Asset path relative to plugin directory.
	 * @return string Asset file path.
	 */
	public function get_asset_path( $path ) {
		return RESPONSIVE_MOBILE_SELECT_PLUGIN_DIR . $path;
	}

	/**
	 * Check if asset exists.
	 *
	 * @since 2.0.0
	 * @param string $path Asset path relative to plugin directory.
	 * @return bool Whether asset exists.
	 */
	public function asset_exists( $path ) {
		return file_exists( $this->get_asset_path( $path ) );
	}
}
