<?php
/**
 * File: Extension_CloudFlare_Page.php
 *
 * @package W3TC
 */

namespace W3TC;

/**
 * Class Extension_CloudFlare_Page
 */
class Extension_CloudFlare_Page {
	/**
	 * Enqueues the necessary scripts for the Cloudflare performance page.
	 *
	 * phpcs:disable WordPress.Security.NonceVerification.Recommended
	 *
	 * @return void
	 */
	public static function admin_print_scripts_performance_page_w3tc_cdn() {
		if (
			(
				isset( $_REQUEST['extension'] ) &&
				'cloudflare' === Util_Request::get_string( 'extension' )
			) ||
			(
				isset( $_REQUEST['page'] ) &&
				'w3tc_cdnfsd' === Util_Request::get_string( 'page' )
			)
		) {
			wp_enqueue_script(
				'w3tc_extension_cloudflare',
				plugins_url( 'Extension_CloudFlare_Page_View.js', W3TC_FILE ),
				array( 'jquery', 'w3tc-nonce', 'w3tc-lightbox' ),
				'1.0',
				false
			);
		}
	}

	/**
	 * Includes the CDN settings box view for the Cloudflare extension.
	 *
	 * @return void
	 */
	public static function w3tc_settings_box_cdnfsd() {
		include W3TC_DIR . '/Extension_CloudFlare_Cdn_Page_View.php';
	}

	/**
	 * Renders the main Cloudflare extension page.
	 *
	 * @return void
	 *
	 * @throws \Exception If an error occurs while retrieving Cloudflare settings.
	 */
	public static function w3tc_extension_page_cloudflare() {
		$w3tc_c = Dispatcher::config();
		$api    = Extension_CloudFlare_SettingsForUi::api();

		$email    = $w3tc_c->get_string( array( 'cloudflare', 'email' ) );
		$w3tc_key = $w3tc_c->get_string( array( 'cloudflare', 'key' ) );
		$zone_id  = $w3tc_c->get_string( array( 'cloudflare', 'zone_id' ) );

		if ( empty( $zone_id ) || ! Extension_CloudFlare_Api::are_api_credentials_usable( $email, $w3tc_key ) ) {
			$state = 'not_configured';
		} else {
			$w3tc_settings = array();

			try {
				$w3tc_settings = Extension_CloudFlare_SettingsForUi::settings_get( $api );
				$state         = 'available';
			} catch ( \Exception $ex ) {
				$state         = 'not_available';
				$error_message = $ex->getMessage();

			}
		}

		$w3tc_config = $w3tc_c;

		include W3TC_DIR . '/Extension_CloudFlare_Page_View.php';
	}

	/**
	 * Renders a checkbox input for the Cloudflare settings.
	 *
	 * @param array $w3tc_settings The current Cloudflare settings.
	 * @param array $w3tc_data     Metadata for the checkbox input (key, label, description, etc.).
	 *
	 * @return void
	 */
	private static function cloudflare_checkbox( $w3tc_settings, $w3tc_data ) {
		if ( ! isset( $w3tc_settings[ $w3tc_data['key'] ] ) ) {
			return;
		}

		$w3tc_value    = ( 'on' === $w3tc_settings[ $w3tc_data['key'] ]['value'] );
		$w3tc_disabled = ! $w3tc_settings[ $w3tc_data['key'] ]['editable'];

		Util_Ui::table_tr(
			array(
				'id'          => $w3tc_data['key'],
				'label'       => $w3tc_data['label'],
				'checkbox'    => array(
					'name'     => 'cloudflare_api_' . $w3tc_data['key'],
					'value'    => $w3tc_value,
					'disabled' => $w3tc_disabled,
					'label'    => 'Enable',
				),
				'description' => $w3tc_data['description'],
			)
		);
	}

	/**
	 * Renders a select box input for the Cloudflare settings.
	 *
	 * @param array $w3tc_settings The current Cloudflare settings.
	 * @param array $w3tc_data     Metadata for the select box input (key, label, values, etc.).
	 *
	 * @return void
	 */
	private static function cloudflare_selectbox( $w3tc_settings, $w3tc_data ) {
		if ( ! isset( $w3tc_settings[ $w3tc_data['key'] ] ) ) {
			return;
		}

		$w3tc_value    = $w3tc_settings[ $w3tc_data['key'] ]['value'];
		$w3tc_disabled = ! $w3tc_settings[ $w3tc_data['key'] ]['editable'];

		Util_Ui::table_tr(
			array(
				'id'          => $w3tc_data['key'],
				'label'       => $w3tc_data['label'],
				'selectbox'   => array(
					'name'     => 'cloudflare_api_' . $w3tc_data['key'],
					'value'    => $w3tc_value,
					'disabled' => $w3tc_disabled,
					'values'   => $w3tc_data['values'],
				),
				'description' => $w3tc_data['description'],
			)
		);
	}

	/**
	 * Renders a text input box for the Cloudflare settings.
	 *
	 * @param array $w3tc_settings The current Cloudflare settings.
	 * @param array $w3tc_data     Metadata for the text input box (key, label, description, etc.).
	 *
	 * @return void
	 */
	private static function cloudflare_textbox( $w3tc_settings, $w3tc_data ) {
		if ( ! isset( $w3tc_settings[ $w3tc_data['key'] ] ) ) {
			return;
		}

		$w3tc_value    = $w3tc_settings[ $w3tc_data['key'] ]['value'];
		$w3tc_disabled = ! $w3tc_settings[ $w3tc_data['key'] ]['editable'];

		Util_Ui::table_tr(
			array(
				'id'          => $w3tc_data['key'],
				'label'       => $w3tc_data['label'],
				'textbox'     => array(
					'name'     => 'cloudflare_api_' . $w3tc_data['key'],
					'value'    => $w3tc_value,
					'disabled' => $w3tc_disabled,
				),
				'description' => $w3tc_data['description'],
			)
		);
	}
}
