<?php
namespace Leadlion\SolicitaMontaj\Plugin;

use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Quote\Api\CartItemRepositoryInterface as QuoteItemRepository;
use \Magento\Framework\Exception\NoSuchEntityException;
class MontajQuoteToOrderItem
{
    /**
     * @var CheckoutSession
     */
    private $checkoutSession;
    /**
     * @var QuoteItemRepository
     */
    private $quoteItemRepository;
    private \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository;
    private \Magento\Framework\Session\SessionManager $session;
    private \Magento\Store\Model\StoreManagerInterface $_storeManager;
    private \Magento\Catalog\Api\ProductRepositoryInterface $_productRepository;
    private \Leadlion\Autogedal\Helper\Config $helper;

    public function __construct(
        \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
        \Magento\Framework\Session\SessionManager $session,
        CheckoutSession $checkoutSession,
        QuoteItemRepository $quoteItemRepository,
        \Leadlion\Autogedal\Helper\Config $helper,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository

    ){
        $this->categoryRepository = $categoryRepository;
        $this->session = $session;
        $this->checkoutSession = $checkoutSession;
        $this->quoteItemRepository = $quoteItemRepository;
        $this->helper = $helper;
        $this->_storeManager = $storeManager;
        $this->_productRepository = $productRepository;
        
    }
    public function afterGetConfig(\Magento\Checkout\Model\DefaultConfigProvider $subject, $result)
    {
        $quoteId = $this->checkoutSession->getQuote()->getId();
        $montajData = $this->session->getMontaj();
        $visible = false;

        if ($quoteId) {
            $itemOptionCount = count($result['quoteItemData']);
            
            $categories = $this->helper->getConfig('leadlion/solicita_montaj/category_montaj', $this->_storeManager->getStore()->getId());
            $categories = explode(",", $categories);
            $cartData = $this->checkoutSession->getQuote()->getAllVisibleItems();

            if($cartData):
                foreach($cartData as $item):
                    $product = $this->getProductById($item->getProductId());
                    if($product):
                        $productCategory = $product->getCategoryIds();
                        foreach($productCategory as $category):
                            $categoryData = $this->getCategory($category);
                            if($categoryData):
                                if($categoryData->getIsActive()):
                                    if(in_array($categoryData->getName() , $categories)):
                                        $visible = true;
                                    endif; 
                                endif;
                            endif;
                        endforeach;
                    endif;
                endforeach;
            endif;
            
            if($montajData && $visible){
                $montajData = true;
            }else{
                $montajData = false;
            }
        
            for ($i=0; $i < $itemOptionCount; $i++) {
                $quoteParentId = $result['totalsData']['items'][$i]['item_id'];
                $currentOption = [];
                $isNew = null;
                $result['quoteItemData'][$i]['new'] = 'newvv';
                $productId = $result['quoteItemData'][$i]['product']['entity_id'];

                $result['quoteItemData'][$i]['montaj'] = $montajData;
                json_encode($currentOption);
            }
        }
        return $result;
    }
    public function getProductById($id)
    {        
        try {
            return $this->_productRepository->getById($id);
        } catch (NoSuchEntityException $ex) {
            return null;
        }
    }
    public function getCategory($category)
    {
        try {
            return $this->categoryRepository->get($category);
        } catch (NoSuchEntityException $ex) {
            return null;
        }
    }
}