<?php

if (!defined('ABSPATH')) {
	exit;
}

function gl_edd_check_all_licenses()
{
	$licenses = get_option('gl_edd_licenses');
	$res = [];
	foreach ($licenses as $plugin_key => $data) {
		$expires = $data['expires'];
		$res[$plugin_key] = $data['status'] === 'valid' && !empty($expires) && ($expires === 'lifetime' || (date('Y-m-d') <= date('Y-m-d', strtotime($expires)))) ? 'valid' : 'invalid';
	}
	return $res;
}

function gl_edd_has_premium_access($licenses = null)
{
	if ($licenses === null) {
		$licenses = gl_edd_check_all_licenses();
	}

	return (!empty($licenses['gl_addon']) && $licenses['gl_addon'] === 'valid')
		|| (!empty($licenses['all_in_one']) && $licenses['all_in_one'] === 'valid');
}

// if all access key is valid return it, else return addon key
function greenshift_edd_get_license_for_addon($addon)
{
	$licenses = get_option('gl_edd_licenses');

	if(!empty($licenses[$addon]['included_in'])){
		foreach ($licenses[$addon]['included_in'] as $key_pack) {
			if ($licenses[$key_pack]['status'] === 'valid') return $licenses[$key_pack]['license'];
		}
	}

	return $licenses[$addon]['license'];
}

class EddLicensePage
{
	private $licensesData = [
		'all_in_one' => [
			'plugin_id' => 223,
			'plugin_name' => 'All in One Access',
			'license_key' => 'edd_license_key_all_in_one',
			'expires_key' => 'edd_license_expires_all_in_one',
			'license_status_key' => 'edd_license_status_all_in_one',
			'license' => '',
			'status' => '',
			'expires' => '',
			'license_limit' => '',
			'purchase_date' => '',
			'included_in' => [],
		],
		'gl_addon' => [
			'plugin_id' => 25677,
			'plugin_name' => 'GreenLight PRO',
			'license_key' => 'edd_license_key_gl_addon',
			'expires_key' => 'edd_license_expires_gl_addon',
			'license_status_key' => 'edd_license_status_gl_addon',
			'license' => '',
			'status' => '',
			'expires' => '',
			'license_limit' => '',
			'purchase_date' => '',
			'included_in' => ['all_in_one'],
		],
	];

	public function __construct()
	{
		//    update_option('gl_edd_licenses', []);
		if (!is_admin()) return false;

		$dbOptions = get_option('gl_edd_licenses');
		if (!empty($dbOptions)) $this->licensesData = $dbOptions;
		else {
			update_option('gl_edd_licenses', $this->licensesData);
		}

		if (!empty($_GET['page']) && $_GET['page'] === 'gl-license') {
			$this->edd_check_and_update_licenses();
		} else {
			//gl_edd_check_all_licenses();
		}

		//    add_action( 'admin_init', [$this, 'edd_check_and_update_licenses'] );
		add_action('admin_menu', [$this, 'edd_license_menu'], 999);
		add_action('admin_init', [$this, 'edd_register_option']);
		add_action('admin_init', [$this, 'edd_activate_license']);
		add_action('admin_init', [$this, 'edd_deactivate_license']);
		add_action('admin_notices', [$this, 'edd_admin_notices']);
	}

	public function edd_license_menu()
	{
		add_submenu_page(
			'gl_dashboard',
			__('Manage Licenses', 'gl-page-builder'),
			__('Manage Licenses', 'gl-page-builder'),
			'manage_options',
			EDD_GSPB_PLUGIN_LICENSE_PAGE,
			[$this, 'edd_license_page'],
			99
		);
	}

