<?php

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

if ( ! $order || ! is_a( $order, 'WC_Order' ) ) {
    return;
}

/**
 * Re-checked on every render of this page.
 *
 * An order can sit on the payment screen for the length of the payment window,
 * and the buyer may refresh it repeatedly while they move funds. The licence
 * can lapse inside that window, and the API stops forwarding notifications the
 * moment it does — so continuing to show a wallet address here would be
 * inviting someone to send crypto that nothing will ever confirm.
 */
if ( function_exists( 'cryptoniq_license_is_blocked' ) && cryptoniq_license_is_blocked() ) {
    return wc_print_notice( cryptoniq_license_notice(), 'error' );
}

$order_id  = $order->get_id();
$timestamp = $order->get_meta( 'cryptoniq_time_start' );
$wallet    = $order->get_meta( 'cryptoniq_coin_wallet' );
$amount    = $order->get_meta( 'cryptoniq_coin_amount' );
$coin      = $order->get_meta( 'cryptoniq_coin_ticker' );
$coin_name = cryptoniq_get_coin_data( $coin, 'name' );

// cryptoniq_get_payment_coins() already filters to what the current
// license allows (no license = BTC + ETH only; with license = everything).
// So a coin that passes this check is, by definition, payable right now —
// no separate license-key gate is needed here. BTC/ETH are detected via
// public block explorers; the rest via the Cryptoniq API webhook.
if ( ! $coin || ! array_key_exists( $coin, cryptoniq_get_payment_coins() ) ) {
	$message = esc_html( 'No payment coin', 'cryptoniq-pro' );
	return wc_print_notice( $message, 'error' );
}

if ( ! $wallet ) {
	$message = esc_html( 'No wallet for', 'cryptoniq-pro' );
	return wc_print_notice( $message . ' ' . $coin_name, 'error' );
}

if ( ! $amount ) {
	$message = esc_html( 'Invalid amount', 'cryptoniq-pro' );
	return wc_print_notice( $message, 'error' );
}

if ( $order->get_status() == 'failed' ) {
	$message = esc_html( 'Payment failed', 'cryptoniq-pro' );
	return wc_print_notice( $message, 'error' );
}

?>

<div class="cryptoniq-paybox-layer cryptoniq-paybox-layer-mask cryptoniq-clearfix">
	<div id="cryptoniq-paybox" class="cryptoniq-paybox cryptoniq-border-box cryptoniq-clearfix">
		<div class="cryptoniq-paybox-topbar">
			<div class="cryptoniq-paybox-topbar-wrapper cryptoniq-clearfix">
				<div class="cryptoniq-paybox-heading"><span class="cryptoniq-paybox-heading-pre"><?php esc_html_e( 'Payment', 'cryptoniq-pro' ); ?></span>: <?php echo esc_html( $coin_name ); ?></div>
			</div>
		</div>
		<div class="cryptoniq-paybox-container">
			<div class="cryptoniq-paybox-section-wrapper">
				<div class="cryptoniq-paybox-section-content">
					<div class="cryptoniq-paybox-data">
						<div class="cryptoniq-paybox-section-title cryptoniq-clearfix">
							<div class="cryptoniq-paybox-section-title-left cryptoniq-paybox-section-title-mask"><?php esc_html_e( 'How to pay', 'cryptoniq-pro' ); ?></div>
							<div id="cryptoniq-paybox-tooltip-button" class="cryptoniq-paybox-section-title-right"><i></i></div>
						</div>
						<div class="cryptoniq-paybox-order-details">
							<ul>
								<li>
									<div class="cryptoniq-paybox-order-title"><?php esc_html_e( 'Please, send', 'cryptoniq-pro' ); ?> <i id="cryptoniq-paybox-address-copy" data-clipboard-target="#cryptoniq-paybox-amount"></i></div>
									<div class="cryptoniq-paybox-order-text">
										<span class="cryptoniq-paybox-order-text-mask"><?php echo esc_html( $coin ); ?>: <span id="cryptoniq-paybox-amount"><?php echo esc_html( $amount ); ?></span></span><span class="cryptoniq-paybox-order-price-fiat"><?php echo wp_kses_post( $order->get_formatted_order_total() ); ?></span>
									</div>
								</li>
								<li>
									<div class="cryptoniq-paybox-order-title"><?php esc_html_e( 'To this address', 'cryptoniq-pro' ); ?> <i id="cryptoniq-paybox-address-copy" data-clipboard-target="#cryptoniq-paybox-address"></i></div>
									<div class="cryptoniq-paybox-order-text cryptoniq-paybox-order-text-address">
										<span id="cryptoniq-paybox-address" class="cryptoniq-paybox-order-text-mask"><?php echo esc_html( $wallet ); ?></span>
									</div>
								</li>
								<li>
									<div class="cryptoniq-paybox-order-title"><?php esc_html_e( 'Scan QR code', 'cryptoniq-pro' ); ?></div>
									<div id="cryptoniq-paybox-qrcode" class="cryptoniq-paybox-order-text cryptoniq-paybox-qrcode"></div>
								</li>
							</ul>
						</div>
						<div class="cryptoniq-paybox-instructions">
							<div class="cryptoniq-paybox-instructions-container">
								<div class="cryptoniq-paybox-instructions-topbar cryptoniq-clearfix">
									<div class="cryptoniq-paybox-instructions-title"><?php esc_html_e( 'Instruction', 'cryptoniq-pro' ); ?></div>
									<div id="cryptoniq-paybox-instructions-close"><i></i></div>
								</div>
								<div class="cryptoniq-paybox-instructions-content">
									<p><?php esc_html_e( 'To make a payment, send required amount of coins to mentioned address. It must be as one transaction (not included the fees).', 'cryptoniq-pro' ); ?></p>
									<p><?php esc_html_e( 'System will give you 3 hours, in which time the transaction must be completed. So if you haven\'t sent the coins before clicking the check button, you still can. But note that if the coins are not received within a given time, order will be automatically cancelled and payment will be failed.', 'cryptoniq-pro' ); ?></p>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<div class="cryptoniq-paybox-bottombar">
			<div class="cryptoniq-paybox-bottombar-wrapper">
				<div id="cryptoniq-paybox-tx-bar" class="cryptoniq-paybox-tx-form" data-id="<?php echo esc_attr( $order_id ); ?>" data-coin="<?php echo esc_attr( $coin ); ?>" data-time="<?php echo esc_attr( $timestamp ); ?>">
					<div id="cryptoniq-paybox-tx-checker" class="cryptoniq-paybox-tx-check-bar">
						<div class="cryptoniq-paybox-tx-content">
							<div class="cryptoniq-paybox-tx-info">
								<div class="cryptoniq-paybox-line-st-time">
									<span id="cryptoniq-paybox-loader" class="cryptoniq-paybox-loader"></span><span id="cryptoniq-paybox-alert" class="cryptoniq-paybox-alert"><?php esc_html_e( 'Checking...', 'cryptoniq-pro' ); ?></span><span class="cryptoniq-paybox-tx-counter-text"><?php esc_html_e( 'Time:', 'cryptoniq-pro' ); ?></span> <span id="cryptoniq-paybox-tx-counter">00:00:00</span>
								</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>