<?php

namespace Sibs2;

class sibsGeneralFunctions
{
    public static $percent = 100;

    public static function sibs_get_payment_gateway_variable($payment_id)
    {
        $payment_gateway                  = [];
        $payment_gateway['payment_type']  = self::sibs_get_payment_type($payment_id);
        $payment_gateway['payment_brand'] = self::sibs_get_payment_brand($payment_id);
        $payment_gateway['payment_group'] = self::sibs_get_payment_group($payment_id);
        $payment_gateway['is_recurring']  = self::sibs_is_recurring($payment_id);
        $payment_gateway['language']      = sibsFunctions::sibs_get_shop_language();

        return $payment_gateway;
    }

    public static function sibs_get_credentials($payment_id, $is_testmode_available = true, $is_multichannel_available = false)
    {
        $payment_settings = get_option("woocommerce_{$payment_id}_settings");

        $credentials                = [];
        $credentials['server_mode'] = get_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_general_environment');
        $credentials['channel_id']  = (! empty($payment_settings['multichannel']) && $is_multichannel_available) ? $payment_settings['channel_moto'] : get_option(sibsConstants::SIBS_PLUGIN_PREFIX . '_general_merchant_no');

        if ($is_testmode_available) {
            $credentials['test_mode'] = self::sibs_get_test_mode($payment_id, $credentials['server_mode']);
        }

        return $credentials;
    }

    public static function sibs_get_test_mode($payment_id, $server_mode)
    {
        if ($server_mode === 'LIVE') {
            return false;
        }

        return ($payment_id === sibsConstants::SIBS_PAYMENT_METHOD_ORDER_GIROPAY) ? 'INTERNAL' : 'EXTERNAL';
    }

    public static function sibs_get_recurring_payment()
    {
        $payments = [
            sibsConstants::SIBS_PAYMENT_METHOD_ORDER_CREDIT_CARD_SAVED,
            sibsConstants::SIBS_PAYMENT_METHOD_ORDER_DIRECT_DEBIT_SAVED,
            sibsConstants::SIBS_PAYMENT_METHOD_ORDER_PAYPAL_SAVED,
        ];

        return $payments;
    }

    public static function sibs_get_payment_brand($payment_id)
    {
        switch ($payment_id) {
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_CREDIT_CARD:
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_CREDIT_CARD_SAVED:
                $payment_brand = self::sibs_get_payment_brand_cards($payment_id);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_PAYPAL:
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_PAYPAL_SAVED:
                $payment_brand = sibsConstants::SIBS_PAYMENT_METHOD_PAYPAL;

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_BLIK:
                $payment_brand = sibsConstants::SIBS_PAYMENT_METHOD_BLIK;

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_PAY_BY_LINK:
                $payment_brand = sibsConstants::SIBS_PAYMENT_METHOD_PAY_BY_LINK;

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_PAY_BY_LINK_KEVIN:
                $payment_brand = sibsConstants::SIBS_PAYMENT_METHOD_PBLKV;

                break;
            default:
                $payment_brand = false;
        }

        return $payment_brand;
    }

    public static function sibs_get_payment_brand_cards($payment_id)
    {
        $payment_settings = get_option("woocommerce_{$payment_id}_settings");
        $cards            = ! empty($payment_settings['card_types']) ? $payment_settings['card_types'] : false;
        $payment_brand    = '';

        if (! empty($cards) && is_array($cards)) {
            foreach ($cards as $card) {
                $payment_brand .= strtoupper($card) . ' ';
            }
        }

        return $payment_brand;
    }

    /**
     * Get Payment Form.
     *
     * @param string $payment_id payment id.
     * @return string
     */
    public static function sibs_get_payment_form($payment_id)
    {
        switch ($payment_id) {
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_EASYCREDIT:
                $payment_form = 'servertoserver';

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_PAYPAL_SAVED:
                $payment_form = 'form_paypalsaved';

                break;
            default:
                $payment_form = 'form';
        }

        return $payment_form;
    }

