<?php

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

final class HKDigital_Converse_Blocks_Support extends AbstractPaymentMethodType
{
    /**
     * Name/ID of the payment method (must match gateway id)
     * @var string
     */
    protected $name = 'hkd_converse';

    private $pluginDirUrl;

    public function __construct()
    {
        global $pluginDirUrlConverse;
        $this->pluginDirUrl = $pluginDirUrlConverse;
    }

    /**
     * Initialize settings for the payment method from WooCommerce options.
     */
    public function initialize()
    {
        $this->settings = get_option('woocommerce_hkd_converse_settings', []);
    }

    /**
     * Whether this payment method is active (controls script enqueue and visibility in Blocks).
     * @return bool
     */
    public function is_active()
    {
        if (!function_exists('WC')) {
            return false;
        }
        $gateways = WC()->payment_gateways();
        if (!isset($gateways) || !is_object($gateways)) {
            return false;
        }
        $payment_gateways = $gateways->payment_gateways();
        if (!isset($payment_gateways['hkd_converse'])) {
            return false;
        }
        return $payment_gateways['hkd_converse']->is_available();
    }

    /**
     * Return any scripts that should be enqueued for this payment method in the Blocks checkout/editor.
     * We keep it minimal for now (no custom fields), so no extra JS is required.
     * @return array
     */
    public function get_payment_method_script_handles()
    {
        $version      = '1.0.0';
        $dependencies = [
            'wc-blocks-registry',
            'wc-settings',
            'wp-element',
            'wp-html-entities',
            'wp-i18n',
        ];

        wp_register_script(
            'wc-hkd_converse-blocks-integration',
            $this->pluginDirUrl . 'assets/js/support.js',
            $dependencies,
            $version,
            true
        );
        // Provide translations for any strings in the integration script
        if (function_exists('wp_set_script_translations')) {
            wp_set_script_translations(
                'wc-hkd_converse-blocks-integration',
                'hk-payment-gateway-for-converse'
            );
        }
        return ['wc-hkd_converse-blocks-integration'];
    }

    /**
     * Data exposed to the payment method script (and Blocks frontend).
     * @return array
     */
    public function get_payment_method_data()
    {
        return [
            'title'       => $this->get_setting('title'),
            'description' => $this->get_setting('description'),
            'supports'    => $this->get_supported_features(),
            'logo_url'    => $this->pluginDirUrl . 'assets/images/logo_converse.png',
        ];
    }

    /**
     * Supported features mapping from the underlying gateway.
     * @return array
     */
    public function get_supported_features()
    {
        if (!function_exists('WC')) {
            return [];
        }
        $payment_gateways = WC()->payment_gateways->payment_gateways();
        return isset($payment_gateways['hkd_converse']) ? $payment_gateways['hkd_converse']->supports : [];
    }
}
