<?php
/**
 * Credorax Payments For WooCommerce
 * https://www.credorax.com/
 *
 * @category Credorax
 * @package  WC_Credorax
 * @author   Girit-Interactive (https://www.girit-tech.com/)
 */

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

 /**
  * WC_Credorax_Model_WCSettingsAPI.
  */
 class WCCredoraxModelWCSettingsAPI extends WC_Settings_API
 {
     /**
      * ID of the class extending the settings API. Used in option names.
      * @var string
      */
     public $id = 'credorax';
 }

/**
 * Credorax config model.
 */
class WC_Credorax_Model_Config extends WC_Settings_API
{
    //= Integration (sandbox) URLs:
    const CREDORAX_GATEWAY_INTEGATION_URL = 'https://intconsole.credorax.com/intenv/service/gateway';
    const CREDORAX_STORE_INTEGATION_URL = 'https://ppskey-int.credorax.com/keypayment/rest/v2/store';
    const CREDORAX_PAYMENT_INTEGATION_URL = 'https://pps-int.credorax.com/keypayment/rest/v2/payment';
    //= Production (live) URLs:
    const CREDORAX_GATEWAY_PRODUCTION_URL = 'https://xts.gate.credorax.net/crax_gate/service/gateway';
    const CREDORAX_STORE_PRODUCTION_URL = 'https://ppskey.credorax.net/keypayment/rest/v2/store';
    const CREDORAX_PAYMENT_PRODUCTION_URL = 'https://PPS.credorax.net/keypayment/rest/v2/payment';


    /**
     * ID of the class extending the settings API. Used in option names.
     *
     * @var WC_Credorax_Model_WCSettingsAPI
     */
    private static $wcSettingsApi = null;

    /**
     * Constructor
     */
    public static function init()
    {
        if (self::$wcSettingsApi === null) {
            self::$wcSettingsApi = new WCCredoraxModelWCSettingsAPI();
        }
    }

    /**
     * Get option from DB.
     *
     * Gets an option from the settings API, using defaults if necessary to prevent undefined notices.
     *
     * @param  string $key Option key.
     * @param  mixed  $empty_value Value when empty.
     * @return string The value specified for the option or a default value for the option.
     */
    public static function getOption($key, $empty_value = null)
    {
        return self::$wcSettingsApi->get_option($key, $empty_value);
    }

    /**
     * Return bool value depends of that if payment method is enabled or not.
     *
     * @return bool
     */
    public static function isActive()
    {
        return self::getOption('enabled') === 'yes';
    }

    /**
     * Return 'yes'/'no', the actual value of the `enabled` config
     *
     * @return string
     */
    public static function isEnabled()
    {
        return self::getOption('enabled');
    }

    /**
     * Return title.
     *
     * @return string
     */
    public static function getTitle()
    {
        return self::getOption('title');
    }

    /**
     * Return description.
     *
     * @return string
     */
    public static function getDescription()
    {
        return self::getOption('description');
    }

    /**
     * Return merchant id.
     *
     * @return string
     */
    public static function getMerchantId()
    {
        return self::getOption((self::isSandboxMode()) ? 'sandbox_merchant_id' : 'merchant_id');
    }

    /**
     * Return sub-merchant id.
     *
     * @return string
     */
    public static function getSubMerchantId()
    {
        return self::getOption((self::isSandboxMode()) ? 'sandbox_sub_merchant_id' : 'sub_merchant_id');
    }

    /**
     * Return signature key.
     *
     * @return string
     */
    public static function getSignatureKey()
    {
        return self::getOption((self::isSandboxMode()) ? 'sandbox_signature_key' : 'signature_key');
    }

    /**
     * Return static key.
     *
     * @return string
     */
    public static function getStaticKey()
    {
        self::getOption((self::isSandboxMode()) ? 'sandbox_static_key' : 'static_key');
    }

    /**
     * Return payment action configuration value (operation code).
     *
     * @return string
     */
    public static function getPaymentAction()
    {
        return self::getOption('payment_action');
    }

