<?php
/**
 * Copyright ©  All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Leadlion\AutogedalWarehouses\Controller\Adminhtml\Warehouse;

use Magento\Framework\Exception\LocalizedException;

class OrderSave extends \Magento\Backend\App\Action
{

    protected $dataPersistor;
    protected $orderRepository;
    protected $adminQuoteSession;
    protected $quoteFactory;
    protected $quoteRepository;
    protected $scopeConfig;

    /**
     * @param \Magento\Backend\App\Action\Context $context
     * @param \Magento\Framework\App\Request\DataPersistorInterface $dataPersistor
     */
    public function __construct(
        \Magento\Backend\App\Action\Context $context,
        \Magento\Framework\App\Request\DataPersistorInterface $dataPersistor,
        \Magento\Backend\Model\Session\Quote $adminQuoteSession,
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
        \Magento\Quote\Model\QuoteFactory $quoteFactory,
        \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
    ) {
        $this->dataPersistor = $dataPersistor;
        $this->orderRepository = $orderRepository;
        $this->adminQuoteSession = $adminQuoteSession;
        $this->quoteFactory = $quoteFactory;
        $this->quoteRepository = $quoteRepository;
        $this->scopeConfig = $scopeConfig;
        parent::__construct($context);
    }

    /**
     * Save action
     *
     * @return \Magento\Framework\Controller\ResultInterface
     */
    public function execute()
    {
        /** @var \Magento\Backend\Model\View\Result\Redirect $resultRedirect */
        $resultRedirect = $this->resultRedirectFactory->create();
        $data = $this->getRequest()->getPostValue();
        $orderId = $this->getRequest()->getParam('order_id');
        $warehouseAllocation = $this->getRequest()->getParam('warehouse');
        $carrierAllocation = $this->getRequest()->getParam('carrier');
        $quoteId = $this->adminQuoteSession->getQuote()->getEntityId();
        $warehouseUpdateId = $this->getRequest()->getParam('warehouse_update');
        $packagesNumber = $this->getRequest()->getParam('packages_number');
        $orderStatusBackend = $this->scopeConfig->getValue('orderflow/order_status/backend_pending_status', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

        if ($warehouseUpdateId == true) {
            $orderObj = $this->orderRepository->get($orderId);
            $orderObj->setData('autogedal_allocated_carrier', $carrierAllocation)->setData('autogedal_allocated_warehouse', $warehouseAllocation);
            $orderObj->setData('packages_number', $packagesNumber);
            $this->orderRepository->save($orderObj);
        } else {
            if (!empty($quoteId)) {
            $quoteObj = $this->quoteRepository->get($quoteId);
            $quoteObj->setData('autogedal_allocated_carrier', $carrierAllocation)->setData('autogedal_allocated_warehouse', $warehouseAllocation);
            $this->quoteRepository->save($quoteObj);
            }
        }
    }
}
