<?php
namespace Vidplex;

// If this file is called directly, abort.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class Admin
 *
 * Handles all admin-side functionality for the plugin.
 *
 * Responsible for registering menus, settings pages, meta boxes,
 * enqueueing admin scripts/styles, and managing plugin configuration
 * in the WordPress dashboard.
 */
class Admin {
	/**
	 * The single instance of the Admin class.
	 *
	 * This static variable holds the single instance of the class and ensures
	 * that only one instance of the class is created (Singleton pattern).
	 *
	 * @var Admin|null The single instance of the Admin class or null if not instantiated.
	 */
	private static $instance = null;

	/**
	 * The directory path for the admin folder.
	 *
	 * This variable stores the file path to the plugin's admin directory.
	 * It is used to load admin-related files from the plugin's directory.
	 *
	 * @var string The full path to the admin directory within the plugin.
	 */
	private static $dir = VIDPLEX_PLUGIN_DIR . 'admin/';

	/**
	 * The URL for the admin folder.
	 *
	 * This variable stores the URL to the plugin's admin directory.
	 * It is used to reference admin-related files that are publicly accessible on the web.
	 *
	 * @var string The URL to the admin directory within the plugin.
	 */
	private static $url = VIDPLEX_PLUGIN_URL . 'admin/';

	/**
	 * Retrieves the single instance of the Admin class.
	 *
	 * This method implements the Singleton pattern, ensuring that only
	 * one instance of the Admin class is created and used throughout
	 * the application. If the instance doesn't exist yet, it will be created.
	 *
	 * @return Admin The single instance of the Admin class.
	 */
	public static function get_instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Class constructor.
	 *
	 * This is the constructor for the class. It is automatically called when an instance of the class is created.
	 * As it stands, this constructor does not perform any specific actions, but it can be expanded to initialize
	 * properties, set up necessary configurations, or handle dependencies when needed.
	 *
	 * @access private
	 */
	private function __construct() {
		$this->load_admin_files();

		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) );
		add_action( 'admin_menu', array( $this, 'register_admin_page' ) );
	}

	/**
	 * Enqueue admin-specific styles and scripts for the Rootblox plugin.
	 *
	 * This function loads custom styles and JavaScript files used in the WordPress admin dashboard
	 * for the Rootblox plugin interface.
	 *
	 * @return void
	 */
	public function enqueue_admin_assets() {

		$current_screen = get_current_screen();

		// Load Notice Styles.
		wp_enqueue_style( 'vidplex--notice-style', self::$url . 'assets/css/admin-notice.css', array(), VIDPLEX_VERSION, false );

		if ( 'toplevel_page__vidplex' === $current_screen->id ) {
			// Load Admin Styles.
			wp_enqueue_style( 'vidplex--admin-style', self::$url . 'assets/css/admin-style.css', array(), VIDPLEX_VERSION, false );

			// Load Admin Script.
			wp_enqueue_script( 'vidplex--admin-script', self::$url . 'assets/js/admin-script.js', array( 'jquery' ), VIDPLEX_VERSION, false );

			wp_localize_script(
				'vidplex--admin-script',
				'scriptObj',
				array(
					'ajaxURL'            => admin_url( 'admin-ajax.php' ),
					'apiKeyNonce'        => wp_create_nonce( 'vidplex-api-key' ),
					'channelNonce'       => wp_create_nonce( 'vidplex-channel_ID' ),
					'channelVideosNonce' => wp_create_nonce( 'vidplex-channel-videos-list' ),
					'refreshNonce'       => wp_create_nonce( 'vidplex-refresh' ),
					'saveNonce'          => wp_create_nonce( 'vidplex-settings' ),
					'isPremium'          => vidplex_premium_access(),
					'advancedSettings'   => \Vidplex\Helpers::advanced_settings(),
				)
			);
		}
	}

	/**
	 * Register the Rootblox admin menu page and dashboard submenu.
	 *
	 * Adds a top-level menu item called "Rootblox" to the WordPress admin sidebar,
	 * along with a submenu item for the Dashboard. The admin page is restricted to users
	 * with the 'manage_options' capability.
	 *
	 * @return void
	 */
	public function register_admin_page() {
		if ( ! menu_page_url( '_vidplex' ) ) {
			add_menu_page( 'Vidplex', 'Vidplex', 'manage_options', '_vidplex', array( $this, 'admin_dashboard_render' ), self::$url . 'assets/img/vidplex_logo.png', 2 );
			add_submenu_page(
				'_vidplex',
				'Dashboard',
				__( 'Dashboard', 'vidplex' ),
				'manage_options',
				'_vidplex',
			);

		}
	}

	/**
	 * Render the content of the Rootblox dashboard admin page.
	 *
	 * Includes the dashboard.php template file, which contains the markup for the admin interface.
	 *
	 * @return void
	 */
	public function admin_dashboard_render() {
		include_once self::$dir . 'dashboard.php';
	}

	/**
	 * Loads admin-only files required for the plugin's backend functionality.
	 *
	 * This method includes admin-related PHP files such as admin notices,
	 * settings pages, or dashboard widgets.
	 *
	 * @access private
	 * @return void
	 */
	private function load_admin_files() {
		if ( is_admin() ) {
			// Require admin files here.
			require_once self::$dir . 'helpers/class-helpers.php';
		}
	}
}
