<?php
/**
 * Jet Smart Filters Admin class
 */

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

if ( ! class_exists( 'Jet_Smart_Filters_Admin' ) ) {
	/**
	 * Define Jet_Smart_Filters_Admin class
	 */
	class Jet_Smart_Filters_Admin {
		/**
		 * Components
		 */
		public $data;
		public $multilingual_support;

		/**
		 * Constructor for the class
		 */
		public function __construct() {
			// Init components
			require jet_smart_filters()->plugin_path( 'admin/includes/data.php' );
			add_action( 'init', function() {
				$this->data = new Jet_Smart_Filters_Admin_Data();
			}, 999 );

			// Init Setting Pages
			require_once jet_smart_filters()->plugin_path( 'admin/setting-pages/setting-pages.php' );
			new Jet_Smart_Filters_Admin_Setting_Pages();

			// Register plugin menu page
			add_action( 'admin_menu', array( $this, 'register_plugin_page' ) );

			if ( ! $this->is_filters_page() ) {
				return;
			}

			// Init Multilingual Support
			require_once jet_smart_filters()->plugin_path( 'admin/includes/multilingual-support.php' );
			$this->multilingual_support = new Jet_Smart_Filters_Admin_Multilingual_Support();

			// Register dynamic query data
			require_once jet_smart_filters()->plugin_path( 'admin/includes/dynamic-query/registration.php' );
			new Jet_Smart_Filters_Admin_Dynamic_Query_Registration();

			// Register default value data
			require_once jet_smart_filters()->plugin_path( 'admin/includes/dynamic-default-value/registration.php' );
			new Jet_Smart_Filters_Admin_Dynamic_Default_Value_Registration();

			// Register and enqueue admin assets
			add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) );

			// Localize admin app data
			add_action( 'admin_enqueue_scripts', array( $this, 'localize_data' ) );

			// Remove all notices
			add_action('in_admin_header', array( $this, 'remove_all_notices' ), 99);
		}

		/**
		 * Check if is filters page
		 */
		public function is_filters_page() {

			$page_request_val = jet_smart_filters()->data->get_request_var( 'page' );
			return ( $page_request_val && jet_smart_filters()->post_type->slug() === $page_request_val );
		}

		/**
		 * Register plugin menu page
		 */
		public function register_plugin_page() {
			/**
			 * Register Smart Filters page
			 */
			add_menu_page(
				esc_html__( 'Smart Filters', 'jet-smart-filters' ),
				esc_html__( 'Smart Filters', 'jet-smart-filters' ),
				'manage_options',
				jet_smart_filters()->post_type->slug(),
				array( $this, 'render_plugin_page' ),
				'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjQiIGhlaWdodD0iMjQiIHZpZXdCb3g9IjAgMCAyNCAyNCIgZmlsbD0iI2E3YWFhZCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48cGF0aCBmaWxsLXJ1bGU9ImV2ZW5vZGQiIGNsaXAtcnVsZT0iZXZlbm9kZCIgZD0iTTIwIDFINEMyLjM0MzE1IDEgMSAyLjM0MzE1IDEgNFYyMEMxIDIxLjY1NjkgMi4zNDMxNSAyMyA0IDIzSDIwQzIxLjY1NjkgMjMgMjMgMjEuNjU2OSAyMyAyMFY0QzIzIDIuMzQzMTUgMjEuNjU2OSAxIDIwIDFaTTQgMEMxLjc5MDg2IDAgMCAxLjc5MDg2IDAgNFYyMEMwIDIyLjIwOTEgMS43OTA4NiAyNCA0IDI0SDIwQzIyLjIwOTEgMjQgMjQgMjIuMjA5MSAyNCAyMFY0QzI0IDEuNzkwODYgMjIuMjA5MSAwIDIwIDBINFoiIGZpbGw9IiNhN2FhYWQiLz48cGF0aCBmaWxsLXJ1bGU9ImV2ZW5vZGQiIGNsaXAtcnVsZT0iZXZlbm9kZCIgZD0iTTIxLjYyOTMgNi4wMDA2NkMyMS45NDAyIDUuOTgxNDggMjIuMTE3NiA2LjM4NTc4IDIxLjkxMSA2LjY0Mjc3TDIwLjA3MjIgOC45MzAzNUMxOS44NTY5IDkuMTk4MjQgMTkuNDU1NiA5LjAyNjk4IDE5LjQ1OTggOC42NjlMMTkuNDcwOCA3Ljc0MDg0QzE5LjQ3MjIgNy42MTkyMyAxOS40MjE2IDcuNTAzOTggMTkuMzM0MyA3LjQyOTc1TDE4LjY2NzYgNi44NjMyMUMxOC40MTA1IDYuNjQ0NyAxOC41Mzc4IDYuMTkxMzQg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art_filters()->post_type->slug(),
				esc_html__( 'All Filters', 'jet-smart-filters' ),
				esc_html__( 'All Filters', 'jet-smart-filters' ),
				'manage_options',
				admin_url( 'admin.php' ) . '?page=' . jet_smart_filters()->post_type->slug() . '#/'
			);

			/**
			 * Add new filter submenu page
			 */
			add_submenu_page(
				jet_smart_filters()->post_type->slug(),
				esc_html__( 'Add New', 'jet-smart-filters' ),
				esc_html__( 'Add New', 'jet-smart-filters' ),
				'manage_options',
				admin_url( 'admin.php' ) . '?page=' . jet_smart_filters()->post_type->slug() . '#/new'
			);

			/**
			 * Settings submenu page
			 */
			add_submenu_page(
				jet_smart_filters()->post_type->slug(),
				esc_html__( 'Settings', 'jet-smart-filters' ),
				esc_html__( 'Settings', 'jet-smart-filters' ),
				'manage_options',
				add_query_arg(
					array(
						'page' => 'jet-dashboard-settings-page',
						'subpage' => 'jet-smart-filters-general-settings'
					),
					admin_url( 'admin.php' )
				)
			);

			// Remove Smart Filters link
			global $submenu;

			if ( isset( $submenu[jet_smart_filters()->post_type->slug()] ) ) {
				array_shift( $submenu[jet_smart_filters()->post_type->slug()] );
			}
		}

		/**
		 * Render plugin page callback
		 */
		public function render_plugin_page() {

			$app_template = jet_smart_filters()->plugin_path( 'admin/templates/admin-app.php' );

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

			include $app_template;
		}

		/**
		 * Enqueue admin assets only on gallery page
		 */
		public function enqueue_admin_assets() {

			wp_enqueue_media();

			wp_enqueue_script(
				'jet-smart-filters-admin-app',
				jet_smart_filters()->plugin_url( 'admin/assets/js/jsf-admin-app.js' ),
				array(),
				jet_smart_filters()->get_version(),
				true
			);
		}

		/**
		 * Localize data
		 */
		public function localize_data() {

			wp_localize_script( 'jet-smart-filters-admin-app', 'JetSmartFiltersAdminData',
				apply_filters( 'jet-smart-filters/admin/localized-data',
					array(
						'urls' => array(
							'admin'     => get_admin_url(),
							'ajaxurl'   => admin_url( 'admin-ajax.php' ),
							'endpoints' => jet_smart_filters()->rest_api->get_endpoints_urls(),
						),
						'nonce'           => wp_create_nonce( 'wp_rest' ),
						'plugin_settings' => $this->data->plugin_settings(),
						'filter_settings' => $this->data->settings(),
						'filter_types'    => $this->data->types(),
						'filter_sources'  => $this->data->sources(),
						'sort_by_list'    => $this->data->sort_by_list(),
						'help_block'      => $this->data->help_block_data()
					)
				)
			);
		}

		/**
		 * Remove all notices
		 */
		public function remove_all_notices() {

			remove_all_actions( 'user_admin_notices' );
			remove_all_actions( 'admin_notices' );
		}
	}
}
