<?php
/**
 * Quote Delete from buyer end.
 *
 * @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\Customer\Model\Session;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Framework\App\RequestInterface;
use Magetop\Quotesystem\Api\QuoteRepositoryInterface;
use Magento\Customer\Model\Url;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\App\Response\RedirectInterface;

class Delete extends \Magento\Customer\Controller\AbstractAccount
{
    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $_customerSession;
    /**
     * @var PageFactory
     */
    protected $_resultPageFactory;
    /**
     * @var Magetop\Quotesystem\Api\QuoteRepositoryInterface
     */
    protected $_quoteRepository;
     /**
      * @var \Magento\Store\Model\StoreManagerInterface
      */
    protected $_storeManagerInterface;
    /**
     * @var RedirectInterface
     */
    protected $_redirectInterface;

    /**
     * @param Context                  $context
     * @param Session                  $customerSession
     * @param PageFactory              $resultPageFactory
     * @param QuoteRepositoryInterface $quoteRepository
     * @param StoreManagerInterface    $storeManagerInterface
     * @param RedirectInterface        $redirectInterface
     */
    public function __construct(
        Context $context,
        Session $customerSession,
        PageFactory $resultPageFactory,
        QuoteRepositoryInterface $quoteRepository,
        StoreManagerInterface $storeManagerInterface,
        RedirectInterface $redirectInterface
    ) {
        $this->_customerSession = $customerSession;
        $this->_resultPageFactory = $resultPageFactory;
        $this->_quoteRepository = $quoteRepository;
        $this->_storeManagerInterface = $storeManagerInterface;
        $this->_redirectInterface = $redirectInterface;
        parent::__construct(
            $context
        );
    }

    /**
     * Retrieve customer session object
     *
     * @return \Magento\Customer\Model\Session
     */
    protected function _getSession()
    {
        return $this->_customerSession;
    }

    /**
     * Delete quote from model.
     *
     * @return \Magento\Framework\View\Result\Page
     */
    public function execute()
    {
        try {
            $quoteId = $this->getRequest()->getParam('id', false);
            $customerSessionId = $this->_customerSession->getCustomerId();
            $qouteCustomerId = $this->_quoteRepository->getCustomerIdByQuoteId($quoteId);
            $resultRedirect = $this->resultFactory->create(\Magento\Framework\Controller\ResultFactory::TYPE_REDIRECT);
            $storeUrl = $this->_storeManagerInterface->getStore()->getBaseUrl();
            if ($quoteId && ($this->_redirectInterface->getRefererUrl() != $storeUrl)) {
                if ($customerSessionId == $qouteCustomerId) {
                    $this->_quoteRepository->deleteById($quoteId);
                    $this->messageManager->addSuccess(__('Quote is successfully deleted.'));
                } else {
                    $this->messageManager->addError(__('You are not allowed to delete the quotes of other.'));
                }
            } else {
                $this->messageManager->addError(__('Quote Does Not exists.'));
            }
        } catch (\Exception $e) {
            $this->messageManager->addException($e, $e->getMessage());
        }
        return $this->resultRedirectFactory
            ->create()->setPath(
                '*/*/index',
                ['_secure'=>$this->getRequest()->isSecure()]
            );
    }
}