	public function edd_license_page()
	{
		if (!current_user_can('manage_options')) {
			wp_die('Unauthorized user');
		}
		add_settings_section(
			'edd_license_section',
			__('Manage Licenses', 'gl-page-builder'),
			[$this, 'edd_license_key_settings_section'],
			EDD_GSPB_PLUGIN_LICENSE_PAGE
		);

		// adding fields for addons keys
		foreach ($this->licensesData as $addon_key => $data) {
			add_settings_field(
				'edd_license_key_' . $addon_key,
				'<label for="edd_license_key_' . $addon_key . '">' . $data['plugin_name'] . '</label>',
				[$this, 'edd_license_key_settings_field'],
				EDD_GSPB_PLUGIN_LICENSE_PAGE,
				'edd_license_section',
				['product' => $addon_key]
			);
		}
?>
		
		<div class="wp-block-gl-blocks-container alignfull gspb_container gspb_container-gsbp-ead11204-4841" id="gspb_container-id-gsbp-ead11204-4841">
			<div class="wp-block-gl-blocks-container gspb_container gspb_container-gsbp-cbc3fa8c-bb26" id="gspb_container-id-gsbp-cbc3fa8c-bb26">

				<?php $activetab = 'license'; ?>
				<?php include(GL_DIR_PATH . 'templates/admin/navleft.php'); ?>


				<div class="wp-block-gl-blocks-container gspb_container gspb_container-gsbp-89d45563-1559" id="gspb_container-id-gsbp-89d45563-1559">
					<div class="wp-block-gl-blocks-container gspb_container gspb_container-gsbp-efb64efe-d083" id="gspb_container-id-gsbp-efb64efe-d083">
						<h2 id="gspb_heading-id-gsbp-ca0b0ada-6561" class="gspb_heading gspb_heading-id-gsbp-ca0b0ada-6561 "><?php esc_html_e('Plugins License Options', 'gl-page-builder'); ?></h2>
					</div>


					<div class="wp-block-gl-blocks-container gspb_container gspb_container-gsbp-7b4f8e8f-1a69" id="gspb_container-id-gsbp-7b4f8e8f-1a69">
						<div class="greenshift_form">
							<form method="post" action="options.php" class="gspb-edd-settings-form">
								<?php
								do_settings_sections(EDD_GSPB_PLUGIN_LICENSE_PAGE);
								settings_fields('edd_license_section');
								submit_button();
								?>
							</form>
						</div>
					</div>
				</div>
			</div>
		</div>
	<?php
	}

	public function edd_license_key_settings_section()
	{
		echo '<p class="gs-introtext">';
		esc_html_e('This is where you enter your license key. If you have All in one license, you can activate it without activation separate addons', 'gl-page-builder');
		echo '</p>';
	}

	public function edd_license_key_settings_field($args)
	{
		if ($args['product'] != 'all_in_one') {
			if ($this->licensesData['all_in_one']['status'] == 'valid') {
				echo '<p class="description">License: <span style="color: green;">Activated</span></p>';
				return;
			}
		}

		$license = $this->licensesData[$args['product']]['license'];
		$status  = $this->licensesData[$args['product']]['status'];
		$expires = $this->licensesData[$args['product']]['expires'];

	?>
		<p class="description"><?php esc_html_e('Enter your license key.', 'gl-page-builder'); ?></p>
		<?php
		if ('valid' !== $status) {
			printf(
				'<input type="password" autocomplete="off" class="regular-text" id="edd_license_key_' . $args['product'] . '" name="edd_license_key_' . $args['product'] . '" value="%s" />',
				esc_attr($license)
			);
		} else {
			printf(
				'<input type="text" class="regular-text" value="%s" />',
				"******************"
			);
		}
		$button = array(
			'name'  => 'edd_license_deactivate_' . $args['product'],
			'label' => __('Deactivate License', 'gl-page-builder'),
		);
		if ('valid' !== $status) {
			$button = array(
				'name'  => 'edd_license_activate_' . $args['product'],
				'label' => __('Activate License', 'gl-page-builder'),
			);
		}
		wp_nonce_field('edd_nonce', 'edd_nonce');
		?>
		<input type="submit" class="button-secondary" name="<?php echo esc_attr($button['name']); ?>" value="<?php echo esc_attr($button['label']); ?>" />

		<p class="description">
			<?php esc_html_e('License:', 'gl-page-builder') ?>
			<?php if (!empty($expires) && (date('Y-m-d') <= date('Y-m-d', strtotime($expires)) || $expires === 'lifetime') && 'valid' === $status) : ?>
				<span style="color: green;">Activated</span>
			<?php else : ?>
				<span style="color: red;">Deactivated</span>
			<?php endif; ?>
		</p>

		<?php if (!empty($expires) && date('Y-m-d') > date('Y-m-d', strtotime($expires)) && $expires !== 'lifetime') : ?>
			<p class="description">
				<span style="color: red;"><?php esc_html_e('This license is expired', 'gl-page-builder') ?></span>
			</p>
		<?php endif; ?>

		<?php if (!empty($expires) && 'valid' === $status) : ?>
			<p class="description"><?php esc_html_e('Expires:', 'gl-page-builder') ?> <?php echo esc_html($expires); ?></p>
		<?php endif; ?>
		<?php
	}

