<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

require_once('abstract-payment-method.php');

class WC_BobPay_Plugin_Google_Pay extends BobPay_Abstract_Payment_Method
{
    public function __construct()
    {
        $this->id = 'bobpay_google_pay';
        $this->method_title = 'Bob Pay Google Pay';
        $this->icon = WC_BOBPAY_PLUGIN_URL . '/assets/images/google_pay.png';
        $this->response_url = add_query_arg('wc-api', 'WC_BobPay_Plugin_Google_Pay', home_url('/'));
        $this->available_currencies = (array) apply_filters('woocommerce_bobpay_google_pay_plugin_available_currencies', array('ZAR'));

        if (defined('BOBPAY_LOCAL_DEV') && defined('NGROK_TUNNEL_URL') && 'true' === BOBPAY_LOCAL_DEV) {
            $this->response_url = add_query_arg('wc-api', 'WC_BobPay_Plugin_Google_Pay', NGROK_TUNNEL_URL . '/');
        }

        parent::__construct();

        $this->get_gateway_title();

        add_action('woocommerce_receipt_bobpay_google_pay', array($this, 'receipt_page'));
        add_action('woocommerce_api_wc_bobpay_plugin_google_pay', array($this, 'process_payment_response'));
    }

    protected function get_method_description_string(): string
    {
        /* translators: %1$s: opening link tag, %2$s: closing link tag */
        return __('Bob Pay Google Pay redirects customers to %1$sBob Pay%2$s to pay with Google Pay.', 'bobpay-woo');
    }

    public function init_form_fields()
    {
    }

    public function is_available() {
        $settings = get_option('woocommerce_bobpay_settings', array());

        if (!$settings) {
            return false;
        }

        if (!isset($settings['google_pay_enabled']) || $settings['google_pay_enabled'] !== 'yes') {
            return false;
        }

        return parent::is_available();
    }

    public function get_gateway_title() {
        $settings = get_option('woocommerce_' . $this->id . '_settings');
        if (isset($settings['title']) && !empty($settings['title'])) {
            return;
        }

        $settings['title'] = 'Google Pay';
        update_option('woocommerce_' . $this->id . '_settings', $settings);
    }

    public function receipt_page($order)
    {
        echo '<p>' . esc_html__('Thank you for your order, please click the button below to pay with Bob Pay.', 'bobpay-woo') . '</p>';
        $this->redirect_to_bobpay($order, 'google-pay');
    }
}