    public static function sibs_get_payment_type($payment_id)
    {
        $payment_auth_db = [
            sibsConstants::SIBS_PAYMENT_METHOD_ORDER_CREDIT_CARD_SAVED,
            sibsConstants::SIBS_PAYMENT_METHOD_ORDER_DEBIT_CARD,
            sibsConstants::SIBS_PAYMENT_METHOD_ORDER_DIRECT_DEBIT,
            sibsConstants::SIBS_PAYMENT_METHOD_ORDER_DIRECT_DEBIT_SAVED,
            sibsConstants::SIBS_PAYMENT_METHOD_ORDER_PAYDIREKT,
            sibsConstants::SIBS_PAYMENT_METHOD_ORDER_KLARNAINS,
            sibsConstants::SIBS_PAYMENT_METHOD_ORDER_KLARNAINV,
            sibsConstants::SIBS_PAYMENT_METHOD_ORDER_EASYCREDIT,
            sibsConstants::SIBS_PAYMENT_METHOD_ORDER_CREDIT_CARD,
        ];

        if (in_array($payment_id, $payment_auth_db)) {
            $settings = get_option("woocommerce_{$payment_id}_settings");

            return ! empty($settings['trans_mode']) ? $settings['trans_mode'] : sibsConstants::SPG_PAYMENT_TYPE_PURS;
        } else {
            return sibsConstants::SPG_PAYMENT_TYPE_PURS;
        }
    }

    public static function sibs_get_payment_group($payment_id)
    {
        switch ($payment_id) {
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_CREDIT_CARD_SAVED:
                $payment_group = sibsConstants::SIBS_PAYMENT_METHOD_CC;

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_DIRECT_DEBIT_SAVED:
                $payment_group = sibsConstants::SIBS_PAYMENT_METHOD_DD;

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_PAYPAL_SAVED:
                $payment_group = sibsConstants::SIBS_PAYMENT_METHOD_PAYPAL;

                break;
            default:
                $payment_group = false;
        }

        return $payment_group;
    }

    public static function sibs_get_payment_id_by_group($payment_group)
    {
        switch ($payment_group) {
            case sibsConstants::SIBS_PAYMENT_METHOD_CC:
                $payment_id = sibsConstants::SIBS_PAYMENT_METHOD_ORDER_CREDIT_CARD_SAVED;

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_DD:
                $payment_id = sibsConstants::SIBS_PAYMENT_METHOD_ORDER_DIRECT_DEBIT_SAVED;

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_PAYPAL:
                $payment_id = sibsConstants::SIBS_PAYMENT_METHOD_ORDER_PAYPAL_SAVED;

                break;
            default:
                $payment_id = false;
        }

        return $payment_id;
    }

    public static function sibs_is_recurring($payment_id)
    {
        return
            $payment_id === sibsConstants::SIBS_PAYMENT_METHOD_ORDER_CREDIT_CARD_SAVED ||
            $payment_id === sibsConstants::SIBS_PAYMENT_METHOD_ORDER_DIRECT_DEBIT_SAVED ||
            $payment_id === sibsConstants::SIBS_PAYMENT_METHOD_ORDER_PAYPAL_SAVED;
    }

    public static function sibs_get_account_by_result($payment_id, $payment_result)
    {
        switch ($payment_id) {
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_CREDIT_CARD_SAVED:
                $account_card['holder']        = $payment_result['card']['holder'];
                $account_card['last_4_digits'] = $payment_result['card']['last4Digits'];
                $account_card['expiry_month']  = $payment_result['card']['expiryMonth'];
                $account_card['expiry_year']   = $payment_result['card']['expiryYear'];
                $account_card['email']         = '';

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_DIRECT_DEBIT_SAVED:
                $account_card['holder']        = $payment_result['bankAccount']['holder'];
                $account_card['last_4_digits'] = substr($payment_result['bankAccount']['iban'], -4);
                $account_card['expiry_month']  = '';
                $account_card['expiry_year']   = '';
                $account_card['email']         = '';

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_PAYPAL_SAVED:
                $account_card['holder']        = $payment_result['virtualAccount']['holder'];
                $account_card['last_4_digits'] = '';
                $account_card['expiry_month']  = '';
                $account_card['expiry_year']   = '';
                $account_card['email']         = $payment_result['virtualAccount']['accountId'];

                break;
        }

        return $account_card;
    }

