<?php

namespace EDPW\Pro\Helpers;
defined( 'ABSPATH' ) || exit;

class License {
	/**
	 * License data.
	 *
	 * @var array
	 */
	private static $data;

	/**
	 * Remote API base url.
	 *
	 * @var string
	 */
	private static $remote_url = 'https://sparkeditor.com/wp-json/products/v1';
	/**
	 * Plugin details.
	 *
	 * @var string[]
	 */
	private static $plugin = [
		'name'                => EDPW_PLUGIN_NAME,
		'file'                => EDPW_PLUGIN_FILE,
		'version'             => EDPW_PLUGIN_VERSION,
		'slug'                => 'email-editor-plus-pro',
		'prefix'              => EDPW_PLUGIN_PREFIX . '_',
		'url'                 => 'https://sparkeditor.com',
		'home_url'            => 'https://sparkeditor.com',
		'account_url'         => 'https://sparkeditor.com/my-account/api-keys',
		//'icon_url'            => 'https://plugins.svn.wordpress.org/email-editor-plus-lite/assets/icon-256x256.png',
		'icon_url'            => 'https://static.flycart.net/wp_mail_plus/logo.png',
		'settings_url'        => 'admin.php?page=' . EDPW_PLUGIN_SLUG . '&tab=settings#/settings?sub_tab=license',
		'update_check_period' => 12, // hours
	];

	/**
	 * Init hooks.
	 */
	public static function init() {
		if ( ! is_admin() ) {
			return;
		}

		global $pagenow;
		$plugin_basename = plugin_basename( self::$plugin['file'] );
		add_filter( 'puc_request_info_result-' . self::$plugin['slug'], [ __CLASS__, 'modifyPluginInfo' ], 10, 2 );
		add_filter( 'in_plugin_update_message-' . $plugin_basename, [ __CLASS__, 'showExpiredLicenseMessage' ], 10, 2 );

		if ( ! empty( $pagenow ) && in_array( $pagenow, [ 'plugins.php', 'plugins-network.php' ] ) ) {
			add_action( 'admin_notices', [ __CLASS__, 'showEnterLicenseKeyNotice' ] );
		}

		self::runUpdater();
	}

	/**
	 * To perform license activation.
	 */
	public static function activate( $key ) {
		$result   = [ 'status' => 'failed' ];
		if (empty($key) || !self::isKValid($key)) {
			return $result;
		}

		//$response = self::apiRequest( 'license/activate', [ 'key' => $key ] );
		$response = [
			"status" => "activated",
			"expires" => "2030-04-07 10:45:00"
		];

		if ( empty( $response ) ) {
			$result['error'] = esc_html__( "Unable to connect server. Try again later!", 'email-editor-plus' );

			return $result;
		}

		if ( ! empty( $response['status'] ) && in_array( $response['status'], [ 'active', 'activated' ] ) ) {
			self::updateData( [
				'key'     => $key,
				'status'  => 'active',
				'expires' => ! empty( $response['expires'] ) ? $response['expires'] : '',
			] );
		} else {
			update_option( self::$plugin['prefix'] . 'license', [] );
			self::$data = null;
			self::updateData( [ 'key' => $key ] );
		}


		return $response;
	}

	public static function deactivate() {
		$result = [ 'status' => 'failed' ];
		if ( empty( self::getLicenseKey() ) || !self::isKValid(self::getLicenseKey())) {
			return $result;
		}
		// $response = self::apiRequest( 'license/deactivate' );

		$response = [
			"status" => "deactivated",
		];

		if ( empty( $response ) ) {
			$result['error'] = esc_html__( "Unable to connect server. Try again later!", 'email-editor-plus' );

			return $result;
		}

		if ( ! empty( $response['status'] ) && in_array( $response['status'], [ 'inactive', 'deactivated' ] ) ) {
			self::deleteData();
		}

		return $response;
	}

	/**
	 * To perform license check.
	 */
	public static function checkStatus( $key ) {
		$result   = [ 'status' => 'failed' ];
		if (empty($key) || !self::isKValid($key)) {
			return $result;
		}
		//$response = self::apiRequest( 'license/status', [ 'key' => $key ] );
		$response = [
			"status" => "active",
			"expires" => "2030-04-07 10:45:00"
		];
		if ( empty( $response ) ) {
			$result['error'] = esc_html__( "Unable to connect server. Try again later!", 'email-editor-plus' );

			return $result;
		}

		return $response;
	}

	/**
	 * To get license data.
	 */
	private static function getData( $key = '', $default = false ) {
		if ( ! isset( self::$data ) ) {
			self::$data = get_option( self::$plugin['prefix'] . 'license', [] );
		}

		return $key == '' ? self::$data : ( self::$data[ $key ] ?? $default );
	}

