<?php

use common\helpers\GlobalParam;
use common\logic\Producer\Producer\Model\Producer;
use common\logic\User\User\Module\UserModule;
use yii\bootstrap\Nav;
use yii\helpers\Html;

$userModule = UserModule::getInstance();
$userCurrent = GlobalParam::getCurrentUser();
$isUserCurrentGrantedAsProducer = $userModule->getAuthorizationChecker()->isGrantedAsProducer($userCurrent);
$producer = null;
if ($isUserCurrentGrantedAsProducer) {
    $producer = Producer::searchOne(['id' => \Yii::$app->user->identity->id_producer]);
}

?>

<div class="nav-user-top">
    <nav class="navbar navbar-default">
        <div class="container">

            <?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 vos favoris.',
                ];
            }

            $itemsProducersArray[] = [
                'options' => ['class' => 'divider'],
                'label' => ''
            ];
            $itemsProducersArray[] = [
                'label' => '<span class="glyphicon glyphicon-search"></span> Rechercher un producteur',
                'url' => $this->getUrlManagerFrontend()->createAbsoluteUrl(['site/producers'])
            ];

            // Items du menu
            $itemAdministration = [
                'label' => '<span class="glyphicon glyphicon-cog"></span> <span class="link-text">Administration</span>',
                'url' => $this->getUrlManagerBackend()->createAbsoluteUrl(['dashboard/index']),
                'visible' => $isUserCurrentGrantedAsProducer,
                'linkOptions' => ['class' => 'btn btn-default navbar-btn']
            ];
            $itemProducerSpace = [
                'label' => '<span class="glyphicon glyphicon-eye-open"></span> <span class="link-text">Mon espace producteur</span>',
                'url' => $this->getUrlManagerProducer()->createAbsoluteUrl(['site/index', 'slug_producer' => $producer ? $producer->slug : '']),
                'visible' => $isUserCurrentGrantedAsProducer,
                'linkOptions' => ['class' => 'btn btn-default navbar-btn']
            ];
            $itemProducers = [
                'label' => '<span class="glyphicon glyphicon-star"></span> <span class="link-text">Mes producteurs</span>',
                'url' => '#',
                'items' => $itemsProducersArray,
                'linkOptions' => ['class' => 'btn btn-default navbar-btn'],
                'visible' => !Yii::$app->user->isGuest
            ];

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

            $itemsUserArray[] = [
                'label' => '<span class="glyphicon glyphicon-user"></span> Mon profil',
                'url' => $this->getUrlManagerFrontend()->createAbsoluteUrl(['user/update']),
            ];
            $itemsUserArray[] = [
                'label' => '<span class="glyphicon glyphicon-off"></span> Déconnexion',
                'url' => $this->getUrlManagerFrontend()->createAbsoluteUrl(['site/logout']),
            ];

            $itemUser = [
                'label' => '<span class="glyphicon glyphicon-user"></span> <span class="link-text">' .
                    ((!Yii::$app->user->isGuest) ? Html::encode(Yii::$app->user->identity->name . ' ' . strtoupper(substr(Yii::$app->user->identity->lastname, 0, 1)) . '.') : '') .
                    '</span>',
                'options' => ['id' => 'label1'],
                'url' => '#',
                'linkOptions' => ['class' => 'btn btn-default navbar-btn'],
                'items' => $itemsUserArray,
                'visible' => !Yii::$app->user->isGuest
            ];
            $itemConnexion = [
                'label' => '<span class="glyphicon glyphicon-log-in"></span> 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' => 'btn btn-default navbar-btn'],
                'visible' => \Yii::$app->user->isGuest,
                'active' => $this->getControllerAction() == 'site/login'
            ];

            $itemSignup = [
                'label' => '<span class="glyphicon glyphicon-user"></span> 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' => 'btn btn-default navbar-btn'],
                'visible' => \Yii::$app->user->isGuest,
                'active' => $this->getControllerAction() == 'site/signup'
            ];

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

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