<?php
/**
 * The admin-specific functionality of the plugin.
 *
 *
 * @package    FENSE_BPVT
 * @subpackage FENSE_BPVT/admin
 * @author     Sanad Nabil <sanad@devozon.com>
 */

if ( !defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

class FENSE_BPVT_Admin
{
	
	public FENSE_BPVT_ADM $Fense_FUN;
	/**
	 * The ID of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string $plugin_name The ID of this plugin.
	 */
	private string $plugin_name;
	/**
	 * The version of this plugin.
	 *
	 * @since    1.0.0
	 * @access   private
	 * @var      string $version The current version of this plugin.
	 */
	private string $version;
	
	/**
	 * Initialize the class and set its properties.
	 *
	 * @param string $plugin_name The name of this plugin.
	 * @param string $version The version of this plugin.
	 * @since    1.0.0
	 */
	public function __construct( string $plugin_name, string $version )
	{
		
		$this->plugin_name = $plugin_name;
		$this->version = $version;
		
		add_action( 'admin_menu', [ $this, 'addPluginAdminMenu' ], 9 );
		add_action( 'admin_init', [ $this, 'registerAndBuildFields' ] );
		
		$this->Fense_FUN = ( new FENSE_BPVT_ADM );
	}
	
	public static function FENSE_BPVT_render_settings_field( $args ): void
	{
		$Fense_FUN = ( new FENSE_BPVT_ADM );
		
		switch ( $args['type'] ) {
			
			case 'input':
				$Fense_FUN->FORM_input( $args );
				break;
			case 'select':
				
				$Fense_FUN->FORM_select( $args );
				break;
			case 'multi_select':
				
				$Fense_FUN->FORM_multi_select( $args );
				break;
			case 'textarea':
				
				$Fense_FUN->FORM_text_area( $args );
				break;
			case 'multi_input':
				
				$Fense_FUN->FORM_multi_input( $args );
				break;
			case 'ad_code':
				$Fense_FUN->FORM_ad_code( $args );
				break;
			case 'ip_ranges':
				$Fense_FUN->FORM_ip_ranges( $args );
				break;
			case 'pages_select':
				$Fense_FUN->FORM_pages_select( $args );
				break;
			case 'radio':
				$Fense_FUN->FORM_radio( $args );
				break;
			default:
				# code...
				break;
		}
		
	}
	
	/**
	 * Register the stylesheets for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_styles( $hook_suffix )
	{
		
		if ( !strpos( $hook_suffix, 'fense-bpvt' ) !== false ) {
			return;
		}
		
		if ( is_rtl() ) {
			wp_enqueue_style( 'tgbtd', plugin_dir_url( __FILE__ ) . 'theme/css/bootstrap-rtl.min.css', [], $this->version, 'all' );
			wp_enqueue_style( 'tyueq', plugin_dir_url( __FILE__ ) . 'theme/css/app-rtl.min.css', [], $this->version, 'all' );
		} else {
			wp_enqueue_style( 'tgbtd', plugin_dir_url( __FILE__ ) . 'theme/css/bootstrap.min.css', [], $this->version, 'all' );
			wp_enqueue_style( 'tyueq', plugin_dir_url( __FILE__ ) . 'theme/css/app.min.css', [], $this->version, 'all' );
		}
		
		wp_enqueue_style( 'dasfgf', plugin_dir_url( __FILE__ ) . 'theme/css/icons.min.css', [], $this->version, 'all' );
		
		wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/fense-bpvt-admin.css', [], $this->version, 'all' );
		//wp_enqueue_style( 'fense-bpvt-mdb', plugin_dir_url( __FILE__ ) . 'css/fense-bpvt-mdb.css', array(), $this->version, 'all' );
		//wp_enqueue_style( 'fense-bpvt-fa', plugin_dir_url( __FILE__ ) . 'css/fontawesome/css/all.min.css', array(), $this->version, 'all' );
		wp_enqueue_style( 'fense-bpvt-tiop', plugin_dir_url( __FILE__ ) . 'css/bootstrap-tagsinput.css', [], $this->version, 'all' );
		
	}
	
	/**
	 * Register the JavaScript for the admin area.
	 *
	 * @since    1.0.0
	 */
	public function enqueue_scripts( $hook_suffix )
	{
		
		if ( !strpos( $hook_suffix, 'fense-bpvt' ) !== false ) {
			return;
		}
		
		wp_enqueue_script( $this->plugin_name . '-options-page', plugin_dir_url( __FILE__ ) . 'js/fense-bpvt-options.js', [ 'jquery' ], $this->version, false );
		wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/fense-bpvt-admin.js', [ 'jquery' ], $this->version, false );
		wp_enqueue_script( $this->plugin_name . '-bs', plugin_dir_url( __FILE__ ) . 'js/bootstrap.min.js', [ 'jquery' ], $this->version, false );
		wp_enqueue_script( "jquery" );
		wp_enqueue_script( $this->plugin_name . '-mskiop', plugin_dir_url( __FILE__ ) . 'js/ms.js', [ 'jquery' ], $this->version, false );
		wp_enqueue_script( $this->plugin_name . '-tgchart', plugin_dir_url( __FILE__ ) . 'js/chart.js', [ 'jquery' ], $this->version, false );
		wp_enqueue_script( $this->plugin_name . '-tgtit', plugin_dir_url( __FILE__ ) . 'js/bootstrap-tagsinput.min.js', [ 'jquery' ], $this->version, false );
		
		wp_enqueue_script( $this->plugin_name . '-apex-chart', plugin_dir_url( __FILE__ ) . 'js/apex-chart.js', [ 'jquery' ], $this->version, false );
		
	}
	
	public function addPluginAdminMenu()
	{
		
		$plug_name = 'Fense';
		//add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position );
		add_menu_page( $plug_name, esc_html__( 'Fense Options', 'fense-block-vpn-proxy' ), 'administrator', $this->plugin_name,
			[ $this, 'displayPluginAdminDashboard' ], 'dashicons-shield', 100 );
		
		//add_submenu_page( '$parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function );
		add_submenu_page( $this->plugin_name, 'Statistics', esc_html__( 'Statistics', 'fense-block-vpn-proxy' ), 'administrator', $this->plugin_name . '-statistics',
			[ $this, 'displayPluginAdminSettings' ] );
	}
	
	public function displayPluginAdminDashboard()
	{
		require_once 'partials/options.php';
	}
	
	public function displayPluginAdminSettings()
	{
		
		$tab = isset( $_GET['tab'] ) ? sanitize_text_field( $_GET['tab'] ) : '';

// set this var to be used in the settings-display view
		$active_tab = $tab ?: 'general';
		
		if ( isset( $_GET['error_message'] ) ) {
			add_action( 'admin_notices', [ $this, 'settingsPageSettingsMessages' ] );
			do_action( 'admin_notices', esc_attr( sanitize_text_field( $_GET['error_message'] ) ) );
		}
		require_once 'partials/statistics.php';
	}
	
	public function settingsPageSettingsMessages( $error_message )
	{
		switch ( $error_message ) {
			case '1':
				$message = esc_html__( 'There was an error adding this setting. Please try again.  If this persists, shoot us an email.', 'fense-block-vpn-proxy' );
				$err_code = esc_attr( 'FENSE_BPVT_example_setting' );
				$setting_field = 'FENSE_BPVT_example_setting';
				break;
		}
		$type = 'error';
		add_settings_error(
			$setting_field,
			$err_code,
			$message,
			$type
		);
	}
	
	public function registerAndBuildFields()
	{
		/**
		 * First, we add_settings_section. This is necessary since all future settings must belong to one.
		 * Second, add_settings_field
		 * Third, register_setting
		 */
		add_settings_section(
		// ID used to identify this section and with which to register options
			'FENSE_BPVT_general_section',
			// Title to be displayed on the administration page
			'',
			// Callback used to render the description of the section
			[ $this, 'FENSE_BPVT_display_general_account' ],
			// Page on which to add this section of options
			'FENSE_BPVT_tab_1'
		);
		
		$this->Fense_FUN->add_option( __( 'Enable Plugin', 'fense-block-vpn-proxy' ), 'enable_dtg', 'input', 'checkbox' );
		
		$this->Fense_FUN->add_option(
			esc_html__( 'Api Key', 'fense-block-vpn-proxy' ), 'licence_key', 'input', 'text', '', 'tab_1',
			esc_html__( 'You can always get a free key from ', 'fense-block-vpn-proxy' )
			. '<a target="_blank" class="text-danger" href="' . esc_url( FENSE_BPVT_PLUGIN::SITE ) . '">' . esc_html__( 'Fense', 'fense-block-vpn-proxy' ) . '</a>'
		);
		
		$data = [
			esc_html__( 'All Site', 'fense-block-vpn-proxy' ) => 'all',
			esc_html__( 'Custom', 'fense-block-vpn-proxy' ) => 'custom',
		];
		
		$this->Fense_FUN->add_option( __( 'Enable on', 'fense-block-vpn-proxy' ), 'enable_on_custom', 'select', 'checkbox', $data );
		
		$this->Fense_FUN->add_option( __( 'Enable on logged in Users', 'fense-block-vpn-proxy' ), 'enable_on_logged_users', 'input', 'checkbox' );
		
		$this->Fense_FUN->add_option( __( 'Enable on Posts', 'fense-block-vpn-proxy' ), 'enable_on_posts', 'input', 'checkbox' );
		$this->Fense_FUN->add_option( __( 'Enable on Pages', 'fense-block-vpn-proxy' ), 'enable_on_pages', 'input', 'checkbox' );
		
		$this->Fense_FUN->add_option( __( 'Enable on Login Page', 'fense-block-vpn-proxy' ), 'enable_on_login', 'input', 'checkbox' );
		
		$data = [
			esc_html__( 'For 1 Hour', 'fense-block-vpn-proxy' ) => 1,
			esc_html__( 'For 6 Hours', 'fense-block-vpn-proxy' ) => 6,
			esc_html__( 'For 12 Hours', 'fense-block-vpn-proxy' ) => 12,
			esc_html__( 'For 1 Day', 'fense-block-vpn-proxy' ) => 24,
			esc_html__( 'No', 'fense-block-vpn-proxy' ) => 0,
		];
		
		$this->Fense_FUN->add_option( __( 'Enable cache', 'fense-block-vpn-proxy' ), 'enable_cache', 'select', 'checkbox', $data, 'tab_1', esc_html__( 'You can enable cache to lower the usage of Fense api requests', 'fense-block-vpn-proxy' ) );
		
		$this->Fense_FUN->add_option( __( 'Enable Safe Mode', 'fense-block-vpn-proxy' ), 'cache_safe_mode', 'input', 'checkbox', null, 'tab_1', __( 'Enable this option only if you are using a WordPress caching plugin or a server-side caching system (e.g., Redis, Varnish, etc.) that caches website content.', 'fense-block-vpn-proxy' ) . '<br><br>' . __( 'When Safe Mode is active, the plugin operates after the content has loaded, ensuring full compatibility with any caching system.', 'fense-block-vpn-proxy' ) . '<br><br>' . __( 'Note: Safe Mode supports only one protection mode — Hide Content and Display a Message.', 'fense-block-vpn-proxy' ) );
		
		
		$data = [
			esc_html__( 'Yes', 'fense-block-vpn-proxy' ) => 'yes',
			esc_html__( 'No', 'fense-block-vpn-proxy' ) => 'no',
		];
		
		$this->Fense_FUN->add_option( __( 'Show Link', 'fense-block-vpn-proxy' ), 'show_link', 'select', 'checkbox', $data, 'tab_1' );
		
		$this->Fense_FUN->add_option(
			esc_html__( 'Link', 'fense-block-vpn-proxy' ), 'show_link_val', 'input', 'text', '', 'tab_1' );
		
		add_settings_section(
		// ID used to identify this section and with which to register options
			'FENSE_BPVT_general_section',
			// Title to be displayed on the administration page
			'',
			// Callback used to render the description of the section
			[ $this, 'FENSE_BPVT_display_general_account' ],
			// Page on which to add this section of options
			'FENSE_BPVT_tab_2'
		);
		unset( $args );
		
		$data = [
			esc_html__( 'Hide content and Display a Message', 'fense-block-vpn-proxy' ) => 'message',
			esc_html__( 'Redirect To Url', 'fense-block-vpn-proxy' ) => 'redirect',
			esc_html__( 'Hide Ads and Show content', 'fense-block-vpn-proxy' ) => 'show_content',
		];
		
		$this->Fense_FUN->add_option( __( 'Protection Mode', 'fense-block-vpn-proxy' ), 'protection_mode', 'select', 'checkbox', $data, 'tab_2' );
		
		$this->Fense_FUN->add_option( __( 'Message', 'fense-block-vpn-proxy' ), 'restrict_message', 'textarea', 'checkbox', '', 'tab_2' );
		
		$this->Fense_FUN->add_option( __( 'URL', 'fense-block-vpn-proxy' ), 'redirect_to_url', 'input', 'text', '', 'tab_2' );
		
		$this->Fense_FUN->add_option( __( 'Ad Code', 'fense-block-vpn-proxy' ), 'ad_code', 'ad_code', 'checkbox', '', 'tab_2',
			esc_html__( 'In order to make this option fully Works, please follow all steps from our', 'fense-block-vpn-proxy' )
			. '<a target="_blank" href="' . esc_url( FENSE_BPVT_PLUGIN::SITE ) . 'wordpress">' . esc_html__( ' Docs', 'fense-block-vpn-proxy' ) . '</a>'
		);
		
		$data = $this->Fense_FUN->country_list();
		
		$this->Fense_FUN->add_option( __( 'Disable on certain countries', 'fense-block-vpn-proxy' ), 'disable_country', 'multi_select', 'checkbox', $data, 'tab_2', esc_html__( 'With this option, You can disable Fense protection on specified countries.', 'fense-block-vpn-proxy' ) );
		
		$this->Fense_FUN->add_option( __( 'Enable on certain countries', 'fense-block-vpn-proxy' ), 'enable_country', 'multi_select', 'checkbox', $data, 'tab_2', esc_html__( 'With this option, you can only enable Fense protection for specific countries.', 'fense-block-vpn-proxy' ) );
		
		$this->Fense_FUN->add_option( __( 'Block Referrer Domains', 'fense-block-vpn-proxy' ), 'restrict_domain', 'multi_input', 'checkbox', '', 'tab_2', esc_html__( 'You can use this option to prevent specified Referrer Domains from accessing your website.', 'fense-block-vpn-proxy' ) );

//		$this->Fense_FUN->add_option(
//			esc_html__( 'Detect Vpn', 'fense-block-vpn-proxy' ), 'block_vpn', 'input', 'checkbox', '', 'tab_2' );
//
//		$this->Fense_FUN->add_option(
//			esc_html__( 'Detect Proxy', 'fense-block-vpn-proxy' ), 'block_proxy', 'input', 'checkbox', '', 'tab_2' );
//
//		$this->Fense_FUN->add_option(
//			esc_html__( 'Detect Tor', 'fense-block-vpn-proxy' ), 'block_vps', 'input', 'checkbox', '', 'tab_2' );
		
		$data = [
			esc_html__( 'Enable', 'fense-block-vpn-proxy' ) => 'yes',
			esc_html__( 'Disable', 'fense-block-vpn-proxy' ) => 'no',
		];
		
		$this->Fense_FUN->add_option(
			esc_html__( 'Enable crawlers Bots', 'fense-block-vpn-proxy' ), 'crawlers_bots', 'select', 'checkbox', $data, 'tab_2' );
		
		$this->Fense_FUN->add_option( __( 'Crawler Names', 'fense-block-vpn-proxy' ), 'crawlers_bots_data', 'multi_input', 'checkbox', '', 'tab_2', esc_html__( 'You can use this option to add Names for crawlers so requests from known crawlers always get through.', 'fense-block-vpn-proxy' ) . '<br><br>' . '<a target="_blank" href="https://raw.githubusercontent.com/monperrus/crawler-user-agents/master/crawler-user-agents.json">' . esc_html__( ' Check out All Crawler Names', 'fense-block-vpn-proxy' ) . '</a>' . esc_html__( ' , You can also add a keyword like bot,Bots and Bots Like Googlebot,facebot,fensebot,otherbot Still always get through Because they have bot keyword', 'fense-block-vpn-proxy' ) );
		
		
		add_settings_section(
		// ID used to identify this section and with which to register options
			'FENSE_BPVT_general_section',
			// Title to be displayed on the administration page
			'',
			// Callback used to render the description of the section
			[ $this, 'FENSE_BPVT_display_general_account' ],
			// Page on which to add this section of options
			'FENSE_BPVT_tab_4'
		);
		
		
		$data = $this->Fense_FUN->country_list();
		
		$this->Fense_FUN->add_option( __( 'Block specific Countries', 'fense-block-vpn-proxy' ), 'restrict_disable_country', 'multi_select', 'checkbox', $data, 'tab_4' );
		
		$this->Fense_FUN->add_option( __( 'Enable specific Countries', 'fense-block-vpn-proxy' ), 'restrict_enable_country', 'multi_select', 'checkbox', $data, 'tab_4' );
		
		add_settings_section(
		// ID used to identify this section and with which to register options
			'FENSE_BPVT_general_section',
			// Title to be displayed on the administration page
			'',
			// Callback used to render the description of the section
			[ $this, 'FENSE_BPVT_display_general_account' ],
			// Page on which to add this section of options
			'FENSE_BPVT_tab_5'
		);
		
		$this->Fense_FUN->add_option( __( 'data', 'fense-block-vpn-proxy' ), 'api_data', 'multi_select', 'input', '', 'tab_5' );
		$this->Fense_FUN->add_option( __( 'time', 'fense-block-vpn-proxy' ), 'api_data_time', 'multi_select', 'input', '', 'tab_5' );
		
		$this->Fense_FUN->add_option( __( 'version', 'fense-block-vpn-proxy' ), 'tg_wp_version', 'multi_select', 'input', '', 'tab_5' );
		$this->Fense_FUN->add_option( __( 'version time', 'fense-block-vpn-proxy' ), 'tg_wp_version_time', 'multi_select', 'input', '', 'tab_5' );
		
		$this->Fense_FUN->add_option( __( 'ip', 'fense-block-vpn-proxy' ), 'current_ip_data', 'multi_select', 'input', '', 'tab_5' );
		
		
		add_settings_section(
		// ID used to identify this section and with which to register options
			'FENSE_BPVT_general_section',
			// Title to be displayed on the administration page
			'',
			// Callback used to render the description of the section
			[ $this, 'FENSE_BPVT_display_general_account' ],
			// Page on which to add this section of options
			'FENSE_BPVT_tab_6'
		);
		
		$this->Fense_FUN->add_option( __( 'Enable IP Whitelisting', 'fense-block-vpn-proxy' ), 'ip_whitelisting_enable', 'input', 'checkbox', '', 'tab_6' );
		
		$this->Fense_FUN->add_option( __( 'IP Whitelisting', 'fense-block-vpn-proxy' ), 'ip_whitelisting', 'multi_input', 'checkbox', '', 'tab_6' );
		
		$this->Fense_FUN->add_option( __( 'IP Whitelisting Range', 'fense-block-vpn-proxy' ), 'ip_whitelisting_range', 'ip_ranges', 'checkbox', '', 'tab_6' );
		
		$this->Fense_FUN->add_option( __( 'Enable IP Blacklisting', 'fense-block-vpn-proxy' ), 'ip_blacklisting_enable', 'input', 'checkbox', '', 'tab_6' );
		
		$this->Fense_FUN->add_option( __( 'IP Blacklisting', 'fense-block-vpn-proxy' ), 'ip_blacklisting', 'multi_input', 'checkbox', '', 'tab_6' );
		
		$this->Fense_FUN->add_option( __( 'IP Blacklisting Range', 'fense-block-vpn-proxy' ), 'ip_blacklisting_range', 'ip_ranges', 'checkbox', '', 'tab_6' );
		
		add_settings_section(
			'FENSE_BPVT_general_section',
			'',
			[ $this, 'FENSE_BPVT_display_general_account' ],
			'FENSE_BPVT_tab_3'
		);
		
		$data = [
			esc_html__( 'Blacklist mode - run everywhere except listed', 'fense-block-vpn-proxy' ) => 'exclude',
			esc_html__( 'Whitelist mode - run only on listed', 'fense-block-vpn-proxy' ) => 'include',
		];
		$this->Fense_FUN->add_option(
			__( 'URL Mode', 'fense-block-vpn-proxy' ),
			'url_mode', 'select', 'select', $data, 'tab_3',
			__( 'Choose whether to run detection everywhere except listed pages/prefixes (Exclude), or only on listed pages/prefixes (Include).', 'fense-block-vpn-proxy' )
		);
		
		$this->Fense_FUN->add_option(
			__( 'Specific Pages', 'fense-block-vpn-proxy' ),
			'specific_pages', 'pages_select', 'checkbox', '', 'tab_3',
			__( 'Select individual published pages or posts where the URL Mode applies.', 'fense-block-vpn-proxy' )
		);
		
		$this->Fense_FUN->add_option(
			__( 'URL Prefixes', 'fense-block-vpn-proxy' ),
			'url_prefixes', 'multi_input', 'checkbox', '', 'tab_3',
			__( 'Comma-separated URL path prefixes (e.g. shop, blog/category). A prefix of "shop" matches /shop, /shop/product, etc.', 'fense-block-vpn-proxy' )
		);
		
		$this->Fense_FUN->add_option(
			__( 'Excluded URLs', 'fense-block-vpn-proxy' ),
			'excluded_urls', 'multi_input', 'checkbox', '', 'tab_3',
			__( 'Comma-separated URL paths to always skip detection on, regardless of mode (e.g. api/payments, checkout/done).', 'fense-block-vpn-proxy' )
		);
	}
	
	public function FENSE_BPVT_display_general_account()
	{
	
	}
	
}
