<?php

/**
 * View: admin/onboarding/active_license.
 *
 * @var ?MemberPress\GroundLevel\Support\Result $form_result
 */

use MemberPress\GroundLevel\Mothership\AbstractPluginConnection;
use MemberPress\GroundLevel\Mothership\Transients\ActivationTransient;

defined('ABSPATH') || exit;

$activation             = memberpress()->container()->get(ActivationTransient::class);
$expires_at_date        = !empty($activation->licenseExpiresAt) ? date_create($activation->licenseExpiresAt) : false;
$expires_at             = $expires_at_date ? MeprUtils::date('F j, Y', $expires_at_date) : null;
$license_activated_text = $expires_at
    // Translators: %s: expiration date.
    ? sprintf(__('License activated until %s', 'memberpress'), $expires_at)
    : __('License activated', 'memberpress');

// The Mothership API returns 999999999 for an unlimited number of activations.
$unlimited_activations = 999999999;
$prod_allowed_text     = $activation->prodActivationsAllowed >= $unlimited_activations
    ? __('Unlimited', 'memberpress')
    : number_format_i18n($activation->prodActivationsAllowed);
$test_allowed_text     = $activation->testActivationsAllowed >= $unlimited_activations
    ? __('Unlimited', 'memberpress')
    : number_format_i18n($activation->testActivationsAllowed);

$prod_activations_text = sprintf(
    // Translators: %1$s: open b tag, %2$d: activation count, %3$s: max activations, %4$s close b tag.
    __('%1$s%2$d of %3$s%4$s production sites have been activated with this license key', 'memberpress'),
    '<b>',
    $activation->prodActivationsUsed,
    $prod_allowed_text,
    '</b>'
);

$test_activations_text = sprintf(
    // Translators: %1$s: open b tag, %2$d: activation count, %3$s: max activations, %4$s close b tag.
    __('%1$s%2$d of %3$s%4$s staging sites have been activated with this license key', 'memberpress'),
    '<b>',
    $activation->testActivationsUsed,
    $test_allowed_text,
    '</b>'
);

$user_email_text = $activation->userEmail ? $activation->userEmail : __('Unknown', 'memberpress');

// Form.
$plugin_connection = memberpress()->container()->get(AbstractPluginConnection::class);
$plugin_id         = $plugin_connection->pluginId;
$form_name         = "{$plugin_id}_deactivate_license_form";
$form_button       = "{$plugin_id}_license_button";
?>

<div id="mepr-license-container" class="mepr-wizard-license-container">
    <div class="mepr-wizard-license">
        <div class="mepr-wizard-license-notice">
            <img
                src="<?php echo esc_url(MEPR_BRAND_URL . '/images/onboarding/green-check.svg'); ?>"
                alt="<?php esc_attr_e('License active', 'memberpress'); ?>"
            />
            <?php echo esc_html($license_activated_text); ?>
        </div>

        <div class="mepr-wizard-license-details">
            <div>
                <div class="mepr-wizard-license-label"><?php esc_html_e('Account email', 'memberpress'); ?></div>
                <div class="mepr-wizard-license-value"><?php echo esc_html($user_email_text); ?></div>
            </div>
            <div>
                <div class="mepr-wizard-license-label"><?php esc_html_e('Product', 'memberpress'); ?></div>
                <div class="mepr-wizard-license-value"><?php echo esc_html($activation->productName); ?></div>
            </div>
            <div>
                <div class="mepr-wizard-license-label"><?php esc_html_e('Activations', 'memberpress'); ?></div>
                <div class="mepr-wizard-license-value"><?php echo wp_kses_post($prod_activations_text); ?></div>
                <div class="mepr-wizard-license-value"><?php echo wp_kses_post($test_activations_text); ?></div>
            </div>
        </div>

        <div class="mepr-wizard-license-manage">
            <a href="<?php echo esc_url(MeprUtils::get_link_url('onboarding_manage_activations')); ?>" target="_blank">
                <?php esc_html_e('Manage activations', 'memberpress'); ?>
            </a>
        </div>

        <?php if ($form_result && $form_result->isFailure()) : ?>
            <div class="notice notice-error">
                <p><?php echo esc_html(sanitize_text_field(wp_unslash($form_result->getMessage()))); ?></p>
            </div>
        <?php endif; ?>

        <div class="mepr-wizard-license-deactivate">
            <form method="post" action="" name="<?php echo esc_attr($form_name); ?>">
                <?php wp_nonce_field('mothership_deactivate_license', '_wpnonce'); ?>
                <input type="hidden" name="hide_notice" value="1" />
                <input type="hidden" name="<?php echo esc_attr($form_button); ?>" value="deactivate">
                <button
                    type="submit"
                    id="mepr-deactivate-license-key"
                    class="mepr-wizard-button-secondary"
                >
                    <?php esc_html_e('Deactivate License', 'memberpress'); ?>
                </button>
            </form>
        </div>
    </div>
</div>
