<?php
/**
 * Mageplaza
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Mageplaza.com license that is
 * available through the world-wide-web at this URL:
 * https://www.mageplaza.com/LICENSE.txt
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade this extension to newer
 * version in the future.
 *
 * @category  Mageplaza
 * @package   Mageplaza_EditOrder
 * @copyright Copyright (c) Mageplaza (https://www.mageplaza.com/)
 * @license   https://www.mageplaza.com/LICENSE.txt
 */

namespace Mageplaza\EditOrder\Plugin\Model\Quote\Item\QuantityValidator\Initializer;

use Closure;
use Magento\CatalogInventory\Api\Data\StockItemInterface;
use Magento\CatalogInventory\Model\Quote\Item\QuantityValidator\Initializer\StockItem;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\InventoryConfigurationApi\Exception\SkuIsNotAssignedToStockException;
use Magento\Quote\Model\Quote\Item;
use Mageplaza\EditOrder\Helper\Data;
use Mageplaza\EditOrder\Model\StockStateResult;

/**
 * Class CustomStockItem
 * @package Mageplaza\EditOrder\Plugin\Model\Quote\Item\QuantityValidator\Initializer
 */

class CustomStockItem
{
    /**
     * @var RequestInterface
     */
    protected $request;
    /**
     * @var Data
     */
    protected $helperData;

    /**
     * @var StockStateResult
     */
    protected $stockStateResult;

    /**
     * @param RequestInterface $request
     * @param Data $helperData
     */
    public function __construct(
        RequestInterface $request,
        Data $helperData
    ) {
        $this->request    = $request;
        $this->helperData = $helperData;
    }

    /**
     * @param StockItem $subject
     * @param Closure $proceed
     * @param StockItemInterface $stockItem
     * @param Item $quoteItem
     * @param $qty
     *
     * @return StockStateResult|mixed
     * @throws InputException
     * @throws LocalizedException
     * @throws NoSuchEntityException
     * @throws SkuIsNotAssignedToStockException
     */
    public function aroundInitialize(
        StockItem $subject,
        Closure $proceed,
        StockItemInterface $stockItem,
        Item $quoteItem,
        $qty
    ) {
        $request = explode('_', $this->request->getFullActionName());
        if (!in_array("mpeditorder", $request)) {
            return $proceed($stockItem, $quoteItem, $qty);
        }

        if ($this->request->getParam('order_id') !== null && $this->helperData->isEnabled()) {
            $requestedQty = $qty;
            if ($this->helperData->isBackorderAllowed($requestedQty, $quoteItem)) {
                $this->stockStateResult = new StockStateResult([
                    'has_error' => false
                ]);
            } else {
                $this->stockStateResult = new StockStateResult([
                    'has_error' => true,
                    'message' => 'The requested qty is not available',
                    'quote_message' => 'The requested qty is not available',
                    'quote_message_index' => 'qty',
                    'error_code' => 'is_salable_with_reservations-not_enough_qty',
                ]);
            }

            $quoteItem->setStockStateResult($this->stockStateResult);

            return $this->stockStateResult;
        }
        return $proceed($stockItem, $quoteItem, $qty);
    }
}
