<?php if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Update Helper class.
 *
 * @link    https://www.fooevents.com
 * @package fooevents-bookings
 */
class FooEvents_Update_Helper {


	/**
	 * Configuration object
	 *
	 * @var array $config contains paths and other configurations
	 */
	private $config;

	/**
	 * Slug
	 *
	 * @var string $slug
	 */
	private $slug;

	/**
	 * Plugin data
	 *
	 * @var array $plugin_data
	 */
	private $plugin_data;

	/**
	 * FooEvents API Key
	 *
	 * @var string $fooevents_api_key
	 */
	private $fooevents_api_key;

	/**
	 * Envato API Key
	 *
	 * @var string $envato_api_key
	 */
	private $envato_api_key;

	/**
	 * Site URL
	 *
	 * @var string $home_url
	 */
	private $home_url;

	/**
	 * Response
	 *
	 * @var array $fooevents_reponse
	 */
	private $fooevents_reponse;

	public function __construct( $config ) {

		$this->config = $config;

		// GPL Bypass - Mock license key hardcoded
		$this->fooevents_api_key = '1415B451BE1A13C283BA771EA52D38BB';
		$this->envato_api_key    = '1415b451-be1a-13c2-83ba-771ea52d38bb';
		$this->home_url          = get_home_url();

		/* Original license key retrieval - bypassed
		$this->fooevents_api_key = get_option( 'globalWooCommerceEventsAPIKey', true );
		$this->envato_api_key    = get_option( 'globalWooCommerceEnvatoAPIKey', true );
		$this->home_url          = get_home_url();

		if ( 1 === (int) $this->fooevents_api_key ) {

			$this->fooevents_api_key = '';

		}

		if ( 1 === (int) $this->envato_api_key ) {

			$this->envato_api_key = '';

		}
		*/

		add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'set_transitent' ) );
		add_filter( 'plugins_api', array( $this, 'plugin_update_information' ), 20, 3 );
		add_action( 'in_plugin_update_message-fooevents/fooevents.php', array( $this, 'show_upgrade_notification' ), 10, 2 );
		add_action( 'upgrader_process_complete', array( $this, 'after_plugin_update' ), 10, 2 );
		// GPL Bypass - License notice disabled
		// add_action( 'admin_notices', array( $this, 'check_if_key_set' ) );
	}

	public function plugin_update_information( $res, $action, $args ) {

		if ( 'plugin_information' !== $action ) {

			return false;

		}

		$plugin_slug = 'fooevents/fooevents.php';

		if ( $plugin_slug !== $args->slug ) {

			return false;

		}

		if ( false == $remote = get_transient( 'fooevents_update_' . $plugin_slug ) ) {

			$remote = wp_remote_get(
				'https://www.fooevents.com/update_info/fooevents_for_woocommerce.json',
				array(
					'timeout' => 10,
					'headers' => array(
						'Accept' => 'application/json',
					),
				)
			);

			if ( ! is_wp_error( $remote ) && isset( $remote['response']['code'] ) && 200 === $remote['response']['code'] && ! empty( $remote['body'] ) ) {

				set_transient( 'fooevents_update_' . $plugin_slug, $remote, 43200 ); // 12 hours cache

			}
		}

		if ( ! is_wp_error( $remote ) && isset( $remote['response']['code'] ) && 200 === $remote['response']['code'] && ! empty( $remote['body'] ) ) {

			$remote = json_decode( $remote['body'] );
			$res    = new stdClass();

			$res->name           = $remote->name;
			$res->slug           = $plugin_slug;
			$res->version        = $remote->version;
			$res->tested         = $remote->tested;
			$res->requires       = $remote->requires;
			$res->author         = '<a href="https://www.fooevents.com">FooEvents</a>';
			$res->author_profile = 'https://www.fooevents.com';
			$res->download_link  = '';
			$res->trunk          = '';
			$res->requires_php   = $remote->requires_php;
			$res->last_updated   = $remote->last_updated;
			$res->sections       = array(
				// 'description' => $remote->description,
				'changelog' => $remote->changelog,
			);

			$res->banners = array(
				'low'  => 'https://www.fooevents.com/update_info/fooevents_for_woocommerce-772x250.jpg',
				'high' => 'https://www.fooevents.com/update_info/fooevents_for_woocommerce-1544x500.jpg',
			);

			return $res;

		}

		return false;
	}

	public function set_transitent( $transient ) {

		$this->init_plugin_data();
		$this->get_latest_plugin_details_fooevents();

		if ( isset( $this->fooevents_reponse['update_available'] ) && 'yes' === $this->fooevents_reponse['update_available'] ) {

			$obj              = new stdClass();
			$obj->slug        = $this->slug;
			$obj->new_version = $this->fooevents_reponse['version'];
			$obj->url         = $this->fooevents_reponse['url'];
			$obj->package     = $this->fooevents_reponse['url'];

			$transient->response[ $this->slug ] = $obj;

		}

		return $transient;
	}

	public function init_plugin_data() {

		$this->slug        = plugin_basename( $this->config->plugin_file );
		$this->plugin_data = get_plugin_data( $this->config->plugin_file );
	}

	private function get_latest_plugin_details_fooevents() {

		if ( empty( $this->fooevents_api_key ) && empty( $this->envato_api_key ) ) {

			return;

		}

		if ( ! empty( $this->fooevents_reponse ) ) {

			return;

		}

		if ( empty( $this->plugin_data ) ) {

			$this->plugin_data = get_plugin_data( $this->config->plugin_file );

		}

		$url = 'https://updates.fooevents.com';

		$last_update_check    = get_option( '_fooevents_last_update_check', true );
		$last_update_response = get_option( '_fooevents_last_update_response', true );
		$expire_check         = current_time( 'timestamp' ) - 7200;

		if ( empty( $last_update_check ) || 1 === (int) $last_update_check || $last_update_check <= $expire_check ) {

			$params = array(
				'api'         => $this->fooevents_api_key,
				'envato_api'  => $this->envato_api_key,
				'plugin_name' => $this->plugin_data['Name'],
				'version'     => $this->plugin_data['Version'],
				'home_url'    => $this->home_url,
			);

			$response = wp_remote_post( $url, array( 'body' => $params ) );

			if ( ! is_wp_error( $response ) ) {

				$response                = $response['body'];
				$this->fooevents_reponse = json_decode( $response, true );
				update_option( '_fooevents_last_update_response', $response );

			} else {

				$this->fooevents_reponse = '';

			}

			$timestamp = current_time( 'timestamp' );
			update_option( '_fooevents_last_update_check', $timestamp );

		} else {

			$this->fooevents_reponse = json_decode( $last_update_response, true );

		}
	}

	public function show_upgrade_notification( $current_plugin_metadata, $new_plugin_metadata ) {

		// GPL Bypass - Only show backup notice, suppress license errors
		echo ' <strong><u>' . esc_attr( __( 'Please backup your files and database before updates!', 'fooevents-for-woocommerce' ) ) . '</u></strong>';

		/* Original upgrade notification - bypassed
		if ( empty( $this->fooevents_reponse ) ) {

			$this->get_latest_plugin_details_fooevents();

		}

		if ( ! empty( $this->fooevents_reponse ) ) {

			if ( 'error' === $this->fooevents_reponse['status'] || 'error_license' === $this->fooevents_reponse['status'] ) {

				echo '<span class="fooevents-update-notice" style="display: block">';
				echo '<strong>' . esc_attr( __( 'Important:', 'fooevents-for-woocommerce' ) ) . '</strong> ';
				echo esc_html( $this->fooevents_reponse['message'] ) . ' ';
				if ( isset( $this->fooevents_reponse['cta'] ) ) {

					echo '<strong>' . wp_kses_post( $this->fooevents_reponse['cta'] ) . '</strong> ';

				}
				echo '</span>';

			}

			if ( 'success' === $this->fooevents_reponse['status'] ) {

				echo ' <strong><u>' . esc_attr( __( 'Please backup your files and database before updates!', 'fooevents-for-woocommerce' ) ) . '</u></strong>';

			}
		}
		*/
	}

	public function after_plugin_update() {

		update_option( '_fooevents_last_update_check', '' );
		update_option( '_fooevents_last_update_response', '' );
	}

	public function check_if_key_set() {

		// GPL Bypass - License always valid, no notice needed
		return;

		/* Original license check notice - bypassed
		$fooevents_api_key = get_option( 'globalWooCommerceEventsAPIKey', true );
		$envato_api_key    = get_option( 'globalWooCommerceEnvatoAPIKey', true );

		if ( '1' == $fooevents_api_key ) {

			$fooevents_api_key = '';

		}

		if ( '1' == $envato_api_key ) {

			$envato_api_key = '';

		}

		if ( empty( $fooevents_api_key ) && empty( $envato_api_key ) ) {

			echo '<div class="notice notice-warning">';
			printf(
				'<p><strong>%s</strong> %s <a href="%s">%s</a>.</p>',
				esc_html__( 'Your FooEvents license key is not set.', 'fooevents-for-woocommerce' ),
				esc_html__( 'Please enter a valid license key in the', 'fooevents-for-woocommerce' ),
				esc_url( admin_url( 'admin.php?page=fooevents-settings' ) ),
				esc_html__( 'FooEvents Settings', 'fooevents-for-woocommerce' )
			);
			echo '</div>';
		}
		*/
	}
}