	/**
	 * Registers the license key setting in the options table.
	 *
	 * @return void
	 */
	public function edd_register_option()
	{
		register_setting('edd_license_section', 'gl_edd_licenses', function ($new) {

			foreach ($this->licensesData as $key => $data) {
				if (isset($_POST[$data['license_key']])) {
					if ($data['license'] && $data['license'] !== $_POST[$data['license_key']]) {
						$this->deactivate_license($data['license'], $data['plugin_id'], $data['plugin_name']);

						$this->licensesData[$key]['status'] = '';
						$this->licensesData[$key]['expires'] = '';
					}
					$this->licensesData[$key]['license'] = $_POST[$data['license_key']];
				}
			}

			return $this->licensesData;
		});
	}

	/**
	 * Activates the license key.
	 *
	 * @return void
	 */
	public function edd_activate_license()
	{

		if (!empty($_POST['edd_license_activate_query_addon'])) {
			$dataKey = 'query_addon';
		} else if (!empty($_POST['edd_license_activate_woocommerce_addon'])) {
			$dataKey = 'woocommerce_addon';
		} else if (!empty($_POST['edd_license_activate_chart_addon'])) {
			$dataKey = 'chart_addon';
		} else if (!empty($_POST['edd_license_activate_seo_addon'])) {
			$dataKey = 'seo_addon';
		} else if (!empty($_POST['edd_license_activate_gsap_addon'])) {
			$dataKey = 'gsap_addon';
		} else if (!empty($_POST['edd_license_activate_all_in_one'])) {
			$dataKey = 'all_in_one';
		} else if (!empty($_POST['edd_license_activate_all_in_one_seo'])) {
			$dataKey = 'all_in_one_seo';
		} else if (!empty($_POST['edd_license_activate_all_in_one_design'])) {
			$dataKey = 'all_in_one_design';
		} else if (!empty($_POST['edd_license_activate_all_in_one_woo'])) {
			$dataKey = 'all_in_one_woo';
		}else if (!empty($_POST['edd_license_activate_gl_addon'])) {
			$dataKey = 'gl_addon';
		} else {
			return;
		}

		// run a quick security check
		if (!check_admin_referer('edd_nonce', 'edd_nonce')) {
			return; // get out if we didn't click the Activate button
		}

		// retrieve the license from the database
		$license = $this->licensesData[$dataKey]['license'];
		$license_key = $this->licensesData[$dataKey]['license_key'];

		if (!$license) {
			$license = !empty($_POST[$license_key]) ? sanitize_text_field($_POST[$license_key]) : '';
		}
		if (!$license) {
			return;
		}

		// data to send in our API request
		$api_params = array(
			'edd_action'  => 'activate_license',
			'license'     => $license,
			'item_id'     => $this->licensesData[$dataKey]['plugin_id'],
			'item_name'   => rawurlencode($this->licensesData[$dataKey]['plugin_name']), // the name of our product in EDD
			'url'         => home_url(),
			'environment' => function_exists('wp_get_environment_type') ? wp_get_environment_type() : 'production',
		);

		// Call the custom API.
		$response = wp_remote_post(
			EDD_GSPB_STORE_URL,
			array(
				'timeout'   => 15,
				'sslverify' => false,
				'body'      => $api_params,
			)
		);

		// make sure the response came back okay
		if (is_wp_error($response) || 200 !== wp_remote_retrieve_response_code($response)) {

			if (is_wp_error($response)) {
				$message = $response->get_error_message();
			} else {
				$message = __('An error occurred, please try again.', 'gl-page-builder');
			}
		} else {

			$license_data = json_decode(wp_remote_retrieve_body($response));

			if (false === $license_data->success) {

				switch ($license_data->error) {

					case 'expired':
						$message = sprintf(
							/* translators: the license key expiration date */
							__('Your license key expired on %s.', 'gl-page-builder'),
							date_i18n(get_option('date_format'), strtotime($license_data->expires, current_time('timestamp')))
						);
						break;

					case 'disabled':
					case 'revoked':
						$message = __('Your license key has been disabled.', 'gl-page-builder');
						break;

					case 'missing':
						$message = __('Invalid license.', 'gl-page-builder');
						break;

					case 'invalid':
					case 'site_inactive':
						$message = __('Your license is not active for this URL.', 'gl-page-builder');
						break;

					case 'item_name_mismatch':
						/* translators: the plugin name */
						$message =  __('This appears to be an invalid license key for %s.', 'gl-page-builder');
						break;

					case 'no_activations_left':
						$message = __('Your license key has reached its activation limit.', 'gl-page-builder');
						break;

					default:
						$message = __('An error occurred, please try again.', 'gl-page-builder');
						break;
				}
			} else if(!isset($license_data->success)){
				$message = __('An error occurred, we can not connect with License server, please contact with support', 'gl-page-builder'); 
			}
		}

		// Check if anything passed on a message constituting a failure
		if (!empty($message)) {
			$redirect = add_query_arg(
				array(
					'page'          => EDD_GSPB_PLUGIN_LICENSE_PAGE,
					'sl_activation' => 'false',
					'message'       => rawurlencode($message),
				),
				admin_url('admin.php?page=' . EDD_GSPB_PLUGIN_LICENSE_PAGE)
			);

			wp_safe_redirect($redirect);
			exit();
		}

		if ('valid' === $license_data->license) {
			$this->licensesData[$dataKey]['expires'] = $license_data->expires;
			$this->licensesData[$dataKey]['status'] = $license_data->license;
			$this->licensesData[$dataKey]['license_limit'] = $license_data->license_limit;
			update_option('gl_edd_licenses', $this->licensesData);
		}

		wp_safe_redirect(admin_url('admin.php?page=' . EDD_GSPB_PLUGIN_LICENSE_PAGE));
		exit();
	}

