<?php
/**
 * License Info View (when license is valid)
 *
 * @package WP_PowerSuite
 * @since 1.0.0
 *
 * @var WPPS_License_Manager $manager
 */

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

use WP_PowerSuite\Core\WhiteLabel;

$r           = $manager->get_response_obj();
$product_url = WPPS_License_Manager::get_product_url();
$renew_url   = WPPS_License_Manager::get_renew_url();
$support_url = WPPS_License_Manager::get_support_url();
$status      = $manager->get_status_data();
$status_key  = sanitize_key( $status['status'] ?? 'active' );
$checked_at  = ! empty( $status['checked_at'] ) ? wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), (int) $status['checked_at'] ) : __( 'Not checked yet', 'wp-powersuite' );
$notice         = isset( $_GET['wpps_license_notice'] ) ? sanitize_key( wp_unslash( $_GET['wpps_license_notice'] ) ) : '';
$is_entitled_status = in_array( $status_key, [ 'active', 'grace', 'expired' ], true );
$updates_current    = function_exists( 'wp_powersuite_is_license_current' ) && wp_powersuite_is_license_current();
$admin_post_url = \WP_PowerSuite\Core\NetworkScope::adminUrl( 'admin-post.php' );
$pending        = $manager->get_page_pending_update();
$email_mismatch = $manager->get_license_email_mismatch_warning();
$expiry_reminder = class_exists( 'WPPS_License_Admin', false ) ? WPPS_License_Admin::get_expiry_reminder() : null;
$status_labels = [
	'active'           => __( 'Active', 'wp-powersuite' ),
	'inactive'         => __( 'Inactive', 'wp-powersuite' ),
	'expired'          => __( 'Expired', 'wp-powersuite' ),
	'invalid'          => __( 'Invalid', 'wp-powersuite' ),
	'connection_issue' => __( 'Connection issue', 'wp-powersuite' ),
	'grace'            => __( 'Grace period', 'wp-powersuite' ),
	'unknown'          => __( 'Unknown', 'wp-powersuite' ),
];
$vendor_label = $manager->get_vendor_site_status_label();

