<?php

namespace CarDealerPress\includes\helpers;

use CarDealerPress\includes\helpers\helpers as helpers;

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

class CheckRequirements {

	private $error_message     = null;
	private $local_versions    = array();
	private $required_versions = array();
	private $master_file;

	public function has_been_checked() {
		return get_option( 'cdp_requirements_checked' );
	}

	public function set_master_file( $file ) {
		$this->master_file = $file;
	}

	public function check_requirements(): bool {
		$this->get_local_versions();
		$this->get_required_versions();
		if ( false === $this->compare_versions() ) {
			$this->unable_to_load();

			return false;
		} else {
			$this->set_has_been_checked_flag();

			return true;
		}
	}

	private function get_local_versions() {
		$this->local_versions['php']       = $this->get_php_version();
		$this->local_versions['wordpress'] = $this->get_wordpress_version();
	}

	private function get_php_version() {
		return phpversion();
	}

	private function get_wordpress_version() {
		global $wp_version;

		return $wp_version;
	}

	private function get_required_versions() {
		$this->required_versions['php']       = $this->get_required_php_version();
		$this->required_versions['wordpress'] = $this->get_required_wordpress_version();

	}

	private function get_required_php_version() {
		if ( ! isset( $this->required_versions['php'] ) ) {
			$this->required_versions['php'] = '7.4';
		}

		return $this->required_versions['php'];
	}

	private function get_required_wordpress_version() {
		if ( ! isset( $this->required_versions['wordpress'] ) ) {
			$this->required_versions['wordpress'] = '5.6';
		}

		return $this->required_versions['wordpress'];
	}

	private function compare_versions() {
		foreach ( $this->required_versions as $requirement_name => $required_version ) {
			if ( ! isset( $this->local_versions[ $requirement_name ] ) || $required_version >= $this->local_versions[ $requirement_name ] ) {
				$this->error_message = '<strong> ' . strtoupper( $requirement_name ) . ' is required to be <span style="color:red;">' . $required_version . '</span> or greater.
				 Your version is: <span style="color:red;">' . $this->local_versions[ $requirement_name ] . '</span>
				</strong>';

				return false;
			}
		}

		return true;
	}

	public function unable_to_load() {
		add_action( 'admin_notices', array( &$this, 'display_admin_error' ) );
		add_action( 'admin_init', array( &$this, 'deactivate_plugin' ) );
	}

	public function display_admin_error() {
		helpers::output_wp_kses( '<div class="error">
			<p><span style="font-weight:bold; color:red;">ERROR:</span>: Unable to activate plugin. System requirements are not met.</p>
			<p>' . $this->error_message . '</p>
			<p>Plugin has been <strong>deactivated</strong>.</p>
		</div>');
		$this->hide_default_activate_notice();
	}

	public function hide_default_activate_notice() {
		$nonce_check = isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_REQUEST['_wpnonce'] ), 'cdp-nonce' );
		if ($nonce_check) {
			// @codingStandardsIgnoreStart
			$safe = helpers::sanitize_get( $_GET );
			// @codingStandardsIgnoreEnd
			unset( $safe['activate'] );
		}
	}

	public function deactivate_plugin() {
		deactivate_plugins( plugin_basename( $this->master_file ) );
	}

	private function set_has_been_checked_flag() {
		update_option( 'cdp_requirements_checked', true );
	}
}
