<?php

namespace Devurai\AcceptbluePaymentPro\GooglePay;

use Devurai\AcceptbluePaymentPro\Settings\GooglePaySettings;
use Devurai\AcceptbluePaymentPro\Traits\WoocommerceFunctions;

class GooglePayButton
{
    use WoocommerceFunctions;
    public function __construct()
    {
        add_action('wp_enqueue_scripts', array($this, 'enqueueScripts'));
        add_filter('script_loader_tag', array($this, 'add_custom_attribute_to_script'), 10, 3);
//        add_action('acceptblue_after_credit_card_form', array($this, 'add_google_pay_button_container'));
        add_action('wp_footer', array($this, 'add_google_pay_order_info'), 10, 1);
    }

    public function enqueueScripts(): void
    {
        if(!GooglePaySettings::getInstance()->is_enabled()) return;

        wp_enqueue_script('pgabfw_google_pay_handler', pgabfw_pro_assets_url('js/pgabfw_google_pay_button.js'), array(), '', true);
        wp_enqueue_script('pgabfw_google_pay_button', 'https://pay.google.com/gp/p/js/pay.js', array('pgabfw_google_pay_handler'), '', true);
    }

    public function add_custom_attribute_to_script($tag, $handle, $src)
    {
        if($handle === 'pgabfw_google_pay_button'){
            $src = esc_url($src);
            $tag = "<script src=\"$src\" id=\"$handle\" onload=\"pgabfwOnGooglePayLoaded()\"></script>";
        }

        return $tag;
    }

    public static function add_google_pay_button_container(): void {
        echo "<input type=\"hidden\" name=\"ab_token\" value=\"\" id=\"pgabfw-google-pay-token\" >";
        echo "<div class='acceptblue-google-pay-wrap'><div id=\"pgabfw_google_pay_button_container\"></div></div>";
    }

    public function add_google_pay_order_info(): void
    {
        if(!GooglePaySettings::getInstance()->is_enabled()) return;

        try {
            $total_price = WC()->cart->get_totals()['total'];
        }catch (\Exception $e){
            $total_price = 0;
        }
        $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();
        $merchant_ID = GooglePaySettings::getInstance()->get_merchant_ID();
        $wc_ajax_checkout = get_site_url() . \WC_AJAX::get_endpoint('checkout');
        $mode = GooglePaySettings::getInstance()->is_test_mode() ? 'TEST' : 'PRODUCTION';
        $surcharge = apply_filters('pgabfw_surcharge_amount', 0, $total_price, GooglePaySettings::getInstance());
        echo "
            <script>
                function pgabfwProGetSurcharge(){
                    return '$surcharge';
                }
                function pgabfwProGetMode(){
                    return '$mode';
                }
                function pgabfwProGetAjaxCheckout(){
                    return '$wc_ajax_checkout';
                }
                function pgabfwProGetGoogleTransactionInfo() {
                    return {
                        countryCode: '$country_code',
                        currencyCode: '$currency_code',
                        totalPriceStatus: 'FINAL',
                        totalPrice: '$total_price'
                    };
                }
                function pgabfwProGetMerchantID(){
                    return '$merchant_ID';
                }
                function pgabfwProGetMerchantName(){
                    return  '$merchant_name';
                }  
            </script>
        ";

    }
}