<?php

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

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound, WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
$platform_address = $seaxpay_order ? $seaxpay_order->customer_address : $order->get_meta('_seaxpay_platform_address');
$crypto_amount = $seaxpay_order ? $seaxpay_order->crypto_amount : $order->get_meta('_seaxpay_crypto_amount');
$selected_coin = $seaxpay_order ? $seaxpay_order->coin : $order->get_meta('_seaxpay_coin');
$selected_network = $seaxpay_order ? $seaxpay_order->network : $order->get_meta('_seaxpay_network');
$exchange_rate = $seaxpay_order ? $seaxpay_order->exchange_rate : '';
$expire_time = $seaxpay_order ? $seaxpay_order->expire_time : $order->get_meta('_seaxpay_expire_time');
$platform_order_no = $seaxpay_order ? $seaxpay_order->platform_order_no : $order->get_meta('_seaxpay_platform_order_no');
$address_mode = $seaxpay_order ? ($seaxpay_order->address_mode ?? 'fingerprint') : ($order->get_meta('_seaxpay_address_mode') ?: 'fingerprint');
$is_hd_mode = ($address_mode === 'hd');

$crypto_amount_display = seaxpay_format_crypto_amount_for_display($crypto_amount);

$is_expired = false;
if ($seaxpay_order && $seaxpay_order->status === 'expired') {
    $is_expired = true;
} elseif ($expire_time) {
    $expire_timestamp = strtotime($expire_time . ' UTC');
    $current_utc_timestamp = time();
    $is_expired = ($expire_timestamp !== false && $expire_timestamp < $current_utc_timestamp);
}
// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound, WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound
?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
    <meta charset="<?php bloginfo('charset'); ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php esc_html_e('Cryptocurrency Payment', 'paystarsea-crypto-payment-gateway'); ?> - <?php bloginfo('name'); ?></title>
    <?php wp_head(); ?>
</head>
<body class="seaxpay-payment-body">