if ( ! $r ) {
	return;
}
?>
<div class="wpps-license-page">
	<div class="el-license-container">
		<div class="el-license-content">
		<?php if ( $notice === 'refreshed' ) : ?>
			<div class="wpps-license-alert wpps-license-alert--success"><p><?php esc_html_e( 'License status refreshed.', 'wp-powersuite' ); ?></p></div>
		<?php elseif ( $notice === 'updates_checked' ) : ?>
			<div class="wpps-license-alert wpps-license-alert--success"><p><?php esc_html_e( 'Update check completed.', 'wp-powersuite' ); ?></p></div>
		<?php elseif ( $notice === 'activated' && $is_entitled_status ) : ?>
			<div class="wpps-license-alert wpps-license-alert--success"><p><?php esc_html_e( 'License activated successfully.', 'wp-powersuite' ); ?></p></div>
		<?php elseif ( $notice === 'failed' || ( $notice === 'activated' && ! $is_entitled_status ) ) : ?>
			<div class="wpps-license-alert wpps-license-alert--error"><p><?php echo esc_html( ! empty( $status['message'] ) ? (string) $status['message'] : __( 'License could not be activated. Check your key and email, then try again.', 'wp-powersuite' ) ); ?></p></div>
		<?php endif; ?>
		<?php if ( $email_mismatch !== '' ) : ?>
			<div class="wpps-license-alert wpps-license-alert--warning"><p><?php echo esc_html( $email_mismatch ); ?></p></div>
		<?php endif; ?>
		<?php if ( is_array( $expiry_reminder ) ) : ?>
			<div class="wpps-license-alert wpps-license-alert--warning">
				<p>
					<?php echo esc_html( (string) $expiry_reminder['message'] ); ?>
					<?php if ( ! empty( $expiry_reminder['renew_url'] ) ) : ?>
						<a href="<?php echo esc_url( (string) $expiry_reminder['renew_url'] ); ?>" target="_blank" rel="noopener noreferrer"><?php esc_html_e( 'Renew license', 'wp-powersuite' ); ?></a>
					<?php endif; ?>
				</p>
			</div>
		<?php endif; ?>
		<?php
		$update_partial = defined( 'WP_POWERSUITE_PLUGIN_DIR' )
			? WP_POWERSUITE_PLUGIN_DIR . 'src/Admin/Views/partials/license-update-available.php'
			: dirname( __DIR__, 3 ) . '/src/Admin/Views/partials/license-update-available.php';
		if ( is_readable( $update_partial ) ) {
			include $update_partial;
		}
		?>
		<div class="wpps-license-status-card wpps-license-status-card--<?php echo esc_attr( $status_key ); ?>">
			<div>
				<span class="wpps-license-eyebrow"><?php esc_html_e( 'Current status', 'wp-powersuite' ); ?></span>
				<strong class="wpps-license-status-badge"><?php echo esc_html( $status_labels[ $status_key ] ?? $status_labels['unknown'] ); ?></strong>
				<?php if ( $vendor_label !== '' ) : ?>
					<span class="wpps-license-status-badge wpps-license-status-badge--vendor"><?php echo esc_html( $vendor_label ); ?></span>
				<?php endif; ?>
			</div>
			<div class="wpps-license-status-meta">
				<span><?php esc_html_e( 'Licensed email', 'wp-powersuite' ); ?>: <?php echo esc_html( $manager->get_license_email() ); ?></span>
				<span><?php esc_html_e( 'Site', 'wp-powersuite' ); ?>: <?php echo esc_html( home_url( '/' ) ); ?></span>
				<span><?php esc_html_e( 'Site hash', 'wp-powersuite' ); ?>: <code><?php echo esc_html( $manager->get_site_hash() ); ?></code></span>
				<span><?php esc_html_e( 'Last checked', 'wp-powersuite' ); ?>: <?php echo esc_html( $checked_at ); ?></span>
			</div>
			<?php if ( ! empty( $status['message'] ) && ! in_array( $status_key, [ 'active' ], true ) ) : ?>
				<p class="wpps-license-status-message wpps-license-status-message--<?php echo esc_attr( $status_key ); ?>"><?php echo esc_html( $status['message'] ); ?></p>
			<?php endif; ?>
			<p class="wpps-license-status-message wpps-license-status-message--updates">
				<?php if ( $updates_current ) : ?>
					<?php esc_html_e( 'Updates and support: included with your active license.', 'wp-powersuite' ); ?>
				<?php elseif ( is_array( $pending ) ) : ?>
					<?php esc_html_e( 'A newer version is available, but your license must be active to download it.', 'wp-powersuite' ); ?>
				<?php else : ?>
					<?php esc_html_e( 'Updates and support: renew your license to receive new versions.', 'wp-powersuite' ); ?>
				<?php endif; ?>
			</p>
		</div>
		<ul class="el-license-info">
			<?php if ( ! empty( $r->license_title ) ) : ?>
				<li>
					<div>
						<span class="el-license-info-title"><?php esc_html_e( 'License Type', 'wp-powersuite' ); ?></span>
						<?php echo esc_html( $r->license_title ); ?>
					</div>
				</li>
			<?php endif; ?>
			<?php if ( isset( $r->expire_date ) ) : ?>
				<li>
					<div>
						<span class="el-license-info-title"><?php esc_html_e( 'Expires', 'wp-powersuite' ); ?></span>
						<?php echo esc_html( $r->expire_date ); ?>
						<?php if ( ! empty( $r->expire_renew_link ) ) : ?>
							<a target="_blank" rel="noopener noreferrer" class="button button-small" href="<?php echo esc_url( $r->expire_renew_link ); ?>"><?php esc_html_e( 'Renew', 'wp-powersuite' ); ?></a>
						<?php endif; ?>
					</div>
				</li>
			<?php endif; ?>
			<?php if ( isset( $r->support_end ) ) : ?>
				<li>
					<div>
						<span class="el-license-info-title"><?php esc_html_e( 'Support Until', 'wp-powersuite' ); ?></span>
						<?php echo esc_html( $r->support_end ); ?>
						<?php if ( ! empty( $r->support_renew_link ) ) : ?>
							<a target="_blank" rel="noopener noreferrer" class="button button-small" href="<?php echo esc_url( $r->support_renew_link ); ?>"><?php esc_html_e( 'Renew', 'wp-powersuite' ); ?></a>
						<?php endif; ?>
					</div>
				</li>
			<?php endif; ?>
			<?php if ( $manager->get_masked_license_key() !== '' ) : ?>
				<li>
					<div>
						<span class="el-license-info-title"><?php esc_html_e( 'License Key', 'wp-powersuite' ); ?></span>
				<span class="el-license-key"><?php echo esc_html( $manager->get_masked_license_key() ); ?></span>
					</div>
				</li>
			<?php endif; ?>
		</ul>
		<div class="el-license-active-btn wpps-license-actions">
			<form method="post" action="<?php echo esc_url( $admin_post_url ); ?>">
				<input type="hidden" name="action" value="WP_PowerSuite_el_refresh_license"/>
				<?php wp_nonce_field( 'el-license' ); ?>
				<button type="submit" class="button button-primary">
					<span class="dashicons dashicons-update" aria-hidden="true"></span>
					<?php esc_html_e( 'Refresh / Check License', 'wp-powersuite' ); ?>
				</button>
			</form>
			<?php if ( ! is_array( $pending ) ) : ?>
				<a class="button button-secondary" href="<?php echo esc_url( WPPS_License_Manager::get_check_updates_url() ); ?>">
					<?php esc_html_e( 'Check for updates', 'wp-powersuite' ); ?>
				</a>
			<?php endif; ?>
			<form method="post" action="<?php echo esc_url( $admin_post_url ); ?>">
				<input type="hidden" name="action" value="WP_PowerSuite_el_deactivate_license"/>
				<?php wp_nonce_field( 'el-license' ); ?>
				<?php submit_button( __( 'Deactivate License', 'wp-powersuite' ), 'secondary', 'submit', false ); ?>
			</form>
		</div>

		<?php if ( ! WhiteLabel::shouldHideExternalLinks() ) : ?>
		<div class="el-license-quick-links">
			<p class="el-license-quick-links__heading"><?php esc_html_e( 'Quick links', 'wp-powersuite' ); ?></p>
			<div class="el-license-quick-links__grid">
				<?php if ( $renew_url !== '' ) : ?>
				<a href="<?php echo esc_url( $renew_url ); ?>" target="_blank" rel="noopener noreferrer" class="button button-secondary">
					<span class="dashicons dashicons-update" aria-hidden="true"></span>
					<?php esc_html_e( 'Renew License', 'wp-powersuite' ); ?>
				</a>
				<?php endif; ?>
				<?php if ( $support_url !== '' ) : ?>
				<a href="<?php echo esc_url( $support_url ); ?>" target="_blank" rel="noopener noreferrer" class="button button-secondary">
					<span class="dashicons dashicons-sos" aria-hidden="true"></span>
					<?php esc_html_e( 'Get Support', 'wp-powersuite' ); ?>
				</a>
				<?php endif; ?>
				<?php if ( $product_url !== '' ) : ?>
				<a href="<?php echo esc_url( $product_url ); ?>" target="_blank" rel="noopener noreferrer" class="button button-secondary">
					<span class="dashicons dashicons-external" aria-hidden="true"></span>
					<?php esc_html_e( 'Product Page', 'wp-powersuite' ); ?>
				</a>
				<?php endif; ?>
			</div>
		</div>
		<?php endif; ?>
		</div>
	</div>
</div>
