<?php

namespace Leadlion\SolicitaMontaj\Block;

use \Magento\Framework\Exception\NoSuchEntityException;

class Montaj extends \Magento\Framework\View\Element\Template
{
    /**
    * @var \Magento\Catalog\Helper\Category
    */
    protected $categoryRepository;
    
    public function __construct(
    \Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
    \Leadlion\Autogedal\Helper\Config $helper,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    \Magento\Framework\View\Element\Template\Context $context,
    \Magento\Catalog\Block\Product\View $product,
    \Magento\Catalog\Block\Category\View $category,
    \Magento\Framework\Session\SessionManager $session,
    \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
    \Magento\Checkout\Model\Session $_checkoutSession
    ) {
        $this->categoryRepository = $categoryRepository;
        $this->helper = $helper;
        $this->_storeManager = $storeManager;
        $this->_product = $product;
        $this->_category = $category;
        $this->session = $session;
        $this->_productRepository = $productRepository;
        $this->_checkoutSession = $_checkoutSession;
        parent::__construct($context);
    }
    
    public function isMotajVisibile()
    {
        $categories = $this->helper->getConfig('leadlion/solicita_montaj/category_montaj', $this->_storeManager->getStore()->getId());
        $categories = explode(",", $categories);
        $visible = false;
        
        foreach($this->getCategoryData() as $name):
            if(in_array($name , $categories)):
                $visible = true;
            endif;  
        endforeach;
        
        return $visible;
    }
    public function isMotajVisibileOsc()
    {
        $visible = false;
        $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;
        
        return $visible;
    }
    public function getProductById($id)
    {   
        try {
            return $this->_productRepository->getById($id);
        } catch (NoSuchEntityException $ex) {
            return null;
        }
    }
    
    public function getCategoryData(){
        
        $categoryName = [];
        $product = $this->_product->getProduct();
        
        $categories = $product->getCategoryIds();
        foreach($categories as $category):
            $categoryData = $this->getCategory($category);
            if($categoryData->getIsActive()):
                array_push($categoryName , $categoryData->getName());
            endif;
        endforeach;
            
        return $categoryName;
    }
    public function isChecked()
    {
        return $this->session->getMontaj();
    }
    
    
    public function getCategory($category)
    {
        try {
            return $this->categoryRepository->get($category);
        } catch (NoSuchEntityException $ex) {
            $this->messageManager->addErrorMessage(__('Requested %1 category doesn\'t exist', $category));
            return null;
        }
    }
}

