<?php
/**
 * The page Settings.
 *
 * @since 1.0.0
 */

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

class WCL_PerformanceInstantPagesPage extends WCL_Page {

	/**
	 * The id of the page in the admin menu.
	 *
	 * Mainly used to navigate between pages.
	 * @see FactoryPages000_AdminPage
	 *
	 * @since 1.0.0
	 * @var string
	 */
	public $id = "performance_instant_pages";

	/**
	 * @var string
	 */
	public $page_parent_page = 'performance';

	/**
	 * @var string
	 */
	public $page_menu_dashicon = 'dashicons-performance';

	/**
	 * @var int
	 */
	public $page_menu_position = 20;

	public $available_for_multisite = true;

	/**
	 * @param WCL_Plugin $plugin
	 */
	public function __construct(WCL_Plugin $plugin)
	{
		$this->menu_title = __('Instant Pages', 'clearfy');

		parent::__construct($plugin);

		$this->plugin = $plugin;
	}

	/**
	 * Permalinks options.
	 *
	 * @return mixed[]
	 * @since 1.0.0
	 */
	public function getPageOptions()
	{
		$options = array();

		$options[] = array(
			'type' => 'html',
			'html' => '<div class="wbcr-factory-page-group-header">' . __('<strong>Instant Pages Settings</strong>.', 'clearfy') . '<p>' . __('General cdn settings.', 'clearfy') . '</p></div>'
		);

		$options[] = array(
			'type' => 'checkbox',
			'way' => 'buttons',
			'name' => 'enable_intant_pages',
			'title' => __('Enable Intant Pages', 'clearfy'),
			'layout' => array('hint-type' => 'icon', 'hint-icon-color' => 'grey'),
			'hint' => __('Enable Intant Pages.', 'clearfy'),
			'default' => false
		);
		$options[] = [
			'type' => 'dropdown',
			'name' => 'instant_pages_delay_start_preloading',
			'data' => [
				['0', __('0 second (recommended)', 'clearfy')],
				['1', __('1 second', 'clearfy')],
				['2', __('2 seconds', 'clearfy')],
				['3', __('3 seconds', 'clearfy')],
				['4', __('4 seconds', 'clearfy')],
				['5', __('5 seconds', 'clearfy')],
				['10', __('10 seconds', 'clearfy')],
				['15', __('15 seconds', 'clearfy')],
				['25', __('25 seconds', 'clearfy')],
				['30', __('30 seconds', 'clearfy')],
			],
			'title' => __('Delay to start preloading', 'clearfy'),
			'hint' => __('Delay to start preloading links in the viewport after browser becomes idle
(hovering links doesn\'t have any delay)', 'clearfy'),
			//'layout' => array('hint-type' => 'icon', 'hint-icon-color' => 'grey'),
			'default' => '0'
		];

		$options[] = [
			'type' => 'dropdown',
			'name' => 'instant_pages_max_requests_per_second',
			'data' => [
				['0', __('Unlimited', 'clearfy')],
				['1', __('1 request', 'clearfy')],
				['2', __('2 requests', 'clearfy')],
				['3', __('3 requests', 'clearfy')],
				['4', __('4 requests', 'clearfy')],
				['5', __('5 requests', 'clearfy')],
				['10', __('10 requests', 'clearfy')]
			],
			'title' => __('Max requests per second', 'clearfy'),
			'hint' => __('Maximum requests browser should send to the server per second', 'clearfy'),
			//'layout' => array('hint-type' => 'icon', 'hint-icon-color' => 'grey'),
			'default' => '0'
		];

		/*$options[] = array(
			'type' => 'checkbox',
			'way' => 'buttons',
			'name' => 'instant_pages_preload_on_mouse_hover',
			'title' => __('Preload only on mouse hover', 'clearfy'),
			'layout' => array('hint-type' => 'icon', 'hint-icon-color' => 'grey'),
			'hint' => __('Start preloading links only on mouse hover.', 'clearfy'),
			'default' => false
		);*/

		$options[] = [
			'type' => 'dropdown',
			'name' => 'instant_pages_mouse_hover_delay',
			'data' => [
				['50', __('50 ms', 'clearfy')],
				['100', __('100 ms', 'clearfy')],
				['150', __('150 ms', 'clearfy')],
				['200', __('200 ms', 'clearfy')],
				['500', __('500 ms', 'clearfy')],
				['1000', __('1 second', 'clearfy')]
			],
			'title' => __('Mouse hover delay', 'clearfy'),
			'hint' => __('Delay to start preloading links on mouse hover.', 'clearfy'),
			//'layout' => array('hint-type' => 'icon', 'hint-icon-color' => 'grey'),
			'default' => '50'
		];

		$options[] = array(
			'type' => 'textarea',
			'way' => 'buttons',
			'name' => 'instant_pages_ignore_keywords',
			'title' => __('Ignore keywords', 'clearfy'),
			'layout' => array('hint-type' => 'icon', 'hint-icon-color' => 'grey'),
			'hint' => __('The list of keywords that should be ignored from preloading.', 'clearfy'),
			'default' => implode("\n", [
				'/wp-admin',
				'/wp-login.php',
				'/cart',
				'/checkout',
				'add-to-cart',
				'logout',
				'#',
				'?',
				'.png',
				'.jpeg',
				'.jpg',
				'.gif',
				'.svg',
				'.webp'
			])
		);

		$options[] = array(
			'type' => 'checkbox',
			'way' => 'buttons',
			'name' => 'instant_pages_disable_for_logged_users',
			'title' => __('Disable for logged in admins', 'clearfy'),
			'layout' => array('hint-type' => 'icon', 'hint-icon-color' => 'grey'),
			'hint' => __('Helps to reduce server load since most of the cache plugins do not serve cached pages when logged in.', 'clearfy'),
			'default' => true
		);

		$form_options = array();

		$form_options[] = array(
			'type' => 'form-group',
			'items' => $options,
			//'cssClass' => 'postbox'
		);

		return apply_filters('wclearfy/performance/intant_pages_form', $form_options, $this);
	}
}
