<?php

namespace VertexMediaLLC\ZyreElementorAddons;

use Elementor\Controls_Manager;
use Elementor\Elements_Manager;

defined( 'ABSPATH' ) || die();

/**
 * Class Plugin
 *
 * Main Plugin class
 *
 * @since 1.0.0
 */
class Plugin {
	/**
	 * Instance
	 *
	 * @since 1.0.0
	 * @access private
	 * @static
	 *
	 * @var Plugin The single instance of the class.
	 */
	private static $instance = null;

	/**
	 * Instance
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @var Client The instance of the class.
	 */
	public $appsero = null;

	/**
	 * Instance
	 *
	 * Ensures only one instance of the class is loaded or can be loaded.
	 *
	 * @return Plugin An instance of the class.
	 * @since 1.0.0
	 * @access public
	 */
	public static function instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Include Files
	 * Register custom category in Elementor
	 * Register custom controls in Elementor
	 *
	 * @since 1.0.0
	 * @access public
	 */
	public function init() {
		add_action( 'init', array( $this, 'include_files' ) );

		// Register custom category.
		add_action( 'elementor/elements/categories_registered', array( $this, 'add_category' ) );

		// Register custom controls.
		add_action( 'elementor/controls/controls_registered', array( $this, 'register_controls' ) );

		add_action( 'wp_ajax_zyreladdons_mailchimp_ajax', [ Ajax_Handler::class, 'mailchimp_prepare_ajax' ] );
		add_action( 'wp_ajax_nopriv_zyreladdons_mailchimp_ajax', [ Ajax_Handler::class, 'mailchimp_prepare_ajax' ] );

		$this->appsero_tracking_init();

		do_action( 'zyreladdons_loaded' );
	}

	/**
	 * Initialize the appsero tracker
	 *
	 * @return void
	 */
	protected function appsero_tracking_init() {
		if ( ! class_exists( 'Appsero\Client' ) ) {
			include_once ZYRELADDONS_DIR_PATH . 'vendor/appsero/client/src/Client.php';
		}

		$this->appsero = new \Appsero\Client(
			'acb2abb0-6399-44b7-a9d9-5a2bc0a70682',
			'Zyre Elementor Addons',
			ZYRELADDONS__FILE__
		);

		$this->appsero->set_textdomain( 'zyre-elementor-addons' );

		// Active insights
		$this->appsero->insights()
			->add_plugin_data()
			->init();
	}

