<?php

namespace Magetop\Agency\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\Message\ManagerInterface;

class CheckoutCartSaveBeforeObserver implements ObserverInterface
{
    /**
     * @var \Magetop\Agency\Helper\Data
     */
    private $_helper;
    /**
     * @var ManagerInterface
     */
    private $_messageManager;
    /**
     * @var CheckoutSession
     */
    protected $_checkoutSession;

    protected $request;
    /**
     * @var ProductRepositoryInterface
     */
    protected $_productRepository;

    protected $_mpAssignQuote;

    /**
     * Initialize dependencies.
     *
     * @param \Magetop\Agency\Helper\Data $helper
     */
    public function __construct(
        CheckoutSession $checkoutSession,
        \Magetop\Agency\Helper\Data $helper,
        \Magetop\Agency\Model\QuoteFactory $mpAssignQuote,
        \Magento\Framework\App\Request\Http $request,
        ManagerInterface $messageManager,
        ProductRepositoryInterface $productRepository
    ) {
        $this->_checkoutSession = $checkoutSession;
        $this->_helper = $helper;
        $this->request = $request;
        $this->_productRepository = $productRepository;
        $this->_mpAssignQuote = $mpAssignQuote;
        $this->_messageManager = $messageManager;        
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
       /* if ($this->_marketplaceHelperData->getAllowProductLimit()) {
            $data = $this->request->getParams();
            $items =  $this->_checkoutSession->getQuote()->getAllVisibleItems();
            $quoteId = $this->_checkoutSession->getQuoteId();
            foreach ($items as $item) {
                $mpProductCartLimit=0;
                $assign_product = 0;
                $product = $this->_productRepository->getById($item->getProductId());
                $productTypeId = $product['type_id'];
                if ($productTypeId != 'downloadable' && $productTypeId != 'virtual') {
                    $collection = $this->_mpAssignQuote->create()->getCollection()
                                ->addFieldToFilter('item_id',$item->getId())
                                ->addFieldToFilter('quote_id',$quoteId);
                    if ($collection->getSize()) {
                        foreach ($collection as $key) {
                            $assign_product = $key->getAssignId();
                        }
                    } 
                    
                    if (!(array_key_exists("assignproduct_id",$data) || $assign_product)) {
                        $mpProductCartLimit = $product['mp_product_cart_limit'];
                    }
                    if (!$mpProductCartLimit) {
                        $mpProductCartLimit = $this->_marketplaceHelperData->getGlobalProductLimitQty();
                    }
                    if ($item->getQty() > $mpProductCartLimit) {
                        $item->setQty($mpProductCartLimit);
                        $productName = "<b>".$item->getName()."</b>";
                        $this->_messageManager->add(
                            __(
                                'Sorry, but you can only add maximum %1 quantity of %2 in this cart.',
                                $mpProductCartLimit,
                                $productName
                            )
                        );
                        $this->_checkoutSession->getQuote()->setHas(true);
                    }
                }
            }
        }
        return $this; */
    }
}