<?php

namespace Ethereumico\Epg;

// @see https://rudrastyh.com/woocommerce/checkout-block-payment-method-integration.html

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

class Gateway_Blocks_Support extends AbstractPaymentMethodType
{

    /**
     * gateway
     *
     * @var Gateway
     */
    private $gateway;

    protected $name = 'ether-and-erc20-tokens-woocommerce-payment-gateway'; // payment gateway id

    public function initialize()
    {
        // get payment gateway settings
        $this->settings = get_option("woocommerce_{$this->name}_settings", array());

        // you can also initialize your payment gateway here
        $gateways = WC()->payment_gateways->payment_gateways();
        if (isset($gateways[$this->name])) {
            $this->gateway = $gateways[$this->name];
        } else {
            $this->gateway = new \Ethereumico\Epg\Gateway($this->name);
        }
    }

    public function is_active()
    {
        return $this->gateway->is_available();
        // return ! empty($this->settings['enabled']) && 'yes' === $this->settings['enabled'];
    }

    public function get_payment_method_script_handles()
    {

        $base_url = $GLOBALS['ether-and-erc20-tokens-woocommerce-payment-gateway']->base_url;
        $base_path = $GLOBALS['ether-and-erc20-tokens-woocommerce-payment-gateway']->base_path;
        // $min = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min';

        // 1. runtime~main
        // 2. vendors
        // 3. main
        $runtimeMain = null;
        $vendors = null;
        $main = null;
        $files = scandir($base_path . "/js/blocks/");
        foreach ($files as $filename) {
            // @see https://stackoverflow.com/a/173876/4256005
            $ext = pathinfo($filename, PATHINFO_EXTENSION);
            if ('js' !== $ext) {
                continue;
            }
            $parts = explode('.', $filename);
            if (count($parts) < 3) {
                continue;
            }
            list($name, $versionHash, $ext) = $parts;
            switch ($name) {
                case 'runtime~main':
                    $runtimeMain = [$filename, $versionHash];
                    break;

                case 'vendors':
                    $vendors = [$filename, $versionHash];
                    break;

                case 'main':
                    $main = [$filename, $versionHash];
                    break;

                default:
                    break;
            }
        }
        $deps = array();
        if (!is_null($runtimeMain)) {
            if ((!wp_script_is('wooetherc20paymentgateway-runtime-main-blocks-integration', 'queue')) &&
                (!wp_script_is('wooetherc20paymentgateway-runtime-main-blocks-integration', 'done'))
            ) {
                wp_dequeue_script('wooetherc20paymentgateway-runtime-main-blocks-integration');
                wp_deregister_script('wooetherc20paymentgateway-runtime-main-blocks-integration');
                wp_register_script(
                    'wooetherc20paymentgateway-runtime-main-blocks-integration',
                    $base_url . "/js/blocks/" . $runtimeMain[0],
                    array(),
                    $runtimeMain[1],
                    true
                );
                array_push($deps, 'wooetherc20paymentgateway-runtime-main-blocks-integration');
            }
        }

        if (!is_null($vendors)) {
            if ((!wp_script_is('wooetherc20paymentgateway-vendors-blocks-integration', 'queue')) &&
                (!wp_script_is('wooetherc20paymentgateway-vendors-blocks-integration', 'done'))
            ) {
                wp_dequeue_script('wooetherc20paymentgateway-vendors-blocks-integration');
                wp_deregister_script('wooetherc20paymentgateway-vendors-blocks-integration');
                wp_register_script(
                    'wooetherc20paymentgateway-vendors-blocks-integration',
                    $base_url . "/js/blocks/" . $vendors[0],
                    array('wooetherc20paymentgateway-runtime-main-blocks-integration'),
                    $vendors[1],
                    true
                );
                array_push($deps, 'wooetherc20paymentgateway-vendors-blocks-integration');
            }
        }

        if (!is_null($main)) {
            if ((!wp_script_is('wooetherc20paymentgateway-main-blocks-integration', 'queue')) &&
                (!wp_script_is('wooetherc20paymentgateway-main-blocks-integration', 'done'))
            ) {
                wp_dequeue_script('wooetherc20paymentgateway-main-blocks-integration');
                wp_deregister_script('wooetherc20paymentgateway-main-blocks-integration');
                wp_register_script(
                    'wooetherc20paymentgateway-main-blocks-integration',
                    $base_url . "/js/blocks/" . $main[0],
                    array_merge(
                        $deps,
                        array(
                            'wc-blocks-registry',
                            'wc-settings',
                            'wp-element',
                            'wp-html-entities',
                        )
                    ),
                    $main[1],
                    true
                );
            }
        }

        return array('wooetherc20paymentgateway-main-blocks-integration');
    }

    public function get_payment_method_data()
    {
        return array(
            'title'        => $this->get_setting('title'),
            // almost the same way:
            // 'title'     => isset( $this->settings[ 'title' ] ) ? $this->settings[ 'title' ] : 'Default value';
            'description'  => $this->get_setting('description'),
            // 'icon'         => plugin_dir_url(__DIR__) . 'assets/icon.svg',
            'icon'         => $this->gateway->get_icon(),
            // if $this->gateway was initialized on line 15
            // 'supports'  => array_filter( $this->gateway->supports, [ $this->gateway, 'supports' ] ),

            // example of getting a public key
            // 'publicKey' => $this->get_publishable_key(),
        );
    }

    //private function get_publishable_key() {
    //	$test_mode   = ( ! empty( $this->settings[ 'testmode' ] ) && 'yes' === $this->settings[ 'testmode' ] );
    //	$setting_key = $test_mode ? 'test_publishable_key' : 'publishable_key';
    //	return ! empty( $this->settings[ $setting_key ] ) ? $this->settings[ $setting_key ] : '';
    //}

}

final class Gateway_Blocks_Support2 extends Gateway_Blocks_Support
{
    protected $name = 'ether-and-erc20-tokens-woocommerce-payment-gateway2'; // payment gateway id
}
