<?php
namespace Jet_Engine_Divi_Views;

// Prevent direct access to the file.
if ( ! defined( 'WPINC' ) ) {
	die;
}

/**
 * Main plugin class.
 *
 * This class initializes the plugin, including its components like the
 * columns controller and the shortcode handler.
 */
class Plugin {

	/**
	 * Instance.
	 *
	 * Holds the plugin instance.
	 *
	 * @since 1.0.0
	 * @access public
	 * @static
	 *
	 * @var Plugin
	 */
	public static $instance = null;

	/**
	 * @var null
	 */
	public $components = null;

	/**
	 * @var
	 */
	public $framework;

	/**
	 * @var
	 */
	public $wizard;

	/**
	 * @var
	 */
	public $jet_dashboard;

	/**
	 * Instance.
	 *
	 * Ensures only one instance of the plugin class is loaded or can be loaded.
	 *
	 * @return Plugin An instance of the class.
	 * @since 1.0.0
	 * @access public
	 * @static
	 */
	public static function instance() {

		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Register autoloader.
	 */
	private function register_autoloader() {
		require JET_ENGINE_DIVI_VIEWS_PATH . 'includes/autoloader.php';
		Autoloader::run();
	}

	/**
	 * Constructor.
	 *
	 * The constructor is private to prevent creating multiple instances
	 * of the singleton.
	 */
	private function __construct() {
		$this->register_autoloader();

		require_once JET_ENGINE_DIVI_VIEWS_PATH . 'includes/utils.php';

		add_action( 'init', [ $this, 'init' ], -1 );
		add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_frontend_overrides' ], 100 );

		// Jet Dashboard Init
		add_action( 'init', array( $this, 'jet_dashboard_init' ), -999 );
		add_action( 'after_setup_theme', [ $this, 'framework_loader' ], -20 );

	}

	/**
	 * @return void
	 */
	public function init() {
		$this->components = new Components_Controller();
		new Modules_Controller();

		$this->wizard = new Wizard\Manager();
	}

	/**
	 * Enqueue local frontend overrides stylesheet.
	 *
	 * This file is intended for small manual CSS fixes that should apply on the
	 * frontend/Divi preview without rebuilding module bundles.
	 *
	 * @return void
	 */
	public function enqueue_frontend_overrides() {
		$path = JET_ENGINE_DIVI_VIEWS_PATH . 'assets/css/frontend-overrides.css';
		$url  = JET_ENGINE_DIVI_VIEWS_URL . 'assets/css/frontend-overrides.css';

		if ( ! file_exists( $path ) ) {
			return;
		}

		wp_enqueue_style(
			'jet-engine-divi-views-frontend-overrides',
			$url,
			[],
			filemtime( $path )
		);
	}

	/**
	 * @return void
	 */
	public function framework_loader() {
		require JET_ENGINE_DIVI_VIEWS_PATH . 'includes/modules/loader.php';

		$this->framework = new \Jet_Engine_Divi_Views_CX_Loader( [
			JET_ENGINE_DIVI_VIEWS_PATH . 'includes/modules/interface-builder/cherry-x-interface-builder.php',
			JET_ENGINE_DIVI_VIEWS_PATH . 'includes/modules/cherry-x-vue-ui/cherry-x-vue-ui.php',
			JET_ENGINE_DIVI_VIEWS_PATH . 'includes/modules/jet-dashboard/jet-dashboard.php',
		] );

	}

	/**
	 * Init the JetDashboard module
	 *
	 * @return void
	 */
	public function jet_dashboard_init( $force = false ) {

		if ( is_admin() || $force ) {
			$jet_dashboard_module_data = $this->framework->get_included_module_data( 'jet-dashboard.php' );
			$this->jet_dashboard = \Jet_Dashboard\Dashboard::get_instance();

			$this->jet_dashboard->init( [
				'path'           => $jet_dashboard_module_data['path'],
				'url'            => $jet_dashboard_module_data['url'],
				'cx_ui_instance' => [ $this, 'jet_dashboard_ui_instance_init' ],
				'plugin_data'    => [
					'slug'    => 'jet-engine-divi-views',
					'file'    => 'jet-engine-divi-views/jet-engine-divi-views.php',
					'version' => JET_ENGINE_DIVI_VIEWS_VERSION,
				],
			] );
		}
	}

    /**
     * [jet_dashboard_ui_instance_init description]
     * @return [type] [description]
     */
	    public function jet_dashboard_ui_instance_init() {
	        $cx_ui_module_data = $this->framework->get_included_module_data( 'cherry-x-vue-ui.php' );

	        return new \CX_Vue_UI( $cx_ui_module_data );
	    }
}

// Initialize the plugin instance.
Plugin::instance();
