<?php

use common\helpers\GlobalParam;
use domain\Producer\Producer\Producer;
use domain\Producer\Producer\ProducerModule;
use domain\User\User\UserModule;
use common\helpers\Environment;
use yii\bootstrap5\Nav;
use yii\helpers\Html;

$userModule = UserModule::getInstance();
$producerModule = ProducerModule::getInstance();
$userCurrent = GlobalParam::getCurrentUser();

$isUserCurrentGrantedAsProducer = $userModule->getAuthorizationChecker()->isGrantedAsProducer($userCurrent);
$producer = null;
if ($isUserCurrentGrantedAsProducer && $userCurrent->id_producer) {
    $producer = $producerModule->getRepository()->findOneProducerById($userCurrent->id_producer);
}

?>
<div class="container container-nav-user-top">
    <?php if($context == 'producer'): ?>
        <?= Environment::badgeBeta(); ?>
    <?php endif; ?>
    <div class="nav-user-top">
        <nav class="navbar navbar-expand-lg">
            <div class="container-fluid">
                    <?php
                    $producersArray = Producer::find()
                        ->joinWith(['userProducer user_producer'])
                        ->where([
                            'user_producer.id_user' => GlobalParam::getCurrentUserId(),
                            'user_producer.bookmark' => 1,
                        ])
                        ->all();

                    $itemsProducersArray = [];
                    if (count($producersArray)) {
                        /*$itemsProducersArray[] = [
                            'label' => 'Mes favoris',
                        ];*/
                        foreach ($producersArray as $producerItem) {
                            $itemsProducersArray[] = [
                                'label' => Html::encode($producerItem->name),
                                'url' => $this->getUrlManagerProducer()->createAbsoluteUrl(['site/index', 'slug_producer' => $producerItem->slug]),
                            ];
                        }
                    } else {
                        $itemsProducersArray[] = [
                            'label' => 'Aucun producteur dans votre liste.',
                        ];
                    }

                    $itemsProducersArray[] = '<hr class="dropdown-divider">';

                    $itemsProducersArray[] = [
                        'label' => '<i class="bi bi-search"></i> Rechercher un producteur',
                        'url' => $this->getUrlManagerFrontend()->createAbsoluteUrl(['site/index'])
                    ];

                    // Items du menu
                    $itemHome = [
                        'label' => '<i class="bi bi-house-door"></i> <span class="link-text">Accueil</span>',
                        'url' => $this->getUrlManagerFrontend()->createAbsoluteUrl(['site/index']),
                        'linkOptions' => ['class' => '']
                    ];
                    $itemAdministration = [
                        'label' => '<i class="bi bi-gear"></i> <span class="link-text">Administration</span>',
                        'url' => $this->getUrlManagerBackend()->createAbsoluteUrl(['dashboard/index']),
                        'visible' => $isUserCurrentGrantedAsProducer,
                        'linkOptions' => ['class' => '']
                    ];
                    $itemProducerSpace = [
                        'label' => '<i class="bi bi-shop"></i> <span class="link-text">Ma boutique</span>',
                        'url' => $this->getUrlManagerProducer()->createAbsoluteUrl(['site/index', 'slug_producer' => $producer ? $producer->slug : '']),
                        'visible' => $isUserCurrentGrantedAsProducer,
                        'linkOptions' => ['class' => '']
                    ];
                    $itemProducers = [
                        'label' => '<i class="bi bi-bookmark-heart"></i> <span class="link-text">Producteurs</span>',
                        'url' => '#',
                        'items' => $itemsProducersArray,
                        'options' => ['class' => 'nav-item-producers'],
                        'linkOptions' => ['class' => ''],
                        'visible' => !Yii::$app->user->isGuest
                    ];

                    $itemsUserArray = [];
                    if ($context == 'producer') {
                        $itemsUserArray = [
                            /*[
                                'label' => '<span class="glyphicon glyphicon-home"></span> Retour à l\'accueil',
                                'url' => $this->getUrlManagerFrontend()->createAbsoluteUrl(['site/index']),
                            ]*/
                        ];
                    }

                    $itemsUserArray[] = [
                        'label' => '<i class="bi bi-person"></i> Mon profil',
                        'url' => $this->getUrlManagerFrontend()->createAbsoluteUrl(['user/update']),
                    ];
                    $itemsUserArray[] = [
                        'label' => '<i class="bi bi-box-arrow-left"></i> Déconnexion',
                        'url' => $this->getUrlManagerFrontend()->createAbsoluteUrl(['site/logout']),
                    ];

                    $itemUser = [
                        'label' => '<i class="bi bi-person"></i> <span class="link-text">' .
                            ((!Yii::$app->user->isGuest) ? Html::encode(Yii::$app->user->identity->name . ' ' . Yii::$app->user->identity->lastname) : '') .
                            '</span>',
                        'options' => ['id' => 'label1', 'class' => 'nav-item-user'],
                        'url' => '#',
                        'linkOptions' => ['class' => ''],
                        'items' => $itemsUserArray,
                        'visible' => !Yii::$app->user->isGuest
                    ];
                    $itemConnexion = [
                        'label' => '<i class="bi bi-box-arrow-in-right"></i> Connexion',
                        'url' => ($context == 'producer')
                            ? $this->getUrlManagerFrontend()->createAbsoluteUrl([
                                'site/producer',
                                'id' => $this->context->getProducerCurrent()->id,
                                'return_url' => \Yii::$app->urlManagerProducer->createAbsoluteUrl(['site/index', 'slug_producer' => $this->context->getProducerCurrent()->slug])
                            ])
                            : $this->getUrlManagerFrontend()->createAbsoluteUrl(['site/login']),
                        'linkOptions' => ['class' => ''],
                        'visible' => \Yii::$app->user->isGuest,
                        'active' => $this->getControllerAction() == 'site/login'
                    ];

                    $itemSignup = [
                        'label' => '<i class="bi bi-person-plus"></i> Inscription',
                        'url' => ($context == 'producer')
                            ? $this->getUrlManagerFrontend()->createAbsoluteUrl([
                                'site/producer',
                                'id' => $this->context->getProducerCurrent()->id,
                                'return_url' => \Yii::$app->urlManagerProducer->createAbsoluteUrl(['site/index', 'slug_producer' => $this->context->getProducerCurrent()->slug])
                            ])
                            : $this->getUrlManagerFrontend()->createAbsoluteUrl(['site/signup']),
                        'linkOptions' => ['class' => ''],
                        'visible' => \Yii::$app->user->isGuest,
                        'active' => $this->getControllerAction() == 'site/signup'
                    ];

                    if ($context == 'frontend') {
                        $itemsMenu = [
                            $itemAdministration,
                            $itemProducerSpace,
                            $itemProducers,
                            $itemUser,
                            $itemConnexion,
                            $itemSignup
                        ];
                    } elseif ($context == 'producer') {
                        $itemsMenu = [
                            $itemHome,
                            $itemAdministration,
                            $itemProducers,
                            $itemUser,
                            $itemConnexion,
                            $itemSignup
                        ];
                    }

                    echo Nav::widget([
                        'encodeLabels' => false,
                        'options' => ['class' => 'nav nav-pills navbar-nav navbar-right'],
                        'items' => $itemsMenu
                    ]);
                    ?>
            </div>
        </nav>
    </div>
</div>