<?php
/**
 * Update quote action from customer
 *
 * @category  Magetop Extensions
 * @package   Magetop_Quotesystem
 * @author    Magetop Developer
 * @copyright Copyright (c) Magetop (https://www.magetop.com)
 * @license   https://www.magetop.com/license.html
 */

namespace Magetop\Quotesystem\Controller\Buyerquote;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\RequestInterface;
use Magetop\Quotesystem\Model\QuotesFactory;
use Magetop\Quotesystem\Helper;
use Magento\Catalog\Model\ProductFactory;
use Magetop\Quotesystem\Model\QuoteconversationFactory;
use Magento\Customer\Model\Url;
use Magetop\Quotesystem\Api\QuoteRepositoryInterface;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;

class UpdateQuoteItem extends \Magento\Customer\Controller\AbstractAccount
{
    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $_customerSession;

    /**
     * @var \Magento\Catalog\Model\Product
     */
    protected $_catalogProduct;

    /**
     * @var \Magetop\Quotesystem\Model\Quotes
     */
    protected $_quoteFactory;

    /**
     * @var \Magetop\Quotesystem\Helper\Mail
     */
    protected $_mailHelper;

    /**
     * @var QuotesFactory
     */
    protected $_quotesFactory;

    /**
     * @var QuoteconversationFactory
     */
    protected $_quoteconversationFactory;

    /**
     * @var \Magento\Framework\Stdlib\DateTime\DateTime
     */
    protected $_date;

    /**
     * @var Magento\Customer\Model\Url
     */
    protected $_customerUrl;

    /**
     * @var Magetop\Quotesystem\Api\QuoteRepositoryInterface
     */
    protected $_quoteRepository;

    /**
     * @var Magetop\Quotesystem\Helper\Data
     */
    protected $_quoteHelper;

    /**
     * @var TimezoneInterface
     */
    protected $_timezoneinterface;

    /**
     * @param Context                                     $context
     * @param \Magento\Catalog\Model\ProductFactory       $catalogProduct
     * @param \Magento\Framework\Stdlib\DateTime\DateTime $date
     * @param \Magento\Customer\Model\Session             $customerSession
     * @param \Magetop\Quotesystem\Model\QuotesFactory     $quotes
     * @param QuoteconversationFactory                    $quoteConversationFactory
     * @param Helper\Mail                                 $helperMail
     * @param Url                                         $customerModelUrl
     * @param QuoteRepositoryInterface                    $quoteRepository
     * @param Helper\Data                                 $quoteHelper
     * @param TimezoneInterface                           $timezoneinterface
     */
    public function __construct(
        Context $context,
        \Magento\Catalog\Model\ProductFactory $catalogProduct,
        \Magento\Framework\Stdlib\DateTime\DateTime $date,
        \Magento\Customer\Model\Session $customerSession,
        \Magetop\Quotesystem\Model\QuotesFactory $quotes,
        QuoteconversationFactory $quoteConversationFactory,
        Helper\Mail $helperMail,
        Url $customerModelUrl,
        QuoteRepositoryInterface $quoteRepository,
        Helper\Data $quoteHelper,
        TimezoneInterface $timezoneinterface
    ) {
        $this->_customerSession = $customerSession;
        $this->_catalogProduct = $catalogProduct;
        $this->_date = $date;
        $this->_quoteFactory = $quotes;
        $this->_mailHelper = $helperMail;
        $this->_quoteconversationFactory = $quoteConversationFactory;
        $this->_customerUrl = $customerModelUrl;
        $this->_quoteRepository = $quoteRepository;
        $this->_quoteHelper = $quoteHelper;
        $this->_timezoneinterface = $timezoneinterface;
        parent::__construct($context);
    }

    /**
     * Save quote from buyer.
     *
     * @return \Magento\Framework\View\Result\Page
     */
    public function execute()
    {
        if (!$this->getRequest()->isGet()) {
            return $this->resultRedirectFactory
                ->create()->setPath(
                    'quotesystem/buyerquote/index/'
                );
        }
        $wholedata = $this->getRequest()->getParams();
        $quoteSuccess = 0;
        $messageSuccess = 0;
        if ($wholedata && array_key_exists('quote_id', $wholedata)) {
            $quote = $this->_quoteRepository->getById($wholedata['quote_id']);
            $quoteProduct = $this->_quoteHelper->getProduct($quote->getProductId());
            if (isset($wholedata['quote_qty']) && isset($wholedata['quote_price'])) {
                if (($quoteProduct->getMinQuoteQty() <= $wholedata['quote_qty']) && $wholedata['quote_price'] > 0) {
                        $quote->setQuoteQty($wholedata['quote_qty'])
                            ->setQuotePrice($wholedata['quote_price'])
                            ->save();
                        $quoteSuccess = 1;
                        $this->messageManager->addSuccess(__('Pending Quote Successfully updated'));
                } else {
                    $this->messageManager->addError(
                        __(
                            'Sorry!! Quote Quantity should not be less than %1.',
                            $quoteProduct->getMinQuoteQty()
                        )
                    );
                }
            }
             
            return $this->resultRedirectFactory
                ->create()->setPath(
                    'quotesystem/buyerquote/viewquotes/',
                    [
                    '_secure' => $this->getRequest()->isSecure(),
                    ]
                );
        } else {
            $this->messageManager->addError(__('Something Went Wrong, Please try again later.'));
            return $this->resultRedirectFactory
                ->create()->setPath(
                    'quotesystem/buyerquote/viewquotes/',
                    [
                        '_secure' => $this->getRequest()->isSecure()
                    ]
                );
        }
        return $this->resultRedirectFactory
            ->create()->setPath(
                'quotesystem/buyerquote/viewquotes/',
                [
                    '_secure' => $this->getRequest()->isSecure()
                ]
            );
    }
}
