<?php

namespace Sibs\Sibs\Plugin\Widget;

use Magento\Backend\Block\Widget\Context as MagentoContext;
use Magento\Framework\UrlInterface;
use Sibs\Sibs\Helper\DataHelper as PaymentHelper;
use Sibs\Sibs\Model\SibsConstants;
use Sibs\Sibs\Model\SibsOrder;

class Context
{
    protected $_urlBuilder;

    protected $_paymentHelper;

    protected $_sibsOrder;

    public function __construct(UrlInterface $urlBuilder, PaymentHelper $paymentHelper)
    {
        $this->_urlBuilder    = $urlBuilder;
        $this->_paymentHelper = $paymentHelper;
    }

    public function afterGetButtonList(MagentoContext $subject, $buttonList)
    {
        if ($subject->getRequest()->getFullActionName() == 'sales_order_view') {
            $orderId          = $subject->getRequest()->getParam('order_id');

            $this->_sibsOrder = new SibsOrder($orderId);

            if ($this->_sibsOrder->isSibsOrder()) {
                if ($this->canBeCaptured()) {
                    $buttonList->add('capture_payment', $this->getCaptureAttributes($orderId));
                } elseif ($this->canBeRefunded()) {
                    $buttonList->add('sibs_refund', $this->getRefundAttributes($orderId));
                } elseif ($this->canBeIquired()) {
                    $buttonList->add('sibs_order_status', $this->getStatusAttributes($orderId));
                }
            }
        }

        return $buttonList;
    }

    // PAYMENT TYPES
    public function isPurchase()
    {
        return $this->_sibsOrder->getPaymentType() == SibsConstants::SIBS_PAYMENT_TYPE_PURS;
    }

    public function isAuthorization()
    {
        return $this->_sibsOrder->getPaymentType() == SibsConstants::SIBS_PAYMENT_TYPE_AUTH;
    }

    // PAYMENT METHODS
    public function isCreditcard()
    {
        return $this->_sibsOrder->getPaymentMethodCode() == SibsConstants::SIBS_PAYMENT_METHOD_CREDITCARD;
    }

    public function isBlik()
    {
        return $this->_sibsOrder->getPaymentMethodCode() == SibsConstants::SIBS_PAYMENT_METHOD_BLIK;
    }

    public function isPayByLink()
    {
        return $this->_sibsOrder->getPaymentMethodCode() == SibsConstants::SIBS_PAYMENT_METHOD_PAY_BY_LINK;
    }

    public function isXpay()
    {
        return $this->_sibsOrder->getPaymentMethodCode() == SibsConstants::SIBS_PAYMENT_METHOD_XPAY;
    }

    // PAYMENT STATUS
    public function isAuthorized()
    {
        return $this->_sibsOrder->getStatus() == SibsConstants::SIBS_STATUS_AUTHORIZED;
    }

    public function isProcessing()
    {
        return $this->_sibsOrder->getStatus() == SibsConstants::SIBS_STATUS_PROCESSING;
    }

    public function isHolded()
    {
        return $this->_sibsOrder->getStatus() == SibsConstants::SIBS_STATUS_HOLDED;
    }

    public function isCaptureHolded()
    {
        return $this->_sibsOrder->getStatus() == SibsConstants::SIBS_STATUS_CAPTURE_HOLDED;
    }

    public function isRefundHolded()
    {
        return $this->_sibsOrder->getStatus() == SibsConstants::SIBS_STATUS_REFUND_HOLDED;
    }

    public function isCanceled()
    {
        return $this->_sibsOrder->getStatus() == SibsConstants::SIBS_STATUS_CANCELED;
    }

    // VALIDATIONS

    public function canBeIquired()
    {
        $orderStatus = $this->_sibsOrder->getStatus();

        return in_array($orderStatus, [
            SibsConstants::SIBS_STATUS_HOLDED,
            SibsConstants::SIBS_STATUS_CAPTURE_HOLDED,
            SibsConstants::SIBS_STATUS_REFUND_HOLDED,
        ]);
    }

    public function canBeCaptured()
    {
        $captureStatus = $this->_sibsOrder->getCaptureStatus();

        return
            $this->isAuthorized() && $this->isAuthorization() && $this->isCreditcard() &&
            (empty($captureStatus) || $captureStatus == SibsConstants::MAGENTO_STATE_CANCELED);
    }

    public function canBeRefunded()
    {
        $refundStatus = $this->_sibsOrder->getRefundedStatus();

        return
            $this->isProcessing() &&
            (empty($refundStatus) || $refundStatus == SibsConstants::MAGENTO_STATE_CANCELED);
    }

    // ATTRIBUTES
    private function getStatusAttributes($order_id)
    {
        $statusUrl = $this->getStatusUrl($order_id);
        $text      = __('Are you sure you want to inquiry the payment of the order #') . "{$order_id} ?";

        return ['label' => __('SIBS Status'), 'onclick' => "confirmSetLocation('$text', '$statusUrl')"];
    }

    private function getCaptureAttributes($order_id)
    {
        $captureUrl = $this->getCaptureUrl($order_id);
        $text       = __('Are you sure you want to capture the payment of the order #') . "{$order_id} ?";

        return ['label' => __('SIBS Capture'), 'onclick' => "confirmSetLocation('$text', '$captureUrl')"];
    }

    private function getRefundAttributes($order_id)
    {
        $refundUrl = $this->getRefundUrl($order_id);
        $text      = __('Are you sure you want to refund the payment of the order #') . "{$order_id} ?";

        return ['label' => __('SIBS Refund'), 'onclick' => "confirmSetLocation('$text', '$refundUrl')"];
    }

    // URLS
    private function getStatusUrl($order_id)
    {
        return $this->_urlBuilder->getUrl('sibs/payments/orderstatus', ['order_id' => $order_id]);
    }

    private function getCaptureUrl($order_id)
    {
        return $this->_urlBuilder->getUrl('sibs/payments/capture', ['order_id' => $order_id]);
    }

    private function getRefundUrl($order_id)
    {
        return $this->_urlBuilder->getUrl('sibs/payments/refund', ['order_id' => $order_id]);
    }
}