	/**
	 * Include all necessary files
	 *
	 * @since 1.0.0
	 * @access public
	 */
	public function include_files() {
		include_once ZYRELADDONS_DIR_PATH . 'includes/functions.php';
		include_once ZYRELADDONS_DIR_PATH . 'includes/filterable-functions.php';

		include_once ZYRELADDONS_DIR_PATH . 'classes/icons-manager.php';
		include_once ZYRELADDONS_DIR_PATH . 'classes/widgets-manager.php';
		include_once ZYRELADDONS_DIR_PATH . 'classes/assets-manager.php';
		include_once ZYRELADDONS_DIR_PATH . 'classes/cache-manager.php';
		include_once ZYRELADDONS_DIR_PATH . 'classes/widgets-cache.php';
		include_once ZYRELADDONS_DIR_PATH . 'classes/assets-cache.php';
		include_once ZYRELADDONS_DIR_PATH . 'classes/query-manager.php';

		if ( ! in_array( 'search-box', Widgets_Manager::get_inactive_widgets(), true ) ) {
			include_once ZYRELADDONS_DIR_PATH . 'classes/search-handler.php';
		}

		if ( is_admin() ) {
			// select2
			include_once ZYRELADDONS_DIR_PATH . 'classes/select2-handler.php';
			add_action( 'wp_ajax_zyreladdons_process_dynamic_select', [ Select2_Handler::class, 'process_select_request' ] );
			add_action( 'wp_ajax_zyreladdons_process_el_dynamic_select', [ Select2_Handler::class, 'process_el_select_request' ] );

			// Dashboard
			include_once ZYRELADDONS_DIR_PATH . 'classes/admin-dashboard.php';
			add_action( 'admin_menu', [ Dashboard::class, 'add_menu' ], 21 );
			add_action( 'admin_menu', [ Dashboard::class, 'update_menu_items' ], 99 );
			add_filter( 'submenu_file', [ Dashboard::class, 'update_submenu_file' ] );
			add_action( 'admin_enqueue_scripts', [ Dashboard::class, 'enqueue_scripts' ] );
			add_action( 'admin_enqueue_scripts', [ Dashboard::class, 'dequeue_scripts' ], 100 );

			add_action( 'wp_ajax_' . Dashboard::DASHBOARD_NONCE, [ Dashboard::class, 'save_settings' ] );
			add_action( 'zyreladdons_save_dashboard_settings', [ Dashboard::class, 'save_widgets' ], 1 );
			add_action( 'zyreladdons_save_dashboard_settings', [ Dashboard::class, 'save_widgets_styles' ] );
			add_action( 'zyreladdons_save_dashboard_settings', [ Dashboard::class, 'save_credentials' ] );

			add_action( 'in_admin_header', [ Dashboard::class, 'remove_admin_notices' ], PHP_INT_MAX );

			add_filter( 'admin_body_class', [ Dashboard::class, 'add_body_class' ] );

			// Include the Credentials_Manager class if it doesn't exist.
			if ( ! class_exists( 'VertexMediaLLC\ZyreElementorAddons\Credentials_Manager' ) ) {
				include_once ZYRELADDONS_DIR_PATH . 'classes/credentials-manager.php';
			}
		}

		if ( is_user_logged_in() ) {
			include_once ZYRELADDONS_DIR_PATH . 'classes/library-manager.php';
			include_once ZYRELADDONS_DIR_PATH . 'classes/library-source.php';

			include_once ZYRELADDONS_DIR_PATH . 'classes/prestyles-manager.php';
		}

		include_once ZYRELADDONS_DIR_PATH . 'classes/ajax-handler.php';

		// Theme Builder
		include_once ZYRELADDONS_DIR_PATH . 'modules/theme-builder/module.php';
		Modules\ThemeBuilder\Module::instance();

		// Mega Menu
		include_once ZYRELADDONS_DIR_PATH . 'modules/mega-menu/module.php';
		Modules\Mega_Menu\Module::instance();
	}

	/**
	 * Add Custom Elementor Categories
	 *
	 * @param Elements_Manager $elements_manager Category Names Array.
	 *
	 * @since 1.0.0
	 * @access public
	 */
	public function add_category( Elements_Manager $elements_manager ) {
		$elements_manager->add_category(
			'zyreladdons_category',
			array(
				'title' => __( 'Zyre Addons', 'zyre-elementor-addons' ),
				'icon'  => 'fa fa-plug',
			)
		);
	}

	/**
	 * Register new Elementor Controls
	 *
	 * @since 1.0.0
	 * @param Controls_Manager $controls_manager Register Controls.
	 * @access public
	 */
	public function register_controls( Controls_Manager $controls_manager ) {
		include_once ZYRELADDONS_DIR_PATH . 'controls/select2.php';
		include_once ZYRELADDONS_DIR_PATH . 'controls/style-select.php';
		include_once ZYRELADDONS_DIR_PATH . 'controls/typography.php';

		$select = __NAMESPACE__ . '\Controls\Select2';
		zyreladdons_elementor()->controls_manager->register( new $select() );

		$select = __NAMESPACE__ . '\Controls\Style_Control';
		zyreladdons_elementor()->controls_manager->register( new $select() );

		$typography = __NAMESPACE__ . '\Controls\Group_Control_Typography_Extended';
		zyreladdons_elementor()->controls_manager->add_group_control(
			$typography::get_type(),
			new $typography()
		);
	}
}