    /**
     * Translate Frontend Payment Name.
     *
     * @param string $payment_id payment id.
     * @return string
     */
    public static function sibs_translate_frontend_payment($payment_id)
    {
        $payment_locale = '';

        switch ($payment_id) {
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_CREDIT_CARD:
                $payment_locale = __('FRONTEND_PM_CC', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_CREDIT_CARD_SAVED:
                $payment_locale = __('FRONTEND_PM_CCSAVED', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_DEBIT_CARD:
                $payment_locale = __('FRONTEND_PM_DC', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_DIRECT_DEBIT:
                $payment_locale = __('FRONTEND_PM_DD', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_DIRECT_DEBIT_SAVED:
                $payment_locale = __('FRONTEND_PM_DDSAVED', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_EASYCREDIT:
                $payment_locale = __('FRONTEND_PM_EASYCREDIT', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_GIROPAY:
                $payment_locale = __('FRONTEND_PM_GIROPAY', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_IDEAL:
                $payment_locale = __('FRONTEND_PM_IDEAL', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_KLARNAINS:
                $payment_locale = __('FRONTEND_PM_KLARNAINS', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_KLARNAINV:
                $payment_locale = __('FRONTEND_PM_KLARNAINV', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_MASTERPASS:
                $payment_locale = __('FRONTEND_PM_MASTERPASS', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_PAYDIREKT:
                $payment_locale = __('FRONTEND_PM_PAYDIREKT', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_PAYPAL:
                $payment_locale = __('FRONTEND_PM_PAYPAL', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_PAYPAL_SAVED:
                $payment_locale = __('FRONTEND_PM_PAYPALSAVED', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_SOFORT:
                $payment_locale = __('FRONTEND_PM_SOFORT', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_SISSPOSTFINANCE:
                $payment_locale = __('FRONTEND_PM_SWISSPOSTFINANCE', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_SISSPOSTFINANCE:
                $payment_locale = __('FRONTEND_PM_SWISSPOSTFINANCE', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_BLIK:
                $payment_locale = __('FRONTEND_PM_BLIK', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_PAY_BY_LINK:
                $payment_locale = __('FRONTEND_PM_PBL', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_PAY_BY_LINK_KEVIN:
                $payment_locale = __('FRONTEND_PM_PBLKV', sibsConstants::SIBS_TRANSLATE);

                break;
            default:
                $payment_locale = '';

                break;
        }

        return $payment_locale;
    }

    /**
     * Translate Backend Payment Name.
     *
     * @param string $payment_id payment id.
     * @return string
     */
    public static function sibs_translate_backend_payment($payment_id)
    {
        $payment_locale = '';

        switch ($payment_id) {
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_CREDIT_CARD:
                $payment_locale = __('BACKEND_PM_CC', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_CREDIT_CARD_SAVED:
                $payment_locale = __('BACKEND_PM_CCSAVED', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_DEBIT_CARD:
                $payment_locale = __('BACKEND_PM_DC', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_DIRECT_DEBIT:
                $payment_locale = __('BACKEND_PM_DD', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_DIRECT_DEBIT_SAVED:
                $payment_locale = __('BACKEND_PM_DDSAVED', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_EASYCREDIT:
                $payment_locale = __('BACKEND_PM_EASYCREDIT', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_GIROPAY:
                $payment_locale = __('BACKEND_PM_GIROPAY', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_IDEAL:
                $payment_locale = __('BACKEND_PM_IDEAL', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_KLARNAINS:
                $payment_locale = __('BACKEND_PM_KLARNAINS', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_KLARNAINV:
                $payment_locale = __('BACKEND_PM_KLARNAINV', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_MASTERPASS:
                $payment_locale = __('BACKEND_PM_MASTERPASS', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_PAYDIREKT:
                $payment_locale = __('BACKEND_PM_PAYDIREKT', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_PAYPAL:
                $payment_locale = __('BACKEND_PM_PAYPAL', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_PAYPAL_SAVED:
                $payment_locale = __('BACKEND_PM_PAYPALSAVED', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_SOFORT:
                $payment_locale = __('BACKEND_PM_SOFORT', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_SISSPOSTFINANCE:
                $payment_locale = __('BACKEND_PM_SWISSPOSTFINANCE', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_BLIK:
                $payment_locale = __('BACKEND_PM_BLIK', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_PAY_BY_LINK:
                $payment_locale = __('BACKEND_PM_PBL', sibsConstants::SIBS_TRANSLATE);

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_PAY_BY_LINK_KEVIN:
                $payment_locale = __('BACKEND_PM_PBLKV', sibsConstants::SIBS_TRANSLATE);

                break;
            default:
                $payment_locale = '';

                break;
        }

        return $payment_locale;
    }

    /**
     * Translate Error Identifier.
     *
     * @param string $error_identifier error identifier.
     * @return string
     */
    public static function sibs_translate_error_identifier($error_identifier)
    {
        switch ($error_identifier) {
            case 'ERROR_ADDRESS_PHONE':
                $error_translate = __('ERROR_ADDRESS_PHONE', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_CAPTURE_BACKEND':
                $error_translate = __('ERROR_CAPTURE_BACKEND', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_CC_3DAUTH':
                $error_translate = __('ERROR_CC_3DAUTH', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_CC_3DERROR':
                $error_translate = __('ERROR_CC_3DERROR', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_CC_ACCOUNT':
                $error_translate = __('ERROR_CC_ACCOUNT', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_CC_ADDRESS':
                $error_translate = __('ERROR_CC_ADDRESS', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_CC_BLACKLIST':
                $error_translate = __('ERROR_CC_BLACKLIST', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_CC_DECLINED_AUTH':
                $error_translate = __('ERROR_CC_DECLINED_AUTH', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_CC_DECLINED_CARD':
                $error_translate = __('ERROR_CC_DECLINED_CARD', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_CC_EXPIRED':
                $error_translate = __('ERROR_CC_EXPIRED', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_CC_EXPIRY':
                $error_translate = __('ERROR_CC_EXPIRY', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_CC_INVALIDCVV':
                $error_translate = __('ERROR_CC_INVALIDCVV', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_CC_INVALIDDATA':
                $error_translate = __('ERROR_CC_INVALIDDATA', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_CC_LIMIT_EXCEED':
                $error_translate = __('ERROR_CC_LIMIT_EXCEED', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_CC_NOBRAND':
                $error_translate = __('ERROR_CC_NOBRAND', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_CC_RECURRING':
                $error_translate = __('ERROR_CC_RECURRING', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_CC_REPEATED':
                $error_translate = __('ERROR_CC_REPEATED', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_EASYCREDIT_BILLING_NOTEQUAL_SHIPPING':
                $error_translate = __('ERROR_EASYCREDIT_BILLING_NOTEQUAL_SHIPPING', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_EASYCREDIT_FUTURE_DOB':
                $error_translate = __('ERROR_EASYCREDIT_FUTURE_DOB', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_EASYCREDIT_PARAMETER_DOB':
                $error_translate = __('ERROR_EASYCREDIT_PARAMETER_DOB', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_GENERAL_ADDRESS':
                $error_translate = __('ERROR_GENERAL_ADDRESS', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_GENERAL_BLACKLIST':
                $error_translate = __('ERROR_GENERAL_BLACKLIST', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_GENERAL_CANCEL':
                $error_translate = __('ERROR_GENERAL_CANCEL', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_GENERAL_DECLINED_RISK':
                $error_translate = __('ERROR_GENERAL_DECLINED_RISK', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_GENERAL_GENERAL':
                $error_translate = __('ERROR_GENERAL_GENERAL', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_GENERAL_LIMIT_AMOUNT':
                $error_translate = __('ERROR_GENERAL_LIMIT_AMOUNT', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_GENERAL_LIMIT_TRANSACTIONS':
                $error_translate = __('ERROR_GENERAL_LIMIT_TRANSACTIONS', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_GENERAL_MANDATORY':
                $error_translate = __('ERROR_GENERAL_MANDATORY', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_GENERAL_NORESPONSE':
                $error_translate = __('ERROR_GENERAL_NORESPONSE', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_GENERAL_REDIRECT':
                $error_translate = __('ERROR_GENERAL_REDIRECT', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_GENERAL_TIMEOUT':
                $error_translate = __('ERROR_GENERAL_TIMEOUT', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_GIRO_NOSUPPORT':
                $error_translate = __('ERROR_GIRO_NOSUPPORT', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_MESSAGE_EASYCREDIT_AMOUNT_NOTALLOWED':
                $error_translate = __('ERROR_MESSAGE_EASYCREDIT_AMOUNT_NOTALLOWED', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_MESSAGE_EASYCREDIT_PARAMETER_GENDER':
                $error_translate = __('ERROR_MESSAGE_EASYCREDIT_PARAMETER_GENDER', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_PARAMETER_GENDER':
                $error_translate = __('ERROR_PARAMETER_GENDER', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_RECEIPT_BACKEND':
                $error_translate = __('ERROR_RECEIPT_BACKEND', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_REFUND_BACKEND':
                $error_translate = __('ERROR_REFUND_BACKEND', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_REORDER_BACKEND':
                $error_translate = __('ERROR_REORDER_BACKEND', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_WRONG_DOB':
                $error_translate = __('ERROR_WRONG_DOB', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'ERROR_UNKNOWN':
            default:
                $error_translate = __('ERROR_UNKNOWN', sibsConstants::SIBS_TRANSLATE);
        }

        return $error_translate;
    }

    /**
     * Translate sibs_term.
     *
     * @param string $sibs_term term.
     * @return string
     */
    public static function sibs_translate_sibs_term($sibs_term)
    {
        switch ($sibs_term) {
            case 'SIBS_TT_VERSIONTRACKER':
                $sibs_term_translate = __('SIBS_TT_VERSIONTRACKER', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'SIBS_BACKEND_BT_ADMIN':
                $sibs_term_translate = __('SIBS_BACKEND_BT_ADMIN', sibsConstants::SIBS_TRANSLATE);

                break;
        }

        return $sibs_term_translate;
    }

    /**
     * Translate Additional Information Name.
     *
     * @param string $info_name info name.
     * @return string
     */
    public static function sibs_translate_additional_information($info_name)
    {
        switch ($info_name) {
            case 'FRONTEND_EASYCREDIT_INTEREST':
                $additional_info_name = __('FRONTEND_EASYCREDIT_INTEREST', sibsConstants::SIBS_TRANSLATE);

                break;
            case 'FRONTEND_EASYCREDIT_TOTAL':
                $additional_info_name = __('FRONTEND_EASYCREDIT_TOTAL', sibsConstants::SIBS_TRANSLATE);

                break;
            default:
                $additional_info_name = $info_name;

                break;
        }

        return $additional_info_name;
    }

    /**
     * Get _REQUEST value.
     *
     * @param string $key key.
     * @param string $default default.
     * @return value
     */
    public static function sibs_get_request_value($key, $default = false)
    {
        if (isset($_REQUEST[$key])) {
            return sanitize_text_field(wp_unslash($_REQUEST[$key]));
        }

        return $default;
    }

    /**
     * Include template.
     *
     * @param string $template_file_path template file path (templates/template.php).
     * @param array  $args variable to include in template.
     */
    public static function sibs_include_template($template_file_path, $args = [])
    {
        if (function_exists('wc_get_template')) {
            $template      = pathinfo($template_file_path);
            $template_path = $template['dirname'] . '/';
            $template_file = $template['basename'];
            wc_get_template(
                $template_file,
                $args,
                $template_path,
                $template_path
            );
        } else {
            foreach ($args as $key => $value) {
                $$key = $value;
            }
            include $template_file_path;
        }
    }

    /**
     * Check if billing address and shipping addres are equal.
     *
     * @return bool
     */
    public static function sibs_is_address_billing_equal_shipping()
    {
        $customer                 = WC()->customer;
        $billing['street']        = "{$customer->address_1}, {$customer->address_2}";
        $billing['city']          = $customer->city;
        $billing['zip']           = $customer->postcode;
        $billing['country_code']  = $customer->country;
        $shipping['street']       = "{$customer->shipping_address_1}, {$customer->shipping_address_2}";
        $shipping['city']         = $customer->shipping_city;
        $shipping['zip']          = $customer->shipping_postcode;
        $shipping['country_code'] = $customer->shipping_country;

        foreach ($billing as $i => $bill) {
            if ($bill !== $shipping[$i]) {
                return false;
            }
        }

        return true;
    }

    /**
     * Get customer order count.
     *
     * @return int count
     */
    public static function sibs_get_order_count()
    {
        if (wc_get_customer_order_count(get_current_user_id()) > 0) {
            return wc_get_customer_order_count(get_current_user_id());
        }

        return 0;
    }

    /**
     * Get customer created date.
     *
     * @return string|bool created date
     */
    public static function sibs_get_customer_created_date()
    {
        $user_data    = get_userdata(get_current_user_id());
        $created_date = strtotime($user_data->user_registered);

        if (isset($user_data->user_registered) && $created_date > 0) {
            return date('Y-m-d', $created_date);
        }

        return date('Y-m-d');
    }

    /**
     * Convert SPG payment status to WooCommerce status by payment type.
     *
     * @param string $payment_type DPG payment type
     * @return string WooCommerce status
     */
    public static function convertTransactionStatusByPaymentType($payment_type)
    {
        if ($payment_type === sibsConstants::SPG_PAYMENT_TYPE_AUTH) {
            $payment_status = sibsConstants::SIBS_ORDER_STATUS_AUTHORIZED;
        } else {
            $payment_status = sibsConstants::WOOCOMMERCE_ORDER_STATUS_PROCESSING;
        }

        return $payment_status;
    }

    /**
     * Convert SPG payment status to WooCommerce status.
     *
     * @param string $payment_status SPG payment status
     * @param string  $payment_type SPG payment type
     * @return string WooCommerce status
     */
    public static function convertTransactionStatus($payment_status, $payment_type)
    {
        switch ($payment_status) {
            case sibsConstants::SIBS_RESULT_SUCCESS:
            case sibsConstants::SIBS_RESULT_PARTIAL:
                return self::convertTransactionStatusByPaymentType($payment_type);

                break;
            case sibsConstants::SIBS_RESULT_IN_PROCESSING:
            case sibsConstants::SIBS_RESULT_PENDING:
                return sibsConstants::WOOCOMMERCE_ORDER_STATUS_ON_HOLD;

                break;
            case sibsConstants::SIBS_RESULT_DECLINED:
            case sibsConstants::SIBS_RESULT_TIMEOUT:
            case sibsConstants::SIBS_RESULT_ERROR:
            default:
                return sibsConstants::WOOCOMMERCE_ORDER_STATUS_FAILED;

                break;
        }
    }

    /**
     * Convert Capture and Refund Status.
     *
     * @param string $payment_status webhook transaction status
     * @return string WooCommerce status
     */
    public static function convertTransactionStatusCaptRfnd($payment_status)
    {
        switch ($payment_status) {
            case sibsConstants::SIBS_RESULT_SUCCESS:
            case sibsConstants::SIBS_RESULT_PARTIAL:
                $refundStatus = sibsConstants::SIBS_PAYMENT_STATUS_REFUND_COMPLETED;

                break;
            case sibsConstants::SIBS_RESULT_IN_PROCESSING:
            case sibsConstants::SIBS_RESULT_PENDING:
                $refundStatus = sibsConstants::SIBS_PAYMENT_STATUS_REFUND_PROGRESS;

                break;
            case sibsConstants::SIBS_RESULT_DECLINED:
            case sibsConstants::SIBS_RESULT_TIMEOUT:
            case sibsConstants::SIBS_RESULT_ERROR:
                $refundStatus = sibsConstants::SIBS_PAYMENT_STATUS_REFUND_FAILED;

                break;
        }

        return $refundStatus;
    }

    /**
     * @return string
     */
    public static function sibs_spg_get_payment_method($payment_method)
    {
        switch ($payment_method) {
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_BLIK:
                $payment_method = sibsConstants::SIBS_PAYMENT_METHOD_BLIK;

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_CREDIT_CARD:
                $payment_method = sibsConstants::SIBS_PAYMENT_METHOD_CARD;

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_PAY_BY_LINK:
                $payment_method = sibsConstants::SIBS_PAYMENT_METHOD_PAY_BY_LINK;

                break;
            case sibsConstants::SIBS_PAYMENT_METHOD_ORDER_PAY_BY_LINK_KEVIN:
                $payment_method = sibsConstants::SIBS_PAYMENT_METHOD_PBLKV;

                break;
        }

        return $payment_method;
    }

    public static function processRefundSucessfull($order_id)
    {
        $args = [
            'post_parent' => $order_id,
            'post_type'   => 'shop_order_refund',
        ];

        $child_posts  = get_children($args);
        $stringSearch = esc_attr(__('Order Fully Refunded', sibsConstants::SIBS_TRANSLATE));
        $posts        = Sibs_General_Functions::sibs_search_array_by_postmeta($child_posts, $stringSearch, '_refund_reason');

        if (count($posts) > 0) {
            wp_delete_post($posts->ID, true);
        }

        sibsGeneralModels::sibs_update_db_transaction_log_status($order_id, sibsConstants::WOOCOMMERCE_ORDER_STATUS_REFUNDED);

        return true;
    }

    public static function sibs_search_array_by_postmeta($array, $string, $postmeta_key = '')
    {
        foreach ($array as $val) {
            $postmeta = get_post_meta($val->ID, $postmeta_key, true);

            if (str_contains($postmeta, $string)) {
                return $val;
            }
        }

        return null;
    }

    public static function sibs_search_array_by_value($value, $array)
    {
        foreach ($array as $val) {
            if ($val->post_excerpt === $value) {
                return $val;
            }
        }

        return null;
    }

    public static function get_page_link($page_prefix)
    {
        $location               = __('en_US', sibsConstants::SIBS_TRANSLATE);
        $location_lower         = strtolower($location);
        $page_prefix_translated = "{$page_prefix}_{$location_lower}";

        return get_permalink(get_option("{$page_prefix_translated}_id"));
    }

    public static function isAlreadyPaidOrAuthorized($order_status)
    {
        return in_array($order_status, [
            sibsConstants::WOOCOMMERCE_ORDER_STATUS_PROCESSING,
            sibsConstants::WOOCOMMERCE_ORDER_STATUS_COMPLETED,
            sibsConstants::WOOCOMMERCE_ORDER_STATUS_REFUNDED,
            sibsConstants::SIBS_ORDER_STATUS_PARTIALLY_CAPTURED,
            sibsConstants::SIBS_ORDER_STATUS_PARTIALLY_REFUNDED,
            sibsConstants::SIBS_ORDER_STATUS_AUTHORIZED,
            sibsConstants::SIBS_ORDER_STATUS_PENDING_REFUND,
        ]);
    }

    public static function isAlreadyCaptured($order_status)
    {
        return in_array($order_status, [
            sibsConstants::WOOCOMMERCE_ORDER_STATUS_PROCESSING,
            sibsConstants::WOOCOMMERCE_ORDER_STATUS_COMPLETED,
            sibsConstants::WOOCOMMERCE_ORDER_STATUS_REFUNDED,
            sibsConstants::SIBS_ORDER_STATUS_PARTIALLY_REFUNDED,
            sibsConstants::SIBS_ORDER_STATUS_PENDING_REFUND,
        ]);
    }

    public static function isAlreadyRefunded($order_status)
    {
        return in_array($order_status, [
            sibsConstants::WOOCOMMERCE_ORDER_STATUS_REFUNDED,
        ]);
    }
}
