<?php
/**
 * Authorize.net API Setting Helper Class
 *
 * Provides setting helper methods to fetch configuration values of credit card and echeck.
 *
 * @package Authorize_Net_Payments_For_Woocommerce
 * @subpackage Authorize_Net_Payments_For_Woocommerce/includes
 */

if (! defined('ABSPATH')) {
    exit;
}

/**
 * Authorize.net Setting Helper Class
 * 
 * This class provides helper methods for managing Authorize.net gateway settings
 * including API credentials, test mode configuration, and logging preferences.
 * 
 * @since 1.0.0
 */
class Anet_Setting_Helper
{
    /**
     * API Login ID for Authorize.net gateway.
     *
     * @var string
     */
    private static $login_id = '';
    /**
     * Transaction Key for Authorize.net gateway.
     *
     * @var string
     */
    private static $transaction_key = '';
    /**
     * Test mode flag for Authorize.net gateway.
     *
     * @var bool
     */
    private static $testmode = false;
    /**
     * Logging flag for Authorize.net gateway.
     *
     * @var bool
     */
    private static $logging;
    /**
     * Set API Login ID.
     *
     * @param string $login_id The API Login ID.
     */
    public static function set_login_id($login_id)
    {
        self::$login_id = $login_id;
    }
    /**
     * Set Transaction Key.
     *
     * @param string $transaction_key The Transaction Key.
     */
    public static function set_transaction_key($transaction_key)
    {
        self::$transaction_key = $transaction_key;
    }
    /**
     * Set test mode preference.
     *
     * @param bool $testmode Test mode flag.
     */
    public static function set_testmode($testmode)
    {
        self::$testmode = $testmode;
    }
    /**
     * Set logging preference.
     *
     * @param bool $logging Logging preference flag.
     */
    public static function set_logging($logging)
    {
        self::$logging = $logging;
    }
    /**
     * Get settings from WordPress options.
     *
     * @param string $type Payment type (CC or eCheck).
     * @return array Settings array from WordPress options.
     */
    private static function get_settings($type)
    {
        $settings_key = (ANET_PAYMENT_TYPE_CREDIT_CARD === $type)
            ? 'woocommerce_anet_settings'
            : 'woocommerce_anet_echeck_settings';
        $settings = get_option($settings_key);
        return $settings;
    }
    /**
     * Get Credit Card payment settings.
     *
     * Retrieves and processes Credit Card gateway settings including.
     * API credentials, test mode, and logging preferences.
     *
     * @return array Processed Credit Card settings.
     */
    public static function get_cc_settings()
    {
        $options = self::get_settings(ANET_PAYMENT_TYPE_CREDIT_CARD);
        // Determine test mode using ternary operator.
        $is_testmode = is_bool(self::$testmode) && isset($options['testmode'])
            ? (ANET_ENVIRONMENT_TEST === $options['testmode'])
            : self::$testmode;
        self::set_testmode($is_testmode);
        // Get login ID using ternary operator.
        $login_id = $is_testmode && isset($options['login_id_test'])
            ? $options['login_id_test']
            : (isset($options['login_id']) ? $options['login_id'] : '');
        self::set_login_id($login_id);
        // Get transaction key using ternary operator.
        $transaction_key = $is_testmode && isset($options['transaction_key_test'])
            ? $options['transaction_key_test']
            : (isset($options['transaction_key']) ? $options['transaction_key'] : '');
        self::set_transaction_key($transaction_key);
        // Get logging using ternary operator.
        $logging = !is_bool(self::$logging) && isset($options['logging'])
            ? (ANET_YES === $options['logging'])
            : self::$logging;
        self::set_logging($logging);
        $cc_settings = [
            'login_id' => self::$login_id,
            'transaction_key' => self::$transaction_key,
            'testmode' => self::$testmode,
            'logging' => self::$logging,
        ];
        return $cc_settings;
    }
    /**
     * Get eCheck payment settings.
     *
     * Retrieves and processes eCheck gateway settings including.
     * API credentials, test mode, and logging preferences.
     *
     * @return array Processed eCheck settings.
     */
    public static function get_echeck_settings()
    {
        $options = self::get_settings(ANET_PAYMENT_TYPE_ECHECK);
        // Determine test mode using ternary operator.
        $is_testmode = is_bool(self::$testmode) && isset($options['testmode'])
            ? (ANET_ENVIRONMENT_TEST === $options['testmode'])
            : self::$testmode;
        self::set_testmode($is_testmode);
        // Get login ID using ternary operator.
        $login_id = $is_testmode && isset($options['login_id_test'])
            ? $options['login_id_test']
            : (isset($options['login_id']) ? $options['login_id'] : '');
        self::set_login_id($login_id);
        // Get transaction key using ternary operator.
        $transaction_key = $is_testmode && isset($options['transaction_key_test'])
            ? $options['transaction_key_test']
            : (isset($options['transaction_key']) ? $options['transaction_key'] : '');
        self::set_transaction_key($transaction_key);
        // Get logging using ternary operator.
        $logging = !is_bool(self::$logging) && isset($options['logging'])
            ? (ANET_YES === $options['logging'])
            : self::$logging;
        self::set_logging($logging);
        $echeck_settings = [
            'login_id' => self::$login_id,
            'transaction_key' => self::$transaction_key,
            'testmode' => self::$testmode,
            'logging' => self::$logging,
        ];
        return $echeck_settings;
    }
    /**
     * Log a message if logging is enabled.
     *
     * Checks if logging is enabled in settings and logs the message
     * using the Authorize.net logger if enabled.
     *
     * @param string $message The message to log.
     * @param string $type    Payment type to determine which settings to check.
     * @param string $level   The log level: 'error', 'warning', 'info', or 'debug'.
     */
    public static function log($message, $type, $level = 'debug')
    {
        $settings = self::get_settings($type);
        !empty($settings['logging'] && ANET_YES === $settings['logging']) ? Anet_Logger::log($message, $type, $level) : null;
    }
}