	/**
	 * Deactivates the license key.
	 * This will decrease the site count.
	 *
	 * @return void
	 */
	public function edd_deactivate_license()
	{
		// listen for our activate button to be clicked
		if (
			isset($_POST['edd_license_deactivate_query_addon']) ||
			isset($_POST['edd_license_deactivate_woocommerce_addon']) ||
			isset($_POST['edd_license_deactivate_all_in_one']) ||
			isset($_POST['edd_license_deactivate_chart_addon']) ||
			isset($_POST['edd_license_deactivate_seo_addon']) ||
			isset($_POST['edd_license_deactivate_gsap_addon']) ||
			isset($_POST['edd_license_deactivate_all_in_one_seo']) ||
			isset($_POST['edd_license_deactivate_all_in_one_design']) ||
			isset($_POST['edd_license_deactivate_all_in_one_woo']) || 
			isset($_POST['edd_license_deactivate_gl_addon'])
		) {

			if (!empty($_POST['edd_license_deactivate_query_addon'])) {
				$dataKey = 'query_addon';
			} else if (!empty($_POST['edd_license_deactivate_woocommerce_addon'])) {
				$dataKey = 'woocommerce_addon';
			} else if (!empty($_POST['edd_license_deactivate_chart_addon'])) {
				$dataKey = 'chart_addon';
			} else if (!empty($_POST['edd_license_deactivate_seo_addon'])) {
				$dataKey = 'seo_addon';
			} else if (!empty($_POST['edd_license_deactivate_gsap_addon'])) {
				$dataKey = 'gsap_addon';
			} else if (!empty($_POST['edd_license_deactivate_all_in_one'])) {
				$dataKey = 'all_in_one';
			} else if (!empty($_POST['edd_license_deactivate_all_in_one_seo'])) {
				$dataKey = 'all_in_one_seo';
			} else if (!empty($_POST['edd_license_deactivate_all_in_one_design'])) {
				$dataKey = 'all_in_one_design';
			} else if (!empty($_POST['edd_license_deactivate_all_in_one_woo'])) {
				$dataKey = 'all_in_one_woo';
			}else if (!empty($_POST['edd_license_deactivate_gl_addon'])) {
				$dataKey = 'gl_addon';
			}

			// run a quick security check
			if (!check_admin_referer('edd_nonce', 'edd_nonce')) {
				return; // get out if we didn't click the Activate button
			}

			$response = $this->deactivate_license($this->licensesData[$dataKey]['license'], $this->licensesData[$dataKey]['plugin_id'], $this->licensesData[$dataKey]['plugin_name']);

			// decode the license data
			$license_data = json_decode(wp_remote_retrieve_body($response));

			// $license_data->license will be either "deactivated" or "failed"
			if ('deactivated' === $license_data->license) {
				$this->licensesData[$dataKey]['expires'] = '';
				$this->licensesData[$dataKey]['status'] = '';
				update_option('gl_edd_licenses', $this->licensesData);
			}

			wp_safe_redirect(admin_url('admin.php?page=' . EDD_GSPB_PLUGIN_LICENSE_PAGE));
			exit();
		}
	}

