<?php

namespace Devurai\AcceptbluePaymentPro\GooglePay;

use \Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType;
use Devurai\AcceptbluePaymentPro\Settings\GooglePaySettings;
use Devurai\AcceptbluePaymentPro\Traits\WoocommerceFunctions;

class WC_AcceptBlue_GooglePay_Block extends AbstractPaymentMethodType
{
    use WoocommerceFunctions;
    private WC_AcceptBlue_GooglePay_Gateway $gateway;
    /**
     * @var $name string
     */
    protected $name;
    /**
     * @var $settings GooglePaySettings
     */
    protected $settings;

    public function __construct($payment_gateway)
    {
        $this->name = $payment_gateway->id;
        $this->gateway = $payment_gateway;
    }

    public function initialize(): void
    {
        $this->settings = GooglePaySettings::getInstance();
    }

    public function is_active(): bool
    {
        return $this->gateway->is_available();
    }

    public function get_payment_method_script_handles(): array
    {
        wp_register_script(
            'wc-acceptblue-google-pay-block-integration',
            pgabfw_pro_root_url( 'wc_blocks/google_pay_block.js'),
            array(
                'wc-settings',
                'wp-element',
                'wp-html-entities',
                'wp-i18n',
                'pgabfw_google_pay_button'
            ),
            null,
            array()
        );

        return array('wc-acceptblue-google-pay-block-integration');
    }

    public function get_payment_method_data(): array
    {
        $total_price = 0;
        if ( function_exists('WC') && WC()->cart && is_object( WC()->cart ) && method_exists( WC()->cart, 'get_totals' ) ) {
            $totals = WC()->cart->get_totals();
            if ( isset( $totals['total'] ) ) {
                $total_price = $totals['total'];
            }
        }
        $currency_code = get_option('woocommerce_currency');
        $country_code = $this->get_shop_country_code();
        $merchant_name  = get_bloginfo('name');
        if(empty($merchant_name)) $merchant_name = $this->get_site_address();
        return array(
            'title' => $this->settings->get_title(),
            'description' => $this->settings->get_description(),
            'supports' => $this->gateway->supports,
            'customerIsLoggedIn' => is_user_logged_in() ? 'true' : 'false',
            'hasSubscription' => (class_exists('WC_Subscriptions_Cart') && \WC_Subscriptions_Cart::cart_contains_subscription()) ? 'true' : 'false',
            'merchant_id' => $this->settings->get_merchant_ID(),
            'merchant_name' => $merchant_name,
            'mode' => $this->settings->is_test_mode() ? 'TEST' : 'PRODUCTION',
            'surcharge_amount' => apply_filters('pgabfw_surcharge_amount', 0, $total_price, $this->settings),
            'transaction_info' => array(
                'countryCode' => $country_code,
                'currencyCode' => $currency_code,
                'totalPriceStatus' => 'FINAL',
                'totalPrice' => $total_price
            )
        );
    }
}