<?php

// phpcs:disable WordPress.WP.I18n.TextDomainMismatch -- Bridge to pay4all-for-twint :
// this WooCommerce integration re-emits the TWINT gateway strings the free
// plugin already ships and translates. Reusing its text domain avoids
// duplicating dozens of msgids × 4 locales in pay4all-pro's .po files.

namespace Twnt4All\Blocks;

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

class BlocksRegistration extends AbstractPaymentMethodType
{
    /**
     * Payment method name defined by payment methods extending this class.
     *
     * @var string
     */
    protected $name = 't4a_twint';

    /**
     * Initializes the payment method type.
     */
    public function initialize()
    {
    }

    /**
     * Returns if this payment method should be active. If false, the scripts will not be enqueued.
     *
     * @return boolean
     */
    public function is_active()
    {
        global $woocommerce;

        $payment_gateways = $woocommerce->payment_gateways->payment_gateways();

        if ( isset( $payment_gateways[ $this->name ] ) ) {

            $gateway = $payment_gateways[ $this->name ];
            if ( !empty( $gateway->enabled ) && 'yes' === $gateway->enabled ) {
                return true;
            }

        }

        return false;
    }

    /**
     * Returns an array of scripts/handles to be registered for this payment method.
     *
     * @return array
     */
    public function get_payment_method_script_handles()
    {
//        $dependencies = require_once( T4A_PLUGIN_PATH . DIRECTORY_SEPARATOR . 'assets' . DIRECTORY_SEPARATOR . 'js' . DIRECTORY_SEPARATOR . 'wc-blocks' . DIRECTORY_SEPARATOR . 'wc_blocks.asset.php' );

        global $woocommerce;
        $payment_gateways = $woocommerce->payment_gateways->payment_gateways();

        $gateway = null;
        if ( isset( $payment_gateways[ $this->name ] ) && $payment_gateways[ $this->name ]->is_available() ) {
            $gateway = $payment_gateways[ $this->name ];
        }

        // `wc-blocks-registry` is required because our bundle calls
        // `wc.wcBlocksRegistry.registerPaymentMethod()` — omitting it
        // makes WooCommerce warn "accessed wc.wcBlocksRegistry without
        // declaring wc-blocks-registry as a dependency". `wp-i18n` is
        // needed for the `__()` translation calls inside the bundle.
        wp_register_script(
            T4A_PREFIX . '-wc-blocks',
            T4A_PLUGIN_URL . 'assets/js/wc-blocks/wc-blocks.js',
            [ 'wc-settings', 'wp-element', 'wc-blocks-registry', 'wp-i18n' ],
            T4A_PLUGIN_VERSION,
            true
        );

        wp_localize_script( T4A_PREFIX . '-wc-blocks', 't4aTwintFrontendBlocks', array(
            'ajaxUrl'      => admin_url( 'admin-ajax.php' ),
            'prefix'       => T4A_PREFIX,
            'userAgent'    => isset( $_SERVER[ 'HTTP_USER_AGENT' ] ) ? strtolower( sanitize_text_field( wp_unslash( (string) $_SERVER[ 'HTTP_USER_AGENT' ] ) ) ) : '',
            'httpXReqWith' => isset( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] ) ? 'true' : 'false',
            //'pairing_uuid' => $this->checkin_response->CheckInNotification->PairingUuid,
            'checkout_url' => wc_get_checkout_url(),
            'icon'         => T4A_PLUGIN_URL . 'assets/images/twint_logo_q.svg',
            'title'        => $gateway ? $gateway->title : __( 'TWINT', 'pay4all-for-twint' ),
            'description'  => $gateway ? $gateway->description : '',
        ) );

        return [ T4A_PREFIX . '-wc-blocks' ];
    }

    /**
     * Returns an array of key=>value pairs of data made available to the payment methods script.
     *
     * @return array
     */
    public function get_payment_method_data()
    {
//        return [];
        return [
            'isAdmin' => is_admin(),
            'icon'    => T4A_PLUGIN_URL . 'assets/images/twint_logo_q.svg',
        ];
    }

}