    /**
     * @return bool
     */
    public static function isAuthirizeAction()
    {
        return self::getPaymentAction() === WC_Credorax_Gateway::ACTION_AUTHORIZE;
    }

    /**
     * @return bool
     */
    public static function isAuthirizeAndCaptureAction()
    {
        return self::getPaymentAction() === WC_Credorax_Gateway::ACTION_AUTHORIZE_CAPTURE;
    }

    /**
     * Return bool value depends of that if payment method sandbox mode
     * is enabled or not.
     *
     * @return bool
     */
    public static function isSandboxMode()
    {
        return (self::getOption('mode') === WC_Credorax_Gateway::MODE_LIVE) ? false : true;
    }

    /**
     * Return bool value depends of that if payment method debug mode
     * is enabled or not.
     *
     * @return bool
     */
    public static function isDebugEnabled()
    {
        return (bool)self::getOption('debug');
    }

    /**
     * Return cc types.
     *
     * @return string
     */
    public static function getCcTypes()
    {
        return self::getOption('cctypes');
    }

    /**
     * @return bool
     */
    public static function is3dSecureEnabled()
    {
        return (bool)self::getOption('enable_3d_secure');
    }

    /**
     * @return bool
     */
    public static function isUsingSmart3d()
    {
        return (bool)self::getOption('use_smart_3d');
    }

    /**
     * Return use tokanization/vault (save_cards) configuration value.
     *
     * @return bool
     */
    public static function isUsingVault()
    {
        return (bool)self::getOption('save_cards');
    }

    /**
     * @return bool
     */
    public static function getMinOrderTotal()
    {
        return (bool)self::getOption('min_order_total');
    }

    /**
     * @return bool
     */
    public static function getMaxOrderTotal()
    {
        return (float)self::getOption('max_order_total');
    }

    /**
     * @return bool
     */
    public static function useJsLoader()
    {
        return (float)self::getOption('use_js_loader');
    }

    /**
     * Return billing descriptor configuration value.
     *
     * @return bool
     */
    public static function getBillingDescriptor()
    {
        return self::getOption('billing_descriptor');
    }

    /**
     * @method getCredoraxGatewayUrl
     * @param string $path
     * @return string
     */
    public static function getCredoraxGatewayUrl($path = "")
    {
        return (self::isSandboxMode() ? self::CREDORAX_GATEWAY_INTEGATION_URL : self::CREDORAX_GATEWAY_PRODUCTION_URL) . (($path) ? '/' . $path : '');
    }

    /**
     * @method getCredoraxStoreUrl
     * @param string $path
     * @return string
     */
    public static function getCredoraxStoreUrl($path = "")
    {
        return (self::isSandboxMode() ? self::CREDORAX_STORE_INTEGATION_URL : self::CREDORAX_STORE_PRODUCTION_URL) . (($path) ? '/' . $path : '');
    }

    /**
     * @method getCredoraxPaymentUrl
     * @param string $path
     * @return string
     */
    public static function getCredoraxPaymentUrl($path = "")
    {
        return (self::isSandboxMode() ? self::CREDORAX_PAYMENT_INTEGATION_URL : self::CREDORAX_PAYMENT_PRODUCTION_URL) . (($path) ? '/' . $path : '');
    }

    /**
     * Alias for WC_Credorax_Model_Logger::log()
     * @method log
     * @param  mixed   $message
     * @param  string  $type
     * @param  array   $data
     * @param  string  $prefix
     * @return $this
     */
    public static function log($message, $type = "debug", $data = [], $prefix = '[Credorax] ')
    {
        WC_Credorax_Model_Logger::log($message, $type, $data, $prefix);
    }

    /**
     * Alias for WC_Credorax_Source_Messages::get3dStatusMessage()
     * @method get3dStatusMessage
     * @param  mixed        $status
     * @return string|null
     */
    public static function get3dStatusMessage($status)
    {
        return WC_Credorax_Source_Messages::get3dStatusMessage($status);
    }
}

WC_Credorax_Model_Config::init();
