<?php
/**
 * Webkul Software.
 *
 * @category  Webkul
 * @package   Webkul_Agorae
 * @author    Webkul
 * @copyright Copyright (c) Webkul Software Private Limited (https://webkul.com)
 * @license   https://store.webkul.com/license.html
 */
namespace Webkul\Agorae\Block\Html;

/**
 * Adminhtml Agorae Sliderimages Edit Form
 */
class Topmenu extends \Magento\Theme\Block\Html\Topmenu
{
    /**
     * Recursively generates top menu html from data that is specified in $menuTree
     *
     * @param \Magento\Framework\Data\Tree\Node $menuTree
     * @param string $childrenWrapClass
     * @param int $limit
     * @param array $colBrakes
     * @return string
     *
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
     * @SuppressWarnings(PHPMD.NPathComplexity)
     */
    protected function _getHtml(
        \Magento\Framework\Data\Tree\Node $menuTree,
        $childrenWrapClass,
        $limit,
        array $colBrakes = []
    ) {
        $html = '';
        $objectManager  = \Magento\Framework\App\ObjectManager::getInstance();
        $agoraeHelper   = $objectManager->create(\Webkul\Agorae\Helper\Data::class);
        $children       = $menuTree->getChildren();
        $parentLevel    = $menuTree->getLevel();
        $childLevel     = $parentLevel === null ? 0 : $parentLevel + 1;

        $counter        = 1;
        $divCounter     = 1;
        $itemPosition   = 1;
        $childrenCount  = $children->count();

        $parentPositionClass = $menuTree->getPositionClass();
        $itemPositionClassPrefix = $parentPositionClass ? $parentPositionClass . '-' : 'nav-';

        foreach ($children as $child) {
            $categoryNode = explode("-", $child->getId());
            $categoryId = $categoryNode[2];
            $categoryImage = $agoraeHelper->getCategoryImage($categoryId);
            
            $child->setLevel($childLevel);
            $child->setIsFirst($counter == 1);
            $child->setIsLast($counter == $childrenCount);
            $child->setPositionClass($itemPositionClassPrefix . $counter);

            $outermostClassCode = '';
            $outermostClass = $menuTree->getOutermostClass();

            if ($childLevel == 0 && $outermostClass) {
                $outermostClassCode = ' class="' . $outermostClass . '" ';
                $child->setClass($outermostClass);
            }

            if (is_array($colBrakes) && count($colBrakes) && $colBrakes[$counter]['colbrake']) {
                $html .= '</ul></li><li class="column"><ul>';
            }

            $html .= '<li ' . $this->_getRenderedMenuItemAttributes($child) . '>';
            if ($childLevel == 1 && $categoryImage) {
                $html .= '<img src="'.$categoryImage.'" />';
            }
            $html .= '<a href="' . $child->getUrl() . '" ' . $outermostClassCode . '><span>' . $this->escapeHtml(
                $child->getName()
            ) . '</span></a>' . $this->_addSubMenu(
                $child,
                $childLevel,
                $childrenWrapClass,
                $limit
            ) . '</li>';
            $itemPosition++;
            $counter++;
        }

        if (is_array($colBrakes) && count($colBrakes) && $limit) {
            $html = '<li class="column"><ul>' . $html . '</ul></li>';
        }

        return $html;
    }

    /**
     * Add sub menu HTML code for current menu item
     *
     * @param \Magento\Framework\Data\Tree\Node $child
     * @param string $childLevel
     * @param string $childrenWrapClass
     * @param int $limit
     * @return string HTML code
     */
    protected function _addSubMenu($child, $childLevel, $childrenWrapClass, $limit)
    {
        $html = '';
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $agoraeHelper = $objectManager->create(\Webkul\Agorae\Helper\Data::class);

        if (!$child->hasChildren()) {
            return $html;
        }
        $colStops = [];
        if ($childLevel == 0 && $limit) {
            $colStops = $this->_columnBrake($child->getChildren(), $limit);
        }
        $categoryNode = explode("-", $child->getId());
        $categoryId = $categoryNode[2];
        $categoryImage = $agoraeHelper->getCategoryImage($categoryId);

        $html .= '<ul class="level' . $childLevel . ' submenu">';
        if ($childLevel == 0 && $categoryImage) {
            $html .= '<img class="img-cat-0" src="'.$categoryImage.'"/>';
        }
        $html .= $this->_getHtml($child, $childrenWrapClass, $limit, $colStops);
        $html .= '</ul>';
        return $html;
    }
}
