<?php
/**
 * PayStarSea Payment Gateway Blocks Support
 * 
 * WooCommerce Blocks integration for PayStarSea payment gateway
 */

use Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;

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

final class SeaXPay_Blocks_Support extends AbstractPaymentMethodType {

    private $gateway;

    protected $name = 'seaxpay';

    public function initialize() {
        $this->settings = get_option('woocommerce_seaxpay_settings', array());
        $gateways = WC()->payment_gateways->payment_gateways();
        if (isset($gateways[$this->name])) {
            $this->gateway = $gateways[$this->name];
        }
    }

    public function is_active() {
        if (!$this->gateway) {
            return false;
        }
        return $this->gateway->is_available();
    }

    public function get_payment_method_script_handles() {
        $script_path = 'assets/js/blocks/seaxpay-blocks.js';
        $script_asset_path = SEAXPAY_PLUGIN_DIR . 'assets/js/blocks/seaxpay-blocks.asset.php';
        
        $script_asset = file_exists($script_asset_path)
            ? require $script_asset_path
            : array(
                'dependencies' => array(),
                'version' => SEAXPAY_VERSION,
            );

        $script_url = SEAXPAY_PLUGIN_URL . $script_path;

        $script_handle = 'seaxpay-payments-blocks';

        wp_register_script(
            $script_handle,
            $script_url,
            $script_asset['dependencies'],
            $script_asset['version'],
            true
        );

        if (function_exists('wp_set_script_translations')) {
            wp_set_script_translations($script_handle, 'paystarsea-crypto-payment-gateway', SEAXPAY_PLUGIN_DIR . 'languages/');
        }

        return array($script_handle);
    }

    public function get_payment_method_data() {
        $supported_coins = seaxpay_get_supported_coins();
        
        $default_coin = $this->get_setting('default_coin', 'USDT');
        $default_network = $this->get_setting('default_network', 'TRC20');
        
        $cart_total = 0;
        $cart_currency = 'USD';
        if (WC()->cart) {
            $cart_total = WC()->cart->get_total('edit');
            $cart_currency = get_woocommerce_currency();
        }
        
        return array(
            'title' => $this->get_setting('title', __('Cryptocurrency Payment', 'paystarsea-crypto-payment-gateway')),
            'description' => $this->get_setting('description', __('Pay with USDT, BTC, ETH and other cryptocurrencies', 'paystarsea-crypto-payment-gateway')),
            'supports' => $this->gateway ? array_filter($this->gateway->supports, array($this->gateway, 'supports')) : array('products'),
            'supported_coins' => $supported_coins,
            'default_coin' => $default_coin,
            'default_network' => $default_network,
            'ajax_url' => admin_url('admin-ajax.php'),
            'nonce' => wp_create_nonce('seaxpay_blocks'),
            'cart_total' => floatval($cart_total),
            'cart_currency' => $cart_currency,
            'i18n' => array(
                'select_coin' => __('Select Cryptocurrency', 'paystarsea-crypto-payment-gateway'),
                'select_network' => __('Select Network', 'paystarsea-crypto-payment-gateway'),
                'pay_with_crypto' => __('Pay with Cryptocurrency', 'paystarsea-crypto-payment-gateway'),
                'loading' => __('Loading...', 'paystarsea-crypto-payment-gateway'),
                'error' => __('Error loading quote', 'paystarsea-crypto-payment-gateway'),
            ),
        );
    }
}