	public function deactivate_license($license, $plugin_id, $plugin_name)
	{
		// data to send in our API request
		$api_params = array(
			'edd_action'  => 'deactivate_license',
			'license'     => $license,
			'item_id'     => $plugin_id,
			'item_name'   => rawurlencode($plugin_name), // the name of our product in EDD
			'url'         => home_url(),
			'environment' => function_exists('wp_get_environment_type') ? wp_get_environment_type() : 'production',
		);

		// Call the custom API.
		$response = wp_remote_post(
			EDD_GSPB_STORE_URL,
			array(
				'timeout'   => 15,
				'sslverify' => false,
				'body'      => $api_params,
			)
		);

		// make sure the response came back okay
		if (is_wp_error($response) || 200 !== wp_remote_retrieve_response_code($response)) {

			if (is_wp_error($response)) {
				$message = $response->get_error_message();
			} else {
				$message = __('An error occurred, please try again.', 'gl-page-builder');
			}

			$redirect = add_query_arg(
				array(
					'page' => EDD_GSPB_PLUGIN_LICENSE_PAGE,
					'sl_activation' => 'false',
					'message' => rawurlencode($message),
				),
				admin_url('admin.php?page=' . EDD_GSPB_PLUGIN_LICENSE_PAGE)
			);

			wp_safe_redirect($redirect);
			exit();
		}

		delete_option('gl_edd_licenses');

		return $response;
	}

	public function edd_check_license($license, $plugin_id, $plugin_name)
	{
		$api_params = array(
			'edd_action'  => 'check_license',
			'license'     => $license,
			'item_id'     => $plugin_id,
			'item_name'   => rawurlencode($plugin_name),
			'url'         => home_url(),
			'environment' => function_exists('wp_get_environment_type') ? wp_get_environment_type() : 'production',
		);

		// Call the custom API.
		$response = wp_remote_post(
			EDD_GSPB_STORE_URL,
			array(
				'timeout'   => 15,
				'sslverify' => false,
				'body'      => $api_params,
			)
		);

		if (is_wp_error($response)) {
			return false;
		}

		return json_decode(wp_remote_retrieve_body($response));
	}

