<?php
/**
 * Currency Switcher Template for Startbutton Integration
 *
 * @var WC_Gateway_Startbutton $gateway
 */

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

// Get gateway instance
$gateway = WC()->payment_gateways()->payment_gateways()['startbutton'] ?? null;

if (!$gateway || !$gateway->is_available() || $gateway->get_option('enable_currency_switcher') !== 'yes') {
    return;
}

$currencies = $gateway->get_available_currencies();
$current_currency = $gateway->get_current_currency();
$current_symbol = $gateway->get_currency_symbol($current_currency);
$current_currency_name = $gateway->get_currency_display_name($current_currency);
$exchange_rates = $gateway->get_exchange_rates();
$last_fx_update = $gateway->get_last_fx_update_time();
?>

<div class="startbutton-currency-switcher" id="startbutton-currency-switcher">
    <!-- Small Trigger Button (Always Visible) -->
    <div class="currency-trigger-button" id="currency-trigger-button" 
         role="button" 
         tabindex="0"
         aria-expanded="false"
         aria-label="<?php echo esc_attr(sprintf(__('Open currency switcher. Current currency: %s', 'woo-startbutton'), $current_currency)); ?>"
         aria-describedby="currency-trigger-description">
        <div class="trigger-icon">
            <span class="currency-symbol"><?php echo esc_html($current_currency_name) . ' ' . esc_html($current_symbol); ?></span>
        </div>
        <div class="trigger-tooltip">
            <span><?php echo esc_html(__('Switch Currency', 'woo-startbutton')); ?></span>
        </div>
        <div id="currency-trigger-description" class="sr-only">
            <?php echo esc_html(__('Click to open currency selection menu', 'woo-startbutton')); ?>
        </div>
    </div>
    <!-- Main Currency Switcher (Hidden by default, slides in) -->
    <div class="currency-switcher-container" id="currency-switcher-container">
        <!-- Current Currency Display -->
        <div class="current-currency" id="currency-trigger">
            <div class="currency-icon">
                <span class="currency-symbol"><?php echo esc_html($current_symbol); ?></span>
            </div>
            <div class="currency-info">
                <span class="currency-code"><?php echo esc_html($current_currency); ?></span>
                <span class="currency-name"><?php echo esc_html($current_currency_name); ?></span>
            </div>
            <div class="arrow-icon">
                <svg width="12" height="8" viewBox="0 0 12 8" fill="none">
                    <path d="M1 1.5L6 6.5L11 1.5" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"/>
                </svg>
            </div>
        </div>

        <!-- Currency Options Dropdown -->
        <div class="currency-options" id="currency-dropdown">
            <div class="currency-header">
                <h4><?php echo esc_html(__('Select Currency', 'woo-startbutton')); ?></h4>
                <?php if ($last_fx_update): ?>
                    <span class="fx-update-info">
                        <?php echo esc_html(sprintf(__('Rates updated: %s', 'woo-startbutton'), $last_fx_update)); ?>
                    </span>
                <?php endif; ?>
            </div>
            
            <div class="currency-list">
                <?php foreach ($currencies as $code => $name): ?>
                    <div class="currency-option <?php echo $code === $current_currency ? 'active' : ''; ?>" 
                         data-currency="<?php echo esc_attr($code); ?>"
                         data-post='<?php echo wp_json_encode($gateway->get_switch_currency_post_data($code)); ?>'>
                        <div class="currency-option-content">
                            <span class="currency-symbol"><?php echo esc_html($gateway->get_currency_symbol($code)); ?></span>
                            <div class="currency-details">
                                <span class="currency-code"><?php echo esc_html($code); ?></span>
                                <span class="currency-name"><?php echo esc_html($name); ?></span>
                                <?php if ($code !== $current_currency && isset($exchange_rates[$code])): ?>
                                    <span class="exchange-rate">
                                        <!-- Rate: <?php echo esc_html(number_format($exchange_rates[$code]['rate'], 4)); ?> -->
                                    </span>
                                <?php endif; ?>
                            </div>
                            <?php if ($code === $current_currency): ?>
                                <div class="check-icon">
                                    <svg width="16" height="16" viewBox="0 0 16 16" fill="none">
                                        <path d="M13.5 4.5L6 12L2.5 8.5" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                                    </svg>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                <?php endforeach; ?>
            </div>

            <div class="currency-footer">
                <span class="powered-by">
                    <?php echo esc_html(__('Powered by Startbutton', 'woo-startbutton')); ?>
                </span>
            </div>
        </div>
    </div>
    <!-- Overlay -->
    <div class="currency-overlay" id="currency-overlay"></div>
</div> 