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

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

class WC_BobPay_Plugin_Nedbank_Direct_EFT extends BobPay_Abstract_Payment_Method
{
    public function __construct()
    {
        $this->id = 'bobpay_nedbank_direct_eft';
        $this->method_title = 'Bob Pay Nedbank Direct EFT';
        $this->icon = WC_BOBPAY_PLUGIN_URL . '/assets/images/nedbank-direct-eft.svg';
        $this->response_url = add_query_arg('wc-api', 'WC_BobPay_Plugin_Nedbank_Direct_EFT', home_url('/'));
        $this->available_currencies = (array) apply_filters('woocommerce_bobpay_nedbank_direct_eft_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_Nedbank_Direct_EFT', NGROK_TUNNEL_URL . '/');
        }

        parent::__construct();

        $this->get_gateway_title();

        add_action('woocommerce_receipt_bobpay_nedbank_direct_eft', array($this, 'receipt_page'));
        add_action('woocommerce_api_wc_bobpay_plugin_nedbank_direct_eft', 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['nedbank_direct_eft_enabled']) || $settings['nedbank_direct_eft_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'] = 'Nedbank Direct EFT';
        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 Nedbank Direct EFT redirects customers to %1$sBob Pay%2$s to pay with Nedbank.', '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, 'nedbank-direct-eft');
    }

}