	public function edd_check_and_update_licenses()
	{
		if(empty($this->licensesData['all_in_one']['plugin_id'])) {
			delete_option('gl_edd_licenses');
		};


		foreach ($this->licensesData as $plugin_key => $data) {
			if (!$data['license']) continue;

			$license_data = $this->edd_check_license($data['license'], $data['plugin_id'], $data['plugin_name']);

			switch ($license_data->license) {
				case 'invalid':
					$this->licensesData[$plugin_key]['expires'] = '';
					$this->licensesData[$plugin_key]['status'] = '';
					$this->licensesData[$plugin_key]['license'] = '';
					break;
				case 'expired':
					$this->licensesData[$plugin_key]['expires'] = $license_data->expires;
					$this->licensesData[$plugin_key]['status'] = '';
					break;
				case 'inactive':
					$this->licensesData[$plugin_key]['expires'] = '';
					$this->licensesData[$plugin_key]['status'] = '';
					break;
				case 'disabled':
					$this->licensesData[$plugin_key]['expires'] = $license_data->expires;
					$this->licensesData[$plugin_key]['status'] = $license_data->license;
					break;
				default:
					$this->licensesData[$plugin_key]['expires'] = $license_data->expires;
					break;
			}
		}
		update_option('gl_edd_licenses', $this->licensesData);
	}

	static function edd_check_and_update_licenses_static()
	{
		$licenses_data = [];
		$dbOptions = get_option('gl_edd_licenses');

		if (empty($dbOptions)) return false;

		$licenses_data = $dbOptions;

		foreach ($licenses_data as $plugin_key => $data) {
			if (!$data['status']) continue;

			$api_params = array(
				'edd_action'  => 'check_license',
				'license'     => $data['license'],
				'item_id'     => $data['plugin_id'],
				'item_name'   => rawurlencode($data['plugin_name']),
				'url'         => home_url(),
				'environment' => function_exists('wp_get_environment_type') ? wp_get_environment_type() : 'production',
			);

			// Call the custom API.
			$response = wp_remote_post(
				EDD_GSPB_STORE_URL,
				array(
					'timeout'   => 15,
					'sslverify' => false,
					'body'      => $api_params,
				)
			);

			if (is_wp_error($response)) {
				return false;
			}

			$license_data = json_decode(wp_remote_retrieve_body($response));

			if(!isset($license_data->license)){
				return false;
			}

			switch ($license_data->license) {
				case 'invalid':
					$licenses_data[$plugin_key]['expires'] = '';
					$licenses_data[$plugin_key]['status'] = '';
					$licenses_data[$plugin_key]['license'] = '';
					break;
				case 'expired':
					$licenses_data[$plugin_key]['expires'] = $license_data->expires;
					$licenses_data[$plugin_key]['status'] = '';
					break;
				case 'inactive':
					$licenses_data[$plugin_key]['expires'] = '';
					$licenses_data[$plugin_key]['status'] = '';
					break;
				case 'disabled':
					$licenses_data[$plugin_key]['expires'] = $license_data->expires;
					$licenses_data[$plugin_key]['status'] = $license_data->license;
					break;
				default:
					$licenses_data[$plugin_key]['expires'] = $license_data->expires;
					break;
			}
		}

		update_option('gl_edd_licenses', $licenses_data);
	}

	/**
	 * This is a means of catching errors from the activation method above and displaying it to the customer
	 */
	public function edd_admin_notices()
	{
		if (isset($_GET['sl_activation']) && !empty($_GET['message'])) {

			switch ($_GET['sl_activation']) {

				case 'false':
					$message = urldecode($_GET['message']);
		?>
					<div class="error">
						<p><?php echo wp_kses_post($message); ?></p>
					</div>
				<?php
					break;

				case 'true':
				default:
				?>
					<div class="updated">
						<p><?php esc_html_e('Activated succesfull.', 'gl-page-builder') ?></p>
					</div>
<?php
					// Developers can put a custom success message here for when activation is successful if they way.
					break;
			}
		}
	}
}

//////////////////////////////////////////////////////////////////
// Schedule events
//////////////////////////////////////////////////////////////////
add_action('wp', 'gl_add_cron_event');
add_action('gl_check_cron_hook', 'gl_check_cron_exec');
function gl_check_cron_exec()
{
	EddLicensePage::edd_check_and_update_licenses_static();
}
function gl_add_cron_event()
{
	if (!wp_next_scheduled('gl_check_cron_hook')) {
		wp_schedule_event(time(), 'daily', 'gl_check_cron_hook');
	}
}