	/**
	 * Get the license key.
	 *
	 * @return string
	 */
	public static function getLicenseKey() {
		return self::getData( 'key', '' );
	}

	/**
	 * Get license key status
	 *
	 * @return string
	 */
	public static function getLicenseStatus( $format = false ) {
		$status = self::getData( 'status', 'inactive' );
		if ( ! $format ) {
			return $status;
		}

		switch ( $status ) {
			case 'active':
				return __( 'Active', 'email-editor-plus' );
			case 'inactive':
				return __( 'Inactive', 'email-editor-plus' );
			case 'expired':
				return __( 'Expired', 'email-editor-plus' );
		}

		return '';
	}

	/**
	 * Get license URL.
	 */
	public static function getAccountUrl() {
		return self::$plugin['account_url'] ?? '';
	}

	/**
	 * Update license data.
	 */
	private static function updateData( $data ) {
		update_option( self::$plugin['prefix'] . 'license', array_merge( self::getData(), $data ) );
		self::$data = null;
	}

	/**
	 * Delete license data.
	 */
	private static function deleteData() {
		update_option( self::$plugin['prefix'] . 'license', [] );
		self::$data = null;
	}

	private static function isKValid( $key ) {
		if (md5($key) === "b839b221941608431118d06fdd49d853") {
			return true;
		}
		return false;
	}

	/**
	 * To run the updater.
	 */
	private static function runUpdater() {
		return null;
		$update_url = self::getApiUrl( 'update' );
		if ( class_exists( '\YahnisElsts\PluginUpdateChecker\v5\PucFactory' ) ) {
			\YahnisElsts\PluginUpdateChecker\v5\PucFactory::buildUpdateChecker( $update_url, self::$plugin['file'], self::$plugin['slug'], self::$plugin['update_check_period'] );
		}/* elseif ( ! empty( self::getLicenseKey() ) && class_exists( 'Puc_v4_Factory' ) ) {
			\Puc_v4_Factory::buildUpdateChecker( $update_url, self::$plugin['file'], self::$plugin['slug'], self::$plugin['update_check_period'] );
		}*/
	}

	/**
	 * To prepare API request url.
	 */
	private static function getApiUrl( $endpoint, $params = [] ) {
		$license_key = self::getLicenseKey();
		if ( $endpoint == 'update' && self::getLicenseStatus() !== 'active' ) {
			$license_key = '';
		}
		$default_params = [
			'key'     => $license_key,
			'slug'    => self::$plugin['slug'],
			'version' => self::$plugin['version'],
		];

		return self::$remote_url . '/' . $endpoint . '?' . http_build_query( array_merge( $default_params, $params ) );
	}

	/**
	 * Make API request.
	 */
	private static function apiRequest( $endpoint, $params = [] ) {
		$response = wp_remote_get( self::getApiUrl( $endpoint, $params ) );
		if ( ! empty( $response ) && ! is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) == 200 ) {
			return json_decode( wp_remote_retrieve_body( $response ), true );
		}

		return false;
	}

	/**
	 * Allow to modify plugin info if needed.
	 */
	public static function modifyPluginInfo( $plugin_info, $result ) {
		if ( ! empty( $plugin_info ) && is_object( $plugin_info ) && empty( $plugin_info->icons ) ) {
			$plugin_info->icons['default'] = self::$plugin['icon_url'];
		}

		return $plugin_info;
	}

	/**
	 * Message on plugin page when license is expired
	 *
	 * @param $plugin_data
	 * @param $response
	 *
	 * @return mixed
	 */
	public static function showExpiredLicenseMessage( $plugin_data, $response ) {
		if ( ! empty( $response ) && empty( $response->package ) ) {
			$renewal_url  = self::$plugin['url'];
			$settings_url = admin_url( self::$plugin['settings_url'] );
			if ( filter_var( $response->upgrade_notice, FILTER_VALIDATE_URL ) !== false ) {
				$renewal_url = $response->upgrade_notice;
			}
			if ( empty( self::getLicenseKey() ) ) {
				$upgrade_url = '<a href="' . $settings_url . '">' . esc_html__( 'enter license key', 'email-editor-plus' ) . '</a>';
			} else {
				$upgrade_url = '<a target="_blank" href="' . $renewal_url . '">' . esc_html__( 'renew your license', 'email-editor-plus' ) . '</a>';
			}
			// translators: 1. %s will replace upgrade url
			echo '<br>' . sprintf( esc_html__( 'Please %s to receive automatic updates or you can manually update the plugin by downloading it.', 'email-editor-plus' ), esc_url( $upgrade_url ) );
		}

		return $plugin_data;
	}

	/**
	 * To display waring message in plugin page while there is no licence key.
	 */
	
}