<?php
/**
 * Resources class for Vidplex plugin.
 *
 * Handles enqueuing of scripts and styles for both frontend and editor.
 *
 * @package Vidplex
 */

namespace Vidplex;

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

/**
 * Class Resources
 *
 * Handles enqueuing of scripts and styles for the plugin.
 */
class Resources {
	/**
	 * The single instance of the Resources 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 Resources|null The single instance of the Resources class or null if not instantiated.
	 */
	private static $instance = null;

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

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

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

		return self::$instance;
	}

	/**
	 * Class constructor.
	 *
	 * This constructor is used to hook into WordPress actions for enqueuing block assets.
	 *
	 * This ensures that the necessary JavaScript and CSS files are loaded when editing blocks in the block editor
	 * and on the frontend for global assets.
	 *
	 * @access private
	 */
	private function __construct() {
		require_once VIDPLEX_PLUGIN_DIR . 'assets/fonts/google-fonts.php';
		add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_editor_assets' ) );
		add_action( 'enqueue_block_assets', array( $this, 'enqueue_frontend_assets' ) );
		add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_global_scripts' ) );
	}

	/**
	 * Enqueues global assets for custom blocks.
	 *
	 * This function is hooked into the `enqueue_block_assets` action to load global styles for the blocks.
	 * It enqueues the necessary CSS files that are applied both in the block editor and the frontend.
	 * These assets include editor styles, global block styles, and pattern-specific styles, ensuring
	 * consistent visual presentation across the editor and the website.
	 *
	 * @return void
	 */
	public function enqueue_frontend_assets() {

		// Enqueue Swiper.js for carousel functionality.
		wp_enqueue_script( 'vidplex-swiper-js', VIDPLEX_PLUGIN_URL . 'vendor/swiper/bundle.js', array(), VIDPLEX_VERSION, true );
		wp_enqueue_style( 'vidplex-swiper-css', VIDPLEX_PLUGIN_URL . 'vendor/swiper/bundle.css', array(), VIDPLEX_VERSION, 'all' );

		wp_enqueue_style( 'vidplex-global-styles', self::$url . 'css/vidplex-styles.css', array(), VIDPLEX_VERSION, 'all' );
		wp_enqueue_style( 'vidplex-block-styles', self::$url . 'css/block-styles.css', array(), VIDPLEX_VERSION, 'all' );

		wp_enqueue_script( 'vidplex-youtube-iframe-js', VIDPLEX_PLUGIN_URL . 'vendor/youtube/iframe.js', array(), VIDPLEX_VERSION, true );
		wp_enqueue_script( 'vidplex-frontend-script', self::$url . 'js/vidplex-scripts.js', array( 'jquery', 'vidplex-youtube-iframe-js' ), VIDPLEX_VERSION, true );
	}

	/**
	 * Enqueues editor-specific assets for the block editor.
	 *
	 * This function is hooked into the `enqueue_block_editor_assets` action. It should be used to enqueue
	 * JavaScript, CSS, or other assets that are only needed within the block editor environment.
	 * Currently, the function does not contain any specific implementation but can be extended to
	 * include necessary assets like block editor styles, scripts, or any other resources required
	 * for block editing.
	 *
	 * @return void
	 */
	public function enqueue_editor_assets() {
		wp_enqueue_style( 'vidplex-editor-style', self::$url . 'css/block-editor-styles.css', array(), VIDPLEX_VERSION, 'all' );
	}

	/**
	 * Enqueues global JavaScript and CSS assets.
	 *
	 * Loads the necessary scripts and styles for the plugin
	 * on the front-end or back-end as required.
	 *
	 * @return void
	 */
	public function enqueue_global_scripts() {
		// Enqueue global assets here.
	}
}
