<?php
namespace Svea_Webpay_For_Woocommerce\Gateways;

use Svea_Webpay_For_Woocommerce\Admin_Functions;
use Svea_Webpay_For_Woocommerce\Service_Providers\Config_Production;
use Svea_Webpay_For_Woocommerce\Service_Providers\Config_Test;
use WC_Logger;

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

/**
 * Class to handle payments through Svea
 */
abstract class WC_Gateway_Abstract extends \WC_Payment_Gateway {

	/**
	 * Meta key used to flag that a successful payment callback has been processed.
	 *
	 * @var string
	 */
	const CALLBACK_PROCESSED_META_KEY = '_svea_callback_processed';

	/**
	 * Value written to CALLBACK_PROCESSED_META_KEY once the callback is processed.
	 *
	 * @var string
	 */
	const CALLBACK_PROCESSED_VALUE = 'yes';

	/**
	 * @inheritDoc
	 */
	public $has_fields = true;

	/**
	 * Whether or not the log should be enabled
	 *
	 * @var boolean
	 */
	protected static $log_enabled = false;

	/**
	 * The logger object
	 *
	 * @var WC_Logger
	 */
	protected static $log = null;

	/**
	 * Base country
	 *
	 * @var string
	 */
	public $base_country;

	/**
	 * Merchant id
	 *
	 * @var string
	 */
	protected $merchant_id;

	/**
	 * Secret word
	 *
	 * @var string
	 */
	protected $secret_word;

	/**
	 * Username
	 *
	 * @var string
	 */
	protected $username;

	/**
	 * Password
	 *
	 * @var string
	 */
	protected $password;

	/**
	 * Client number
	 *
	 * @var string
	 */
	protected $client_nr;

	/**
	 * testmode
	 *
	 * @var string
	 */
	protected $testmode;

	/**
	 * language
	 *
	 * @var string
	 */
	public $language;

	/**
	 * Config
	 *
	 * @var Config_Production
	 */
	protected $config;

	/**
	 * Country code
	 *
	 * @var string
	 */
	protected $country_code;

	/**
	 * Enabled countries
	 *
	 * @var array
	 */
	public $enabled_countries = [];

	/**
	 * Enabled countries with respective currencies
	 */
	public $enabled_country_currencies = [];

	/**
	 * Logging method
	 *
	 * @param string $message
	 * @param string $level
	 * @return void
	 */
	public static function log( $message, $level = 'info' ) {
		if ( static::$log_enabled ) {
			if ( is_null( static::$log ) ) {
				// Failsafe fatal errors, logging is not critical
				if ( ! class_exists( 'WC_Logger' ) ) {
					return;
				}

				static::$log = new WC_Logger();
			}

			static::$log->log(
				$level,
				$message,
				[
					'source' => constant( get_called_class() . '::GATEWAY_ID' ),
				]
			);
		}
	}

	/**
	 * Setup the configuration for this payment method
	 *
	 * @return void
	 */
	public function set_config( $suffix = false ) {
		if ( $suffix === false && ! isset( WC()->customer ) ) {
			return;
		}

		// If no suffix is provided, use the customer's country
		if ( $suffix === false ) {
			$suffix = '_' . strtolower( WC()->customer->get_billing_country() );
		} else if ( $suffix !== '' ) {
			$suffix = '_' . strtolower( $suffix );
		}

		$suffix = strtolower( $suffix );

		$this->merchant_id = $this->get_option( 'merchant_id' . $suffix );
		$this->secret_word = $this->get_option( 'secret_word' . $suffix );
		$this->testmode = $this->get_option( 'testmode' . $suffix ) === 'yes';

		if ( ! $this->merchant_id || ! $this->secret_word ) {
			return;
		}

		$args = [ $this->merchant_id, $this->secret_word, false, false, false ];

		$this->config = $this->testmode ? new Config_Test( ...$args ) : new Config_Production( ...$args );
	}

	/**
	 * Get the gateway description
	 *
	 * @param string $description the description for the provided gateway
	 * @param int $id the id of the provided gateway
	 * @return string the gateway description
	 */
	public function get_gateway_description( $description, $id ) {
		if ( ! isset( $this->description[0] ) ) {
			$this->description = '<div>' . __( $this->get_option( 'description' ), 'svea-webpay-for-woocommerce' ) . '</div>'; // phpcs:ignore
		}

		return wp_kses_post( $this->description );
	}

	/**
	 * Get the country code function.
	 *
	 * @since 1.0.0
	 * @return string
	 */
	public function get_country_code() {
		if ( ! isset( $this->country_code ) || strlen( $this->country_code ) === 0 ) {
			$this->country_code = $this->base_country;
		}

		return $this->country_code;
	}

	/**
	 * Get the config for this payment method
	 *
	 * @return  mixed   the configuration object
	 */
	public function get_config() {
		return $this->config;
	}

	/**
	 * Get the payment gateway instance
	 *
	 * @param string $payment_method_id
	 * @return WC_Gateway_Svea_Card|WC_Gateway_Svea_Invoice|WC_Gateway_Svea_Part_Pay|WC_Gateway_Svea_Swish|WC_Gateway_Svea_Vipps|WC_Gateway_Svea_Mobilepay|WC_Gateway_Svea_Trustly|false
	 */
	public static function get_gateway_instance( $payment_method_id ) {
		$wc_gateway = false;

		if ( $payment_method_id === WC_Gateway_Svea_Card::GATEWAY_ID ) {
			$wc_gateway = WC_Gateway_Svea_Card::init();
		} else if ( $payment_method_id === WC_Gateway_Svea_Swish::GATEWAY_ID ) {
			$wc_gateway = WC_Gateway_Svea_Swish::init();
		} else if ( $payment_method_id === WC_Gateway_Svea_Invoice::GATEWAY_ID ) {
			$wc_gateway = WC_Gateway_Svea_Invoice::init();
		} else if ( $payment_method_id === WC_Gateway_Svea_Part_Pay::GATEWAY_ID ) {
			$wc_gateway = WC_Gateway_Svea_Part_Pay::init();
		} else if ( $payment_method_id === WC_Gateway_Svea_Vipps::GATEWAY_ID ) {
			$wc_gateway = WC_Gateway_Svea_Vipps::init();
		} else if ( $payment_method_id === WC_Gateway_Svea_Mobilepay::GATEWAY_ID ) {
			$wc_gateway = WC_Gateway_Svea_Mobilepay::init();
		} else if ( $payment_method_id === WC_Gateway_Svea_Trustly::GATEWAY_ID ) {
			$wc_gateway = WC_Gateway_Svea_Trustly::init();
		}

		return $wc_gateway;
	}
}
