<?php
declare(strict_types=1);

namespace FiloBlu\ExtBlackbirdMenuManager\Plugin\Block\NodeType;

use Blackbird\MenuManager\Api\NodeRepositoryInterface;
use Closure;
use function in_array;
use Magento\Customer\Model\Context;

use Magento\Customer\Model\Session;

/**
 * Class AbstractNodeTypeFront
 * @package FiloBlu\ExtBlackbirdMenuManager\Plugin\Block\NodeType
 */
class AbstractNodeTypeFront
{

    /**
     * @var Session
     */
    protected $customerSession;
    /**
     * @var \Magento\Framework\App\Http\Context
     */
    protected $context;
    /**
     * @var NodeRepositoryInterface
     */
    protected $nodeRepository;

    /**
     * AbstractNodeTypeFront constructor.
     * @param Session $customerSession
     * @param \Magento\Framework\App\Http\Context $context
     * @param NodeRepositoryInterface $nodeRepository
     */
    public function __construct(
        Session $customerSession,
        \Magento\Framework\App\Http\Context $context,
        NodeRepositoryInterface $nodeRepository
    ) {
        $this->customerSession = $customerSession;
        $this->context = $context;
        $this->nodeRepository = $nodeRepository;
    }

    /**
     * @param \Blackbird\MenuManager\Block\NodeType\AbstractNodeTypeFront $subject
     * @param Closure $proceed
     * @param $urlNodeItem
     * @param $node
     * @param $childrenArray
     * @return bool|mixed
     */
    public function aroundIsActive(\Blackbird\MenuManager\Block\NodeType\AbstractNodeTypeFront $subject, Closure $proceed, $urlNodeItem, $node, $childrenArray)
    {
        $originalResult = $proceed($urlNodeItem, $node, $childrenArray);

        if ($originalResult && (!$urlNodeItem || $urlNodeItem === '/')) {
            return false;
        }

        return $originalResult;
    }

    /**
     * @see \Blackbird\MenuManager\Block\NodeType\AbstractNodeTypeFront::prepareTemplate
     * @param \Blackbird\MenuManager\Block\NodeType\AbstractNodeTypeFront $subject
     * @param Closure $proceed
     * @param $type
     * @param $menuIdentifier
     * @return \Blackbird\MenuManager\Block\NodeType\AbstractNodeTypeFront
     */
    public function aroundPrepareTemplate(\Blackbird\MenuManager\Block\NodeType\AbstractNodeTypeFront $subject, Closure $proceed, $type, $menuIdentifier)
    {
        $proceed($type, $menuIdentifier);

        $subject->setTemplate('Blackbird_MenuManager::menu/view/' . $menuIdentifier . '/nodetype/' . $type . '.phtml');

        if (!$subject->getTemplateFile()) {
            //default case
            $subject->setTemplate('FiloBlu_ExtBlackbirdMenuManager::menu/view/default/nodetype/' . $type . '.phtml');

            if (!$subject->getTemplateFile()) {
                $subject->setTemplate('Blackbird_MenuManager::menu/view/default/nodetype/' . $type . '.phtml');
            }
        }

        return $subject;
    }

    /**
     * @param \Blackbird\MenuManager\Block\NodeType\AbstractNodeTypeFront $subject
     * @param $nodeId
     * @return mixed
     */
    public function beforeGetHtml(\Blackbird\MenuManager\Block\NodeType\AbstractNodeTypeFront $subject, $nodeId)
    {
        $node = $this->nodeRepository->getById($nodeId);
        $subject->setCustomerGroup($node->getData('customer_group'));
        return null;
    }

    /**
     * @param \Blackbird\MenuManager\Block\NodeType\AbstractNodeTypeFront $subject
     * @param $result
     * @return string
     */
    public function afterToHtml(\Blackbird\MenuManager\Block\NodeType\AbstractNodeTypeFront $subject, $result)
    {
        $arrayCustomerGroup = $subject->getCustomerGroup();
        if (!isset($arrayCustomerGroup)) {
            return $result;
        }

        $arrayCustomerGroup = explode(',', $arrayCustomerGroup);
        $currentCustomerGroupId = '-1';

        if ($this->context->getValue(Context::CONTEXT_AUTH)) {
            $currentCustomerGroupId = '-2,';
            $currentCustomerGroupId .= $this->customerSession->getCustomer()->getGroupId();
        }

        $_currentCustomerGroupIdArray = explode(',', $currentCustomerGroupId);
        $found = false;

        foreach ($_currentCustomerGroupIdArray as $groupIdVal) {
            if (in_array($groupIdVal, $arrayCustomerGroup)) {
                $found = true;
            }
        }

        if ($found) {
            return $result;
        }

        return '';
    }
}
