<?php
namespace MGS\Ajaxlayernavigation\Model\Layer\Filter; 

class Category extends \MGS\Ajaxlayernavigation\Model\Layer\Filter\DefaultFilter
{
    protected $appliyedFilter;
    
    protected $filterPlus;

    private $category;

    /**
     * @var int
     */
    private $categoryId;

    /**
     * @var bool
     */
    private $isApplied = false;


    public function __construct(
        \MGS\Ajaxlayernavigation\Model\Layer\Filter\ItemFactory $filterItemFactory,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Catalog\Model\Layer $layer,
        \MGS\Ajaxlayernavigation\Model\Layer\Filter\ItemBuilder $itemDataBuilder,
        \Magento\Framework\Escaper $escaper,
        \Magento\Catalog\Model\Layer\Filter\DataProvider\CategoryFactory $categoryDataProviderFactory,
        array $data = []
    ) {
        parent::__construct(
            $filterItemFactory,
            $storeManager,
            $layer,
            $itemDataBuilder,
            $data
        );
        $this->escaper = $escaper;
        $this->_requestVar = 'cat';
        $this->appliedFilter = [];
        $this->filterPlus = false;
        $this->dataProvider = $categoryDataProviderFactory->create(['layer' => $this->getLayer()]);
    }


    public function apply(\Magento\Framework\App\RequestInterface $request)
    {
        $categoryId = (int)$request->getParam($this->_requestVar);
        if (!$categoryId) {
            return $this;
        }
        $this->dataProvider->setCategoryId($categoryId);

        if ($this->dataProvider->isValid()) {
            $category = $this->dataProvider->getCategory();
            $this->getLayer()->getProductCollection()->addCategoryFilter($category);
            $this->getLayer()->getState()->addFilter($this->_createItem($category->getName(), $categoryId));
        }
        
        return $this;
    }

    protected function _getItemsData()
    {
        /** @var \Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection $productCollection */
        $productCollection = $this->getLayer()->getProductCollection();
        $optionsFacetedData = $productCollection->getFacetedData('category');
        $category = $this->dataProvider->getCategory();
        $categories = $category->getChildrenCategories();  
        $collectionSize = $productCollection->getSize();
        $activeFilters = [];
        if($this->appliedFilter) {
            $activeFilters = explode(',', $this->appliedFilter);
        }  
        $currentProductIds = $productCollection->getAllIds();
        if ($category->getIsActive()) {
            foreach ($categories as $category) {  
                if ($category->getIsActive()
                    && isset($optionsFacetedData[$category->getId()]) 
                ) { 
                    $active = in_array($category->getId(), $activeFilters);
                    $this->_itemBuilder->addItemData(
                        $this->escaper->escapeHtml($category->getName()),
                        $category->getId(),
                        $optionsFacetedData[$category->getId()]['count'],
                        $active,
                        $this->filterPlus
                    );
                }
            }
        } 
        return $this->_itemBuilder->build();
    }

    public function getName()
    {
        return __('Category');
    }
}
