<?php
/**
 * Update miniquote.
 *
 * @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\Sidebar;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Response\Http;
use Psr\Log\LoggerInterface;
use Magetop\Quotesystem\Model\QuotesFactory;

class GenerateQuote extends \Magento\Customer\Controller\AbstractAccount
{
    /**
     * @var LoggerInterface
     */
    protected $logger;

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

    /**
     * construct
     *
     * @param Context $context
     * @param LoggerInterface $logger
     * @param QuotesFactory $quoteFactory
     * @param \Magetop\Quotesystem\Model\QuoteDetailsFactory $mtQuotes
     * @param \Magetop\Quotesystem\Helper\Data $helper
     * @param \Magento\Customer\Model\Session $customerSession
     * @param \Magetop\Quotesystem\Helper\Mail $helperMail
     */
    public function __construct(
        Context $context,
        LoggerInterface $logger,
        QuotesFactory $quoteFactory,
        \Magetop\Quotesystem\Model\QuoteDetailsFactory $mtQuotes,
        \Magetop\Quotesystem\Helper\Data $helper,
        \Magento\Customer\Model\Session $customerSession,
        \Magetop\Quotesystem\Helper\Mail $helperMail
    ) {
        $this->logger = $logger;
        $this->quoteFactory = $quoteFactory;
        $this->mtQuotes = $mtQuotes;
        $this->_helper = $helper;
        $this->_customerSession = $customerSession;
        $this->_mailHelper = $helperMail;
        parent::__construct($context);
    }

    /**
     * Generate Quote
     *
     * @return $this
     */
    public function execute()
    {
        $customerId = $this->_customerSession->getCustomerId();
        try {
            $quoteDetailsColl = $this->mtQuotes->create()->getCollection()
                                                ->addFieldToFilter('customer_id', $customerId)
                                                ->addFieldToFilter('is_active', 1);
            
            if ($quoteDetailsColl->getSize()) {
                $quoteId = $quoteDetailsColl->getFirstItem()->getId();
                $quotes = $this->quoteFactory->create()->getCollection()
                                              ->addFieldToFilter('quote_id', $quoteId);
                foreach ($quotes as $quote) {
                    $currency = $quote->getQuoteCurrency();
                    $baseCurrency = $this->_helper->getBaseCurrencyCode();
                    if ($currency != $baseCurrency) {
                        $quote = $this->quoteFactory->create()->load($quote->getEntityId());
                        $quote->setQuoteCurrency($baseCurrency);
                        $quote->setQuotePrice($this->_helper->getBaseCurrencyPrice($quote->getQuotePrice(), $currency));
                        $quote->save();
                    }
                    $this->_mailHelper->newQuote($quote->getEntityId());
                }
                $quoteModel = $this->mtQuotes->create()->load($quoteId);
                $quoteModel->setQuoteGenerate(1)
                            ->setIsActive(0);
                $quoteModel->save();
                $this->messageManager->addSuccess(__('Quote is successfully Saved.'));
            } else {
                $this->messageManager->addError(__('There is no product in the quote.'));
            }
            
        } catch (LocalizedException $e) {
            $this->messageManager->addError(__('Quote is not Saved.'));
            $this->logger->critical($e);
        } catch (\Exception $e) {
            $this->messageManager->addError(__('Quote is not Saved.'));
            $this->logger->critical($e);
        }
        return $this->resultRedirectFactory
                    ->create()->setPath(
                        'quotesystem/buyerquote/index',
                        ['_secure'=>$this->getRequest()->isSecure()]
                    );
    }
}
