<?php

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

defined('ABSPATH') || exit;

final class Stitch_Express_Blocks_Support extends AbstractPaymentMethodType {
    public Stitch_Express_Client $client;
    public Stitch_Express_Payment_Gateway $gateway;
    protected $name = 'stitch-express';

    public function initialize(): void {
        $this->gateway = new Stitch_Express_Payment_Gateway();
        $client_id = $this->gateway->get_option('client_id');
        $client_secret = $this->gateway->get_option('client_secret');

        $this->client = new Stitch_Express_Client($client_id, $client_secret);
    }

    public function is_active(): bool {
        return true;
    }

    public function get_payment_method_script_handles() {
        $script_path = '/build/index.js';
        $script_asset_path = STITCH_EXPRESS_PLUGIN_PATH.'/build/index.asset.php';
        $script_asset = file_exists($script_asset_path)
            ? require ($script_asset_path)
            : ['dependencies' => [], 'version' => filemtime(STITCH_EXPRESS_PLUGIN_PATH.$script_path)];
        $script_url = STITCH_EXPRESS_PLUGIN_URL.$script_path;

        $dependencies = [];
        wp_register_script(
            'stitch-express-blocks-integration',
            $script_url,
            $dependencies,
            $script_asset['version'],
            true
        );

        wp_enqueue_style(
            'stitch-express-blocks-checkout-style',
            STITCH_EXPRESS_PLUGIN_URL.'/build/index.css',
            [],
            filemtime(STITCH_EXPRESS_PLUGIN_PATH.'/build/index.css')
        );

        return ['stitch-express-blocks-integration'];
    }

    public function get_payment_method_data(): array {
        $icon_block_visa = STITCH_EXPRESS_PLUGIN_URL.'/assets/blocks/visa.svg';
        $icon_block_master_card = STITCH_EXPRESS_PLUGIN_URL.'/assets/blocks/master-card.svg';
        $icon_block_capitec_pay = STITCH_EXPRESS_PLUGIN_URL.'/assets/blocks/capitec-pay.svg';
        $icon_block_bnpl = STITCH_EXPRESS_PLUGIN_URL.'/assets/blocks/stitch.svg';
        $icon_block_google_pay = STITCH_EXPRESS_PLUGIN_URL.'/assets/blocks/google-pay.svg';
        $icon_block_apple_pay = STITCH_EXPRESS_PLUGIN_URL.'/assets/blocks/apple-pay.svg';

        return [
            'icon_block_visa' => $icon_block_visa,
            'icon_block_master_card' => $icon_block_master_card,
            'icon_block_capitec_pay' => $icon_block_capitec_pay,
            'icon_block_bnpl' => $icon_block_bnpl,
            'icon_block_apple_pay' => $icon_block_apple_pay,
            'icon_block_google_pay' => $icon_block_google_pay,
            'checkout_title' => $this->gateway->title,
            'checkout_description' => $this->gateway->description,
        ];
    }
}
