<?php
/**
 * Magetop Store
 *
 * @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\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Message\ManagerInterface;

class CatalogProductTypePrepareFullOptions implements ObserverInterface
{
    /**
     * @var ManagerInterface
     */
    private $messageManager;

    /**
     * @var \Magento\Store\Model\StoreManagerInterface
     */
    protected $storeManager;
  
    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $_customerSession;

    /**
     * @var \Magento\Framework\Json\Helper\Data
     */
    protected $jsonHelper;

    /**
     * @param ManagerInterface                           $messageManager
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\Customer\Model\Session            $customerSession
     * @param \Magetop\Quotesystem\Model\QuotesFactory    $quotes
     * @param \Magento\Framework\Json\Helper\Data        $jsonHelper
     */
    public function __construct(
        ManagerInterface $messageManager,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Customer\Model\Session $customerSession,
        \Magetop\Quotesystem\Model\QuotesFactory $quotes,
        \Magento\Framework\Json\Helper\Data $jsonHelper
    ) {
        $this->messageManager = $messageManager;
        $this->storeManager = $storeManager;
        $this->_customerSession = $customerSession;
        $this->_quoteFactory = $quotes;
        $this->jsonHelper = $jsonHelper;
    }
    
    /**
     * Save action
     *
     * @param \Magento\Framework\Event\Observer $observer
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $quoteId = $this->_customerSession->getQuoteId();
        if ($quoteId) {
            try {
                $observerRequestData = $observer['buy_request'];
                $options = $observerRequestData->getOptions();
                if ($options) {
                    $data = [];
                    foreach ($options as $key => $value) {
                        $data[$key] = $value;
                    }
                    $optionData = $this->jsonHelper->jsonEncode($data);
                    $quoteCollection = $this->_quoteFactory->create()->load($quoteId);
                    if ($quoteCollection->getData()) {
                        try {
                            $quoteCollection->setProductOption($optionData)->save();
                        } catch (\Exception $e) {
                            $this->messageManager->addError($e->getMessage());
                        }
                    }
                }
            } catch (\Exception $e) {
                $this->messageManager->addError($e->getMessage());
            }
        }
    }
}
