<?php

// Exit if accessed directly
if( !defined('ABSPATH') ) {
	exit;
}

/**
 * Class Wbcr_FactoryLicense000_LicensePage is used as template to display form to active premium functionality.
 *
 * @author        Alex Kovalev <alex.kovalevv@gmail.com>, Github: https://github.com/alexkovalevv
 * @since         2.0.7
 */
class Wbcr_FactoryClearfy219_LicensePage extends Wbcr_FactoryClearfy219_PageBase {

	/**
	 * {@inheritdoc}
	 *
	 * @author Alexander Kovalev <alex.kovalevv@gmail.com>
	 * @since  2.1.2
	 * @var string
	 */
	public $type = "page";

	/**
	 * {@inheritdoc}
	 *
	 * @author Alexander Kovalev <alex.kovalevv@gmail.com>
	 * @since  2.1.2
	 * @var string
	 */
	public $page_menu_dashicon = 'dashicons-admin-network';

	/**
	 * {@inheritdoc}
	 *
	 * @author Alexander Kovalev <alex.kovalevv@gmail.com>
	 * @since  2.1.2
	 * @var bool
	 */
	public $show_right_sidebar_in_options = false;

	/**
	 * {@inheritdoc}
	 *
	 * @author Alexander Kovalev <alex.kovalevv@gmail.com>
	 * @since  2.1.2
	 * @var int
	 */
	public $page_menu_position = 0;

	/**
	 * {@inheritdoc}
	 *
	 * @author Alexander Kovalev <alex.kovalevv@gmail.com>
	 * @since  2.1.2
	 * @var bool
	 */
	public $available_for_multisite = true;

	/**
	 * @author Alexander Kovalev <alex.kovalevv@gmail.com>
	 * @since  2.1.2
	 * @var string
	 */
	public $plugin_name;

	/**
	 * @var string Name of the paid plan.
	 */
	public $plan_name;

	// PREMIUM SECTION
	// ------------------------------------------------------------------
	/**
	 * @since 2.0.7
	 * @var bool
	 */
	protected $is_premium;

	/**
	 * @since 2.0.7
	 * @var \WBCR\Factory_427\Premium\Provider
	 */
	protected $premium;

	/**
	 * @since 2.0.7
	 * @var bool
	 */
	protected $is_premium_active;

	/**
	 * @since 2.0.7
	 * @var bool
	 */
	protected $premium_has_subscription;

	/**
	 * @since 2.0.7
	 * @var \WBCR\Factory_427\Premium\Interfaces\License
	 */
	protected $premium_license;

	// END PREMIUM SECTION
	// ------------------------------------------------------------------

	/**
	 * {@inheritdoc}
	 * @param Wbcr_Factory427_Plugin $plugin
	 */
	public function __construct(Wbcr_Factory427_Plugin $plugin)
	{
		$this->plugin = $plugin;

		parent::__construct($plugin);

		if( !$this->id ) {
			$this->id = $this->plugin->getPrefix() . 'license';
		}

		$this->plugin_name = $this->plugin->getPluginName();
		$this->premium = $plugin->premium;
		$this->is_premium = $this->premium->is_activate();
		$this->is_premium_active = $this->premium->is_active();
		$this->premium_has_subscription = $this->premium->has_paid_subscription();
		$this->premium_license = $this->premium->get_license();

		add_action("wp_ajax_wbcr-clearfy-activate-license-for-{$this->plugin_name}", [$this, 'ajax_handler']);
	}

	/**
	 * [MAGIC] Magic method that configures assets for a page.
	 */
	public function assets($scripts, $styles)
	{
		parent::assets($scripts, $styles);

		$this->styles->add(FACTORY_CLEARFY_219_URL . '/assets/css/license-manager.css');
		$this->scripts->add(FACTORY_CLEARFY_219_URL . '/assets/js/license-manager.js');
	}

	/**
	 * Регистрируем ajax обработчик для текущей страницы
	 *
	 * @since 2.0.7
	 */
	public function ajax_handler()
	{
		wbcr_factory_clearfy_219_check_license($this->plugin);
	}

	/**
	 * {@inheritdoc}
	 */
	public function showPageContent()
	{
		?>
		<div id="wcl-license-wrapper"
		     data-loader="<?php echo FACTORY_CLEARFY_219_URL . '/assets/img/loader.gif'; ?>"
		     data-plugin-name="<?php echo esc_attr($this->plugin_name); ?>"
		     data-nonce="<?php echo wp_create_nonce("clearfy_activate_license_for_{$this->plugin_name}") ?>">
			<?php $this->show_license_form(); ?>
		</div>
		<?php
	}

	/**
	 * Get before content.
	 *
	 * @return string Before content.
	 */
	protected function get_plan_description()
	{
		return '';
	}

	/**
	 * @return string
	 */
	protected function get_hidden_license_key()
	{
		if( !$this->is_premium ) {
			return '';
		}

		return $this->premium_license->get_hidden_key();
	}

	/**
	 * @return string
	 */
	protected function get_plan()
	{
		if( !$this->is_premium ) {
			return 'free';
		}

		return $this->premium->get_plan();
	}

	/**
	 * @return mixed
	 */
	protected function get_expiration_days()
	{
		if( !$this->is_premium ) {
			return '';
		}

		return $this->premium_license->get_expiration_time('days');
	}

	/**
	 * @return string
	 */
	protected function get_billing_cycle_readable()
	{
		if( !$this->is_premium ) {
			return '';
		}

		$billing_cycle = $this->premium->get_billing_cycle();
		$billing = 'lifetime';

		if( 1 == $billing_cycle ) {
			$billing = 'month';
		} else if( 12 == $billing_cycle ) {
			$billing = 'year';
		}

		return $billing;
	}

	/**
	 * Тип лицензии, цветовое оформление для формы лицензирования
	 * free - бесплатная
	 * gift - пожизненная лицензия, лицензия на особых условиях
	 * trial - красный цвет, применяется для триалов, если лиценизия истекла или заблокирована
	 * paid - обычная оплаченная лицензия, в данный момент активна.
	 *
	 * @return string
	 */
	protected function get_license_type()
	{
		if( !$this->is_premium ) {
			return 'paid';
		}

		$license = $this->premium_license;

		if( $license->is_lifetime() ) {
			return 'gift';
		} else if( $license->get_expiration_time('days') < 1 ) {
			return 'gift';
		}

		return 'paid';
	}

	protected function render_learnmore_section()
	{
		if( $this->is_premium ):
			?>
			<p style="margin-top: 10px;">
				<?php printf(__('<a href="%s" target="_blank" rel="noopener">Lean more</a> about the premium version and get the license key to activate it now!', 'wbcr_factory_clearfy_219'), $this->plugin->get_support()->get_pricing_url(true, 'license_page')); ?>
			</p>
		<?php else: ?>
			<p style="margin-top: 10px;">
				<?php printf(__('Can’t find your key? Go to <a href="%s" target="_blank" rel="noopener">this page</a> and login using the e-mail address associated with your purchase.', 'wbcr_factory_clearfy_219'), $this->plugin->get_support()->get_contacts_url(true, 'license_page')) ?>
			</p>
		<?php endif;
	}

	/**
	 * @param bool|WP_Error $notice
	 */
	public function show_license_form($notice = false)
	{

	}
}
