<?php
/**
 * Webkul Software.
 *
 * @category  Webkul
 * @package   Webkul_AdvanceSeoManager
 * @author    Webkul
 * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */

namespace Webkul\AdvanceSeoManager\Block;

use Magento\Framework\App\ActionInterface;

class ItemsCollection extends \Magento\Framework\View\Element\Template
{
    /**
     *
     * @param \Magento\Backend\Block\Template\Context $context
     * @param \Webkul\AdvanceSeoManager\Helper\Data $helper
     * @param \Magento\Cms\Model\PageFactory $pageFactory
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollection
     * @param \Magento\Catalog\Helper\Category $categoryHelper
     * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollection
     * @param array $data
     */
    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Webkul\AdvanceSeoManager\Helper\Data $helper,
        \Magento\Cms\Model\PageFactory $pageFactory,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Catalog\Model\ResourceModel\Category\CollectionFactory $categoryCollection,
        \Magento\Catalog\Helper\Category $categoryHelper,
        \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollection,
        array $data = []
    ) {
        $this->scopeConfig = $context->getScopeConfig();
        $this->helper = $helper;
        $this->pageFactory = $pageFactory;
        $this->_storeManager = $storeManager;
        $this->_categoryCollection = $categoryCollection;
        $this->categoryHelper = $categoryHelper;
        $this->productCollection = $productCollection;
        parent::__construct($context, $data);
    }

    /**
     * @return \Webkul\AdvanceSeoManager\Helper\Data
     */
    public function getHelper()
    {
        return $this->helper;
    }

    /**
     * get Store Id
     *
     * @return int
     */
    public function getStoreId()
    {
        return $this->_storeManager->getStore()->getId();
    }

    /**
     * get collection all active cms pages
     *
     * @return collection
     */
    public function getCmsPages()
    {
        $storeId = $this->getStoreId();
        $collection = $this->pageFactory->create()->getCollection();
        $collection->addFieldToSelect("*");
        $collection->addStoreFilter($storeId);
        return $collection;
    }

    /**
     * get collection of active categories
     *
     * @return collection
     */
    public function getCategoryCollection()
    {
        $rootCategoryId = $this->_storeManager->getStore($this->getStoreId())
        ->getRootCategoryId();
        
        $collection = $this->_categoryCollection->create();
        $collection->addAttributeToSelect('*');
        $collection->addAttributeToFilter('is_active', '1');
        $collection->addFieldToFilter('entity_id', ['in' => $rootCategoryId]);
    
        return $collection;
    }

    /**
     * get all sub categories of a root category
     *
     * @param [type] $category
     * @return html
     */
    public function getAllCategories($category)
    {
        $categoryHtmlEnd = null;
        if ($childrenCategories = $category->getChildrenCategories()) {
            foreach ($childrenCategories as $category) {
                if (!$category->getIsActive()) {
                    continue;
                }
                
                $categoryUrl = $this->categoryHelper->getCategoryUrl($category);
                $categoryHtml = '<li><a href="' . $categoryUrl . '">' . $category->getName() . '</a></li>';
                $categoryReturn = $this->getAllCategories($category);
                $categoryHtml = $categoryHtml . $categoryReturn;
                
                $categoryHtmlEnd = $categoryHtmlEnd . $categoryHtml;
            }
            return '<ul>' . $categoryHtmlEnd . '</ul>';
        }
        return '';
    }

    /**
     * get collection of all enabled products
     * with visibililty both (catalog,search)
     * @return collection
     */
    public function getProductCollection()
    {
        $page = ($this->getRequest()->getParam('p')) ? $this->getRequest()->getParam('p') : 1;
        $pageSize = $this->getRequest()->getParam('limit') ?$this->getRequest()->getParam('limit'):50;
        $collection = $this->productCollection->create();
        $collection->addAttributeToSelect('*');
        $collection->addAttributeToFilter('visibility', \Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
        $rulerStatus = \Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED;
        $collection->addAttributeToFilter('status', $rulerStatus);
        $collection->addWebsiteFilter();
        $collection->addUrlRewrite();
        $collection->setPageSize($pageSize);
        $collection->setCurPage($page);
        return $collection;
    }

    protected function _prepareLayout()
    {
        parent::_prepareLayout();
        if ($this->getProductCollection()) {
            
            $blockName = 'custom.pager';
            $pager = null;
            if ($this->getLayout()->getBlock($blockName)) {
                $pager = $this->getLayout()->getBlock($blockName)
                    ->setAvailableLimit([50=>50, 100=>100,200=>200])
                    ->setShowPerPage(true)->setCollection(
                        $this->getProductCollection()
                    );
            } else {
                $pager = $this->getLayout()->createBlock(
                    \Magento\Theme\Block\Html\Pager::class,
                    $blockName
                )->setAvailableLimit([50=>50, 100=>100, 200=>200])
                ->setShowPerPage(true)->setCollection(
                    $this->getProductCollection()
                );
            }
            $this->setChild('pager', $pager);
            $this->getProductCollection()->load();
        }
    }

    public function getPagerHtml()
    {
        return  $this->getChildHtml('pager');
    }
}
