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

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

class WC_BobPay_Plugin extends BobPay_Abstract_Payment_Method
{
    public function __construct()
    {
        $this->id = 'bobpay';
        $this->method_title = 'Bob Pay';
        $this->title = 'Bob Pay';
        $this->response_url = add_query_arg('wc-api', 'WC_BobPay_Plugin', home_url('/'));
        $this->available_currencies = (array) apply_filters('woocommerce_bobpay_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', NGROK_TUNNEL_URL . '/');
        }

        parent::__construct();

        // Set icon after _construct to prevent it from being overwritten
        $this->icon = WC_BOBPAY_PLUGIN_URL . '/assets/images/bob-pay-logo.png';

        $this->get_gateway_title();

        add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
        add_action('woocommerce_receipt_bobpay', array($this, 'receipt_page'));
        add_action('woocommerce_api_wc_bobpay_plugin', array($this, 'process_payment_response'));
    }

    public function init_form_fields()
    {
        parent::init_form_fields();
    }

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

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

    public function is_available()
    {
        $settings = get_option('woocommerce_bobpay_settings');
        if (!empty($settings) && isset($settings['enable_multiple_payment_methods']) && $settings['enable_multiple_payment_methods'] === 'multiple') {
            return false;
        }
        
        return parent::is_available();
    }

    protected function get_method_description_string(): string
    {
        /* translators: %1$s: opening link tag, %2$s: closing link tag */
        return __('Accept payments via Credit/Debit Card, Instant EFT, Capitec Pay, PayShap, and more through Bob Pay. Display payment methods individually or as a single option. No setup fees or monthly costs. Learn more at %1$sBob Pay%2$s.', '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);
    }
}
