<?php

namespace Solwin\Verticalmenu\Block;

use Solwin\Verticalmenu\Model\MenuFactory;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\View\Element\Template;

class Verticalmenu extends Template {

    /**
     * @var \Solwin\Verticalmenu\Model\ResourceModel\Menu\Collection
     */
    protected $_collection;

    /**
     * @var \Solwin\Verticalmenu\Model\ResourceModel\Menu\Collection
     */
    protected $_childcollection;

    /**
     * @var \Solwin\Verticalmenu\Model\MenuFactory
     */
    protected $_modelMenuFactory;

    /**
     * @param  \Solwin\Verticalmenu\Model\MenuFactory $modelMenuFactory
     * @param  \Solwin\Verticalmenu\Model\ResourceModel\Menu\Collection $collection         
     * @param  \Magento\Catalog\Block\Product\Context $context 
     * @param   array $data
     */
    public function __construct(
        \Magento\Catalog\Block\Product\Context $context,
        MenuFactory $modelMenuFactory,
        \Solwin\Verticalmenu\Model\ResourceModel\Menu\Collection $collection,
        \Solwin\Verticalmenu\Model\ResourceModel\Menu\Collection $childcollection,
        array $data = []
    ) {
        $this->_modelMenuFactory = $modelMenuFactory;
        $this->_collection = $collection;
        $this->_childcollection = $childcollection;
        parent::__construct($context, $data);
    }

    public function getMenuCollection() {
        $collection = $this->_collection->addFieldToFilter('status', 1)->setOrder('sort_order', 'ASC');
        return $collection;
    }
    
    public function getChildren($parentId) {
        $this->_childcollection->clear()->getSelect()->reset('where');
        $childCollection = $this->_childcollection->addFieldToFilter('status', 1)->addFieldToFilter('parent_id', $parentId)->setOrder('sort_order', 'ASC');
        $html = "";
        $html .= '<input class="count_li" type="hidden" value="' . count($childCollection) . '"/>';
        foreach ($childCollection as $child) {
            if ($child->getChildrenCount() > 0) {
                $html .= '<li class="vparent" >';
            } else {
                $html .= '<li>';
            }
            $html .= '<a href='.$child->getUrl().'>' . $child->getMenuTitle() . '</a>';
            if ($child->getChildrenCount() > 0) {
                $html .= '<ul class="vsubmenu vslevel-' . $child->getLevel() . '">' . $this->getChildren($child->getMenuId()) . '</ul>';
            }
            $html .= '</li>';
        }

        return $html;
    }

    /* get Extension enable/disable */

    public function getEnableModule() {
        return $this->_scopeConfig->getValue('verticalmenu_settings/generalsettings/active', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
    }

    /**
     * get base url with store code
     */
    public function getBaseUrlWithStoreCode() {
        return $this->_storeManager->getStore()->getBaseUrl();
    }

    /**
     * get base url without store code
     */
    public function getBaseUrl() {
        return $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB);
    }

    public function getMediaPath() {
        return $this->_filesystem->getDirectoryRead(
                        DirectoryList::MEDIA
                )->getAbsolutePath('');
    }

}
