<?php

namespace EDPW\Pro\Controllers;

use EDPW\Core\Helpers\EmailTemplate;
use EDPW\Core\Helpers\Input;
use EDPW\Core\Helpers\WC;

defined( 'ABSPATH' ) || exit;

class License {
	/**
	 * Performs the activation process for a license key.
	 *
	 * This method checks for security validation and the presence of a license key. It then attempts to activate the license using the provided key, updates settings, and sends a JSON response based on the license status.
	 *
	 * @return void
	 */
	public static function activate() {
		if ( ! WC::isSecurityValid( 'edpw-setting-nonce' ) ) {
			wp_send_json_error( [
				'status'  => 'inactive',
				'message' => __( 'Basic check failed', 'email-editor-plus' )
			] );
		}
		$license_key = (string) Input::get( 'license_key' );
		if ( empty( $license_key ) ) {
			wp_send_json_error( [
				'status'  => 'inactive',
				'message' => __( 'License key is required.', 'email-editor-plus' )
			] );
		}
		$response = \EDPW\Pro\Helpers\License::activate( $license_key );
		if ( empty( $response ) || $response['status'] == 'failed' ) {
			wp_send_json_error( [
				'status'  => 'inactive',
				'message' => ! empty( $response['error'] ) ? $response['error'] : __( 'License activation failed', 'email-editor-plus' )
			] );
		}
		$license_status      = \EDPW\Pro\Helpers\License::getLicenseStatus();
		$data                = EmailTemplate::getSettings();
		$data['license_key'] = $license_key;
		update_option( 'edpw_settings', $data, true );
		if ( $license_status == 'active' ) {
			wp_send_json_success( [
				'status'  => 'active',
				'message' => esc_html__( 'License activated. Thank you!', 'email-editor-plus' )
			] );
		}
		if ( $license_status == 'expired' ) {
			wp_send_json_success( [
				'status'  => 'inactive',
				'message' => esc_html__( 'License is expired.', 'email-editor-plus' )
			] );
		}
		wp_send_json_error( [
			'status'  => 'inactive',
			'message' => esc_html__( 'Invalid license key.', 'email-editor-plus' )
		] );
	}

	/**
	 * Deactivate the license and handle the response accordingly.
	 *
	 * This method deactivates the license by performing a series of checks and actions to ensure
	 * successful deactivation. If the deactivation process fails or encounters an error, appropriate
	 * error messages are returned.
	 *
	 * @return void
	 */
	public static function deActivate() {
		if ( ! WC::isSecurityValid( 'edpw-setting-nonce' ) ) {
			wp_send_json_error( [
				'status'  => 'inactive',
				'message' => __( 'Basic check failed', 'email-editor-plus' )
			] );
		}
		$response = \EDPW\Pro\Helpers\License::deactivate();
		if ( empty( $response ) || $response['status'] == 'failed' ) {
			wp_send_json_error( [
				'status'  => 'inactive',
				'message' => ! empty( $response['error'] ) ? $response['error'] : __( 'License deactivation failed', 'email-editor-plus' )
			] );
		}
		$license_status = \EDPW\Pro\Helpers\License::getLicenseStatus();
		if ( $license_status == 'inactive' ) {
			wp_send_json_success( [
				'status'  => 'inactive',
				'message' => esc_html__( 'License deactivated.', 'email-editor-plus' )
			] );
		}
		wp_send_json_error( [
			'status'  => 'inactive',
			'message' => esc_html__( 'Something went wrong. Try again later!', 'email-editor-plus' )
		] );
	}

	/**
	 * Check the status of a license key and handle the response accordingly.
	 *
	 * This method checks the status of a provided license key by performing necessary validations and
	 * querying the status. Different responses are sent back based on the status of the license key.
	 *
	 * @return void
	 */
	public static function checkStatus() {
		if ( ! WC::isSecurityValid( 'edpw-setting-nonce' ) ) {
			wp_send_json_error( [
				'status'  => 'inactive',
				'message' => __( 'Basic check failed', 'email-editor-plus' )
			] );
		}
		$license_key = (string) Input::get( 'license_key' );
		if ( empty( $license_key ) ) {
			wp_send_json_error( [
				'status'  => 'inactive',
				'message' => __( 'License key is required.', 'email-editor-plus' )
			] );
		}
		$response = \EDPW\Pro\Helpers\License::checkStatus( $license_key );
		if ( empty( $response ) || $response['status'] == 'failed' ) {
			wp_send_json_error( [
				'status'  => 'inactive',
				'message' => ! empty( $response['error'] ) ? $response['error'] : __( 'License deactivation failed', 'email-editor-plus' )
			] );
		}
		if ( $response['status'] == 'active' ) {
			wp_send_json_success( [
				'status'  => 'active',
				'message' => esc_html__( 'License is active.', 'email-editor-plus' )
			] );
		}
		if ( $response['status'] == 'expired' ) {
			wp_send_json_success( [
				'status'  => 'inactive',
				'message' => esc_html__( 'License is expired.', 'email-editor-plus' )
			] );
		}
		if ( $response['status'] == 'inactive' ) {
			wp_send_json_success( [
				'status'  => 'inactive',
				'message' => esc_html__( 'Inactive license.', 'email-editor-plus' )
			] );
		}
		wp_send_json_error( [ 'status' => 'inactive', 'message' => __( 'Invalid license.', 'email-editor-plus' ) ] );
	}

	/**
	 * Get the license status based on the provided data array.
	 *
	 * This method checks if the provided data is an array. If it is not an array, it simply returns
	 * the data as it is. If the data is an array, it adds the 'license_status' key to the array
	 * and assigns the license status value obtained from the License class.
	 *
	 * @param array $data The input data to check the license status for.
	 *
	 * @return array
	 */
	public static function getLicenseStatus( array $data ) {
		$data['license_status'] = \EDPW\Pro\Helpers\License::getLicenseStatus();

		return $data;
	}

	/**
	 * Retrieves the current license check status based on the provided data.
	 *
	 * @param array $data The data array containing license information.
	 *
	 * @return array Updated data array with the license key included or the original data if not an array.
	 */
	public static function getLicenseCheckStatus( array $data ) {
		$license_key = Input::get( 'license_key' );
		if ( $license_key != \EDPW\Pro\Helpers\License::getLicenseKey() ) {
			\EDPW\Pro\Helpers\License::activate( $license_key );
		}

		$data['license_key'] = $license_key;

		return $data;
	}

	/**
	 * Displays a header notice based on certain conditions.
	 *
	 * This method checks the current page and license status to determine if a header notice should be displayed.
	 * If the page is not set or does not match the plugin slug, no notice is displayed.
	 * If the license status is not 'active' or the license key is empty, a notice is displayed to remind the user to activate the license key.
	 *
	 * @return void This method does not return any value directly, it outputs the HTML notice if conditions are met.
	 */
	
}