<?php

namespace Sibs\Stargate\Core;

use Sibs\Stargate\Config\SibsConfig;
use Sibs\Stargate\Constants\OrderStatus;
use Sibs\Stargate\Constants\PaymentMethods;
use Sibs\Stargate\Constants\PaymentStatus;
use Sibs\Stargate\Constants\PaymentTypes;
use Sibs\Stargate\Constants\RefundStatus;
use Sibs\Stargate\Constants\WoocommerceOrderStatus;
use Sibs\Stargate\Constants\WoocommercePaymentMethods;

defined('ABSPATH') || exit;

class SibsGeneralFunctions
{
    /**
     * @var int
     */
    public static $percent = 100;

    /**
     * SIBS Get Payment Type.
     *
     * @param string $payment_id
     *
     * @return string
     */
    public static function sibs_get_payment_type($payment_id)
    {
        $payment_auth_db = [
            WoocommercePaymentMethods::CARD,
            // WoocommercePaymentMethods::XPAY,
            // WoocommercePaymentMethods::APAY,
            // WoocommercePaymentMethods::GPAY,
        ];

        if (in_array($payment_id, $payment_auth_db)) {
            $trans_mode = SibsConfig::getPaymentOption($payment_id, 'trans_mode');

            return !empty($trans_mode) ? $trans_mode : PaymentTypes::PURS;
        }

        return PaymentTypes::PURS;
    }

    /**
     * Translate Frontend Payment Name.
     *
     * @return string
     */
    public static function sibs_translate_frontend_payment(\WC_Order $wc_order)
    {
        $payment_locale = '';

        switch ($wc_order->get_payment_method()) {
            case WoocommercePaymentMethods::CARD:
                $payment_locale = __('Cards', 'sibs-woo-stg');

                break;

            case WoocommercePaymentMethods::BLIK:
                $payment_locale = __('BLIK', 'sibs-woo-stg');

                break;

            case WoocommercePaymentMethods::PAY_BY_LINK:
                $payment_locale = __('Pay by Link', 'sibs-woo-stg');

                break;

            case WoocommercePaymentMethods::PAY_BY_LINK_KEVIN:
                $payment_locale = __('Pay by Link', 'sibs-woo-stg');

                break;

            case WoocommercePaymentMethods::XPAY:
                $payment_locale = __('Google Pay / Apple Pay', 'sibs-woo-stg');

                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 WoocommercePaymentMethods::CARD:
                $payment_locale = __('SIBS Cards', 'sibs-woo-stg');

                break;

            case WoocommercePaymentMethods::BLIK:
                $payment_locale = __('SIBS BLIK', 'sibs-woo-stg');

                break;

            case WoocommercePaymentMethods::PAY_BY_LINK:
                $payment_locale = __('SIBS Pay By Link', 'sibs-woo-stg');

                break;

            case WoocommercePaymentMethods::PAY_BY_LINK_KEVIN:
                $payment_locale = __('SIBS Pay By Link', 'sibs-woo-stg');

                break;

            case WoocommercePaymentMethods::XPAY:
                $payment_locale = __('SIBS Google Pay / Apple Pay', 'sibs-woo-stg');

                break;

            default:
                $payment_locale = '';

                break;
        }

        return $payment_locale;
    }

    /**
     * Backend Payment Description.
     *
     * @param string $payment_id payment id
     *
     * @return string
     */
    public static function sibs_backend_payment_description($payment_id)
    {
        $payment_description = '';

        switch ($payment_id) {
            case WoocommercePaymentMethods::CARD:
                $payment_description = __('Accept Card payments from VISA and MasterCard', 'sibs-woo-stg');

                break;

            case WoocommercePaymentMethods::BLIK:
                $payment_description = __('Allow your clients to purchase online by authorizing the purchase in the Blik app', 'sibs-woo-stg');

                break;

            case WoocommercePaymentMethods::PAY_BY_LINK:
                $payment_description = __('Allow your clients to authorize purchase through Pay By Link', 'sibs-woo-stg');

                break;

            case WoocommercePaymentMethods::PAY_BY_LINK_KEVIN:
                $payment_description = __('Allow your clients to authorize purchase through Pay By Link Kevin', 'sibs-woo-stg');

                break;

            case WoocommercePaymentMethods::XPAY:
                $payment_description = __('Allow your clients to purchase through Xpay', 'sibs-woo-stg');

                break;

            case WoocommercePaymentMethods::APAY:
                $payment_description = __('Allow your clients to purchase through Apple Pay', 'sibs-woo-stg');

                break;

            case WoocommercePaymentMethods::GPAY:
                $payment_description = __('Allow your clients to purchase through Google Pay', 'sibs-woo-stg');

                break;

            default:
                $payment_description = '';
        }

        return $payment_description;
    }

