<?php

namespace Sibs\Stargate\Api;

use Sibs\Stargate\Constants\PaymentTypes;
use Sibs\Stargate\Constants\WoocommerceOrderStatus;
use Sibs\Stargate\Constants\WoocommercePaymentMethods;
use Sibs\Stargate\Core\SibsPaymentCore;
use Sibs\Stargate\Exceptions\SibsException;
use Sibs\Stargate\Loggers\SibsLogger;
use WC_Order;

defined('ABSPATH') || exit;

abstract class ApiAbstract
{
    /**
     * Get Backoffice Config.
     *
     * @param float $amount
     *
     * @return array
     */
    protected static function get_backoffice_config(
        array $sibs_payment,
        string $payment_type,
        $amount,
        string $error_message = 'Sibs Error'
    ) {
        $wc_order = wc_get_order($sibs_payment['order_no'] ?? null);
        $reference_id = $sibs_payment['reference_id'];

        switch ($payment_type) {
            case PaymentTypes::RVSL:
            case PaymentTypes::CAUT:
                $order_status = WoocommerceOrderStatus::CANCELLED;

                break;

            case PaymentTypes::RFND:
                $order_status = self::get_order_refunded_status($wc_order, $sibs_payment['payment_id']);

                if (PaymentTypes::AUTH === $sibs_payment['payment_type']) {
                    $reference_id = $sibs_payment['captured_id'];
                }

                break;

            default:
                $order_status = WoocommerceOrderStatus::PROCESSING;

                break;
        }

        return [
            'order_no' => $wc_order->get_id(),
            'payment_id' => $sibs_payment['payment_id'],
            'reference_id' => $reference_id,
            'currency' => $sibs_payment['currency'],
            'payment_type' => $payment_type,
            'order_status' => $order_status,
            'error_message' => $error_message,
            'amount' => $amount,
        ];
    }

    /**
     * SIBS Do Backoffice Payment.
     *
     * @return array
     */
    protected static function sibs_do_back_office_payment(WC_Order $wc_order, array $backoffice_config)
    {
        $backoffice_parameter = [
            'currency' => $backoffice_config['currency'],
            'payment_type' => $backoffice_config['payment_type'],
            'amount' => $backoffice_config['amount'],
        ];

        return SibsPaymentCore::sibs_back_office_operation(
            $wc_order,
            $backoffice_config['reference_id'],
            $backoffice_parameter
        );
    }

    /**
     * Get Order Refunded Status.
     */
    protected static function get_order_refunded_status(WC_Order $wc_order, string $payment_method)
    {
        $async_refund = [
            WoocommercePaymentMethods::BLIK,
        ];

        if (in_array($payment_method, $async_refund)) {
            SibsLogger::debug('[process_refund] Order Fully Refunded (Async)');

            return WoocommerceOrderStatus::REFUNDED;
        }

        if (self::is_fully_refunded($wc_order)) {
            SibsLogger::debug('[process_refund] Order Fully Refunded');

            return WoocommerceOrderStatus::REFUNDED;
        }
        if (self::is_partially_refunded($wc_order)) {
            SibsLogger::debug('[process_refund] Order Partially Refunded');

            return WoocommerceOrderStatus::PARTIALLY_REFUNDED;
        }

        throw new SibsException(
            __('Order is not fully or partially refunded. Amount Value must be wrong', 'sibs-woo-stg')
        );
    }

    /**
     * Is Fully Refunded.
     *
     * @return bool
     */
    protected static function is_fully_refunded(WC_Order $wc_order)
    {
        SibsLogger::debug('[is_fully_refunded] get_remaining_refund_items: '.$wc_order->get_remaining_refund_items());
        SibsLogger::debug('[is_fully_refunded] get_remaining_refund_amount: '.$wc_order->get_remaining_refund_amount());

        return
            0 === $wc_order->get_remaining_refund_items()
            || 0 == $wc_order->get_remaining_refund_amount();
    }

    /**
     * Is Partially Refunded.
     *
     * @return bool
     */
    protected static function is_partially_refunded(WC_Order $wc_order)
    {
        SibsLogger::debug('[is_partially_refunded] get_item_count_refunded: '.$wc_order->get_item_count_refunded());
        SibsLogger::debug('[is_partially_refunded] get_total_refunded: '.$wc_order->get_total_refunded());

        $not_fully_refunded = !self::is_fully_refunded($wc_order);
        $refunded_something = (
            $wc_order->get_item_count_refunded() > 0
            || $wc_order->get_total_refunded() > 0
        );

        return $not_fully_refunded && $refunded_something;
    }

    /**
     * Print Button.
     *
     * @param int    $order_id
     * @param string $button_id
     * @param string $label
     */
    protected static function print_button($order_id, $button_id, $label)
    {
        ?>
            <button
                type="button"
                style="margin-right: 2px"
                class="button sibs-button"
                data-order_id="<?php echo $order_id; ?>"
                data-button_id="<?php echo $button_id; ?>">
                <b><?php echo $label; ?></b>
            </button>
        <?php
    }
}
