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

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

class WC_BobPay_Plugin_Capitec_Pay extends BobPay_Abstract_Payment_Method
{
    public function __construct()
    {
        $this->id = 'bobpay_capitec_pay';
        $this->method_title = 'Bob Pay Capitec Pay';
        $this->icon = WC_BOBPAY_PLUGIN_URL . '/assets/images/capitec-pay.svg';
        $this->response_url = add_query_arg('wc-api', 'WC_BobPay_Plugin_Capitec_Pay', home_url('/'));
        $this->available_currencies = (array) apply_filters('woocommerce_bobpay_capitec_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_Capitec_Pay', NGROK_TUNNEL_URL . '/');
        }

        parent::__construct();

        $this->get_gateway_title();

        add_action('woocommerce_receipt_bobpay_capitec_pay', array($this, 'receipt_page'));
        add_action('woocommerce_api_wc_bobpay_plugin_capitec_pay', array($this, 'process_payment_response'));
    }

    public function init_form_fields()
    {
    }

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

         if (!$settings) {
            return false;
        }

        if (!isset($settings['capitec_pay_enabled']) || $settings['capitec_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'] = 'Capitec Pay';
        update_option('woocommerce_' . $this->id . '_settings', $settings);
    }

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

    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, 'capitec-pay');
    }
}