    /**
     * 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 'Interest':
                return __('Interest', 'sibs-woo-stg');

            case 'Order Total':
                return __('Order Total', 'sibs-woo-stg');

            default:
                return $info_name;
        }
    }

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

        return $default;
    }

    /**
     * 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)
    {
        return (PaymentTypes::AUTH === $payment_type)
            ? WoocommerceOrderStatus::AUTHORIZED
            : WoocommerceOrderStatus::PROCESSING;
    }

    /**
     * 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 PaymentStatus::SUCCESS:
            case PaymentStatus::PARTIAL:
                return self::convertTransactionStatusByPaymentType($payment_type);

            case PaymentStatus::IN_PROCESSING:
            case PaymentStatus::PENDING:
                return WoocommerceOrderStatus::ON_HOLD;

            case PaymentStatus::DECLINED:
            case PaymentStatus::TIMEOUT:
            case PaymentStatus::ERROR:
            default:
                return WoocommerceOrderStatus::FAILED;
        }
    }

    /**
     * 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 PaymentStatus::SUCCESS:
            case PaymentStatus::PARTIAL:
                return RefundStatus::COMPLETED;

            case PaymentStatus::IN_PROCESSING:
            case PaymentStatus::PENDING:
                return RefundStatus::PROGRESS;

            case PaymentStatus::DECLINED:
            case PaymentStatus::TIMEOUT:
            case PaymentStatus::ERROR:
                return RefundStatus::FAILED;
        }
    }

    /**
     * SIBS SPG get Payment Method.
     *
     * @param mixed $payment_method
     *
     * @return string
     */
    public static function sibs_spg_get_payment_method($payment_method)
    {
        switch ($payment_method) {
            case WoocommercePaymentMethods::BLIK:
                return PaymentMethods::BLIK;

            case WoocommercePaymentMethods::CARD:
                return PaymentMethods::CARD;

            case WoocommercePaymentMethods::PAY_BY_LINK:
                return PaymentMethods::PAY_BY_LINK;

            case WoocommercePaymentMethods::PAY_BY_LINK_KEVIN:
                return PaymentMethods::PAY_BY_LINK_KEVIN;

            case WoocommercePaymentMethods::XPAY:
                return PaymentMethods::XPAY;

            case WoocommercePaymentMethods::GPAY:
                return PaymentMethods::GPAY;

            case WoocommercePaymentMethods::APAY:
                return PaymentMethods::APAY;

            default:
                return $payment_method;
        }
    }

    /**
     * Get Page Link.
     *
     * @param string $page_prefix
     *
     * @return false|string
     */
    public static function get_page_link($page_prefix)
    {
        $location = __('en_US', 'sibs-woo-stg');
        $location_lower = strtolower($location);
        $page_prefix_translated = "{$page_prefix}_{$location_lower}";

        return get_permalink(SibsConfig::getWoocommerceOption("{$page_prefix_translated}_id"));
    }

    /**
     * Is Already Paid Or Authorized.
     *
     * @return bool
     */
    public static function isAlreadyPaidOrAuthorized(\WC_Order $wc_order)
    {
        return in_array($wc_order->get_status(), [
            WoocommerceOrderStatus::PROCESSING,
            WoocommerceOrderStatus::COMPLETED,
            WoocommerceOrderStatus::REFUNDED,
            WoocommerceOrderStatus::PARTIALLY_CAPTURED,
            WoocommerceOrderStatus::PARTIALLY_REFUNDED,
            WoocommerceOrderStatus::AUTHORIZED,
            WoocommerceOrderStatus::PENDING_REFUND,
        ]);
    }

    /**
     * Is Already Captured.
     *
     * @return bool
     */
    public static function isAlreadyCaptured(\WC_Order $wc_order)
    {
        return in_array($wc_order->get_status(), [
            WoocommerceOrderStatus::PROCESSING,
            WoocommerceOrderStatus::COMPLETED,
            WoocommerceOrderStatus::REFUNDED,
            WoocommerceOrderStatus::PARTIALLY_REFUNDED,
            WoocommerceOrderStatus::PENDING_REFUND,
        ]);
    }

    /**
     * Is Already Refunded.
     *
     * @return bool
     */
    public static function isAlreadyRefunded(\WC_Order $wc_order)
    {
        return in_array($wc_order->get_status(), [
            OrderStatus::REFUNDED,
        ]);
    }
}