<div class="seaxpay-container">
    <div class="seaxpay-header">
        <h1><?php esc_html_e('Cryptocurrency Payment', 'paystarsea-crypto-payment-gateway'); ?></h1>
        <p><?php
        /* translators: %1$s: order number, %2$s: order total, %3$s: currency code */
        printf(esc_html__('Order #%1$s - %2$s %3$s', 'paystarsea-crypto-payment-gateway'), esc_html($order->get_order_number()), esc_html($order->get_total()), esc_html($order->get_currency())); ?></p>
    </div>

    <?php if (!$seaxpay_order && !$platform_address): ?>
        <div class="seaxpay-error">
            <p><?php esc_html_e('Payment order not found. Please return to checkout and try again.', 'paystarsea-crypto-payment-gateway'); ?></p>
            <a href="<?php echo esc_url(wc_get_checkout_url()); ?>"><?php esc_html_e('Return to Checkout', 'paystarsea-crypto-payment-gateway'); ?></a>
        </div>
    <?php elseif ($is_expired): ?>
        <div class="seaxpay-expired">
            <div class="seaxpay-expired-icon">⏰</div>
            <h2><?php esc_html_e('Payment Expired', 'paystarsea-crypto-payment-gateway'); ?></h2>
            <p><?php esc_html_e('This payment order has expired. For security reasons, the payment address is no longer available.', 'paystarsea-crypto-payment-gateway'); ?></p>
            <div class="order-info">
                <p><strong><?php esc_html_e('Order Number:', 'paystarsea-crypto-payment-gateway'); ?></strong> #<?php echo esc_html($order->get_order_number()); ?></p>
                <p><strong><?php esc_html_e('Order Amount:', 'paystarsea-crypto-payment-gateway'); ?></strong> <?php echo esc_html($order->get_total() . ' ' . $order->get_currency()); ?></p>
                <p><strong><?php esc_html_e('Selected Payment:', 'paystarsea-crypto-payment-gateway'); ?></strong> <?php echo esc_html($selected_coin . ' - ' . $selected_network); ?></p>
                <p><strong><?php esc_html_e('Expired At:', 'paystarsea-crypto-payment-gateway'); ?></strong> <?php echo esc_html($expire_time); ?></p>
            </div>
            <p><?php esc_html_e('Please return to checkout and create a new order to continue your purchase.', 'paystarsea-crypto-payment-gateway'); ?></p>
            <a href="<?php echo esc_url(wc_get_checkout_url()); ?>"><?php esc_html_e('Return to Checkout', 'paystarsea-crypto-payment-gateway'); ?></a>
        </div>
    <?php else: ?>
        <div class="seaxpay-coin-badge">
            <?php echo esc_html($selected_coin . ' - ' . $selected_network); ?>
        </div>

        <div class="seaxpay-qr">
            <h3><?php esc_html_e('Scan QR Code to Pay', 'paystarsea-crypto-payment-gateway'); ?></h3>
            <div class="seaxpay-qr-box" id="seaxpay-qrcode"></div>
        </div>

        <div class="seaxpay-field">
            <label><?php esc_html_e('Payment Address', 'paystarsea-crypto-payment-gateway'); ?></label>
            <div class="seaxpay-field-row">
                <input type="text" id="seaxpay-address" readonly value="<?php echo esc_attr($platform_address); ?>">
                <button type="button" class="seaxpay-copy-btn seaxpay-copy-field-btn" data-target="seaxpay-address"><?php esc_html_e('Copy', 'paystarsea-crypto-payment-gateway'); ?></button>
            </div>
        </div>

        <div class="seaxpay-field">
            <?php if ($is_hd_mode): ?>
            <label><?php esc_html_e('Amount to Pay', 'paystarsea-crypto-payment-gateway'); ?></label>
            <div class="seaxpay-field-row">
                <input type="text" id="seaxpay-amount" class="seaxpay-amount-hd" readonly value="<?php echo esc_attr($crypto_amount_display); ?>">
                <span class="seaxpay-coin-label"><?php echo esc_html($selected_coin); ?></span>
                <button type="button" class="seaxpay-copy-btn seaxpay-copy-amount"><?php esc_html_e('Copy Amount', 'paystarsea-crypto-payment-gateway'); ?></button>
            </div>
            <p class="seaxpay-field-hint">
                <?php esc_html_e('This is your dedicated payment address. Please send the amount shown above.', 'paystarsea-crypto-payment-gateway'); ?>
            </p>
            <?php else: ?>
            <label><?php esc_html_e('Exact Amount to Pay (Amount Fingerprint)', 'paystarsea-crypto-payment-gateway'); ?> <span class="seaxpay-required">*</span></label>
            <div class="seaxpay-field-row">
                <input type="text" id="seaxpay-amount" class="seaxpay-amount-fingerprint" readonly value="<?php echo esc_attr($crypto_amount_display); ?>">
                <span class="seaxpay-coin-label"><?php echo esc_html($selected_coin); ?></span>
                <button type="button" class="seaxpay-copy-btn seaxpay-copy-amount"><?php esc_html_e('Copy Amount', 'paystarsea-crypto-payment-gateway'); ?></button>
            </div>
            <p class="seaxpay-field-hint-warning">
                ⚠️ <?php esc_html_e('You MUST send this EXACT amount. The system identifies your payment by this unique amount.', 'paystarsea-crypto-payment-gateway'); ?>
            </p>
            <?php endif; ?>
        </div>

        <div class="seaxpay-timer">
            <span class="seaxpay-timer-label"><?php esc_html_e('Time Remaining', 'paystarsea-crypto-payment-gateway'); ?></span>
            <span class="seaxpay-countdown" id="seaxpay-countdown">--:--</span>
            <div class="seaxpay-status" id="seaxpay-status">🔄 <?php esc_html_e('Waiting for payment...', 'paystarsea-crypto-payment-gateway'); ?></div>
        </div>

        <div class="seaxpay-warnings">
            <h4><?php esc_html_e('Important', 'paystarsea-crypto-payment-gateway'); ?></h4>
            <ul>
                <?php if ($is_hd_mode): ?>
                <li><strong><?php
                /* translators: %1$s: crypto amount, %2$s: coin symbol */
                printf(esc_html__('Send at least %1$s %2$s to the address above', 'paystarsea-crypto-payment-gateway'), esc_html($crypto_amount_display), esc_html($selected_coin)); ?></strong></li>
                <li><?php esc_html_e('Each order has a unique payment address', 'paystarsea-crypto-payment-gateway'); ?></li>
                <?php else: ?>
                <li><strong><?php
                /* translators: %1$s: crypto amount, %2$s: coin symbol */
                printf(esc_html__('Send exactly %1$s %2$s', 'paystarsea-crypto-payment-gateway'), esc_html($crypto_amount), esc_html($selected_coin)); ?></strong></li>
                <?php endif; ?>
                <li><?php esc_html_e('Payment expires in 30 minutes', 'paystarsea-crypto-payment-gateway'); ?></li>
                <li><?php
                /* translators: %1$s: coin symbol, %2$s: network name */
                printf(esc_html__('Only send %1$s on %2$s network', 'paystarsea-crypto-payment-gateway'), esc_html($selected_coin), esc_html($selected_network)); ?></li>
            </ul>
        </div>

        <?php if (!$is_hd_mode): ?>
        <div class="seaxpay-notice">
            <h4><?php esc_html_e('Amount Notice', 'paystarsea-crypto-payment-gateway'); ?></h4>
            <p><?php esc_html_e('Please send the exact amount. Insufficient payments will NOT be processed.', 'paystarsea-crypto-payment-gateway'); ?></p>
            <p class="warning"><?php esc_html_e('Sending less than required will result in payment rejection.', 'paystarsea-crypto-payment-gateway'); ?></p>
        </div>
        <?php endif; ?>

        <div class="seaxpay-actions">
            <button type="button" class="seaxpay-btn" id="seaxpay-check-btn"><?php esc_html_e('Check Payment Status', 'paystarsea-crypto-payment-gateway'); ?></button>
        </div>
    <?php endif; ?>
</div>

<?php wp_footer(); ?>
</body>
</html>
