<?php


namespace App\FrontModule\Presenters;


use App\Components\Carousel\Carousel;
use App\Components\Carousel\ICarouselFactory;
use App\Components\Sidebar\SidebarMenu\SidebarMenu;
use App\Model\Entities\PortalSite;
use App\Model\Exceptions\Db\RecordNotFoundException;
use App\Model\Facades\SiteFacade;
use App\Model\Facades\UserFacade;
use App\Model\Interfaces\ISite;
use App\Presenters\BasePresenter;

class FrontBasePresenter extends BasePresenter
{
    /** @var  ISite */
    protected $site;
    /** @var  PortalSite */
    protected $portalSite;

    /** @var  SiteFacade */
    protected $siteFacade;
    /** @var  UserFacade */
    protected $userFacade;
    /** @var  ICarouselFactory */
    protected $carouselFactory;

    /** @persistent */
    public $siteId;

    public function injectFrontBase(SiteFacade $siteFacade, UserFacade $userFacade, ICarouselFactory $carouselFactory)
    {
        $this->siteFacade = $siteFacade;
        $this->userFacade = $userFacade;
        $this->carouselFactory = $carouselFactory;
    }

    protected function startup()
    {
        parent::startup();

        if (!$this->user->isLoggedIn()) {
            $this->setLayout('layout_guest');
        }

        try {
            $this->portalSite = $this->siteFacade->getSiteReader()->findPortalSite();
            if ($this->siteId != null) {
                $this->site = $this->siteFacade->getSiteReader()->findSiteById($this->siteId);
            } else {
                $this->site = $this->portalSite;
            }
        } catch (RecordNotFoundException $e) {
            $this->siteId = null;
            $this->error('Site not found.');
        }

        if (!$this->site->isGlobalSite()) {
            $this->breadcrumb->addNode('site', $this->site->getName(), 'Homepage:');
        }
    }

    /**
     * @return SidebarMenu
     */
    protected function createComponentSidebarMenu() : SidebarMenu
    {
        $menu = new SidebarMenu();
        foreach($this->siteFacade->getNavigationReader()->getSiteNavigation($this->portalSite) as $navItem) {
            if (!$this->user->isAllowed($navItem->getResource(), 'view')) {
                continue;
            }
            $params = $navItem->getLinkParams();
            $params['siteId'] = null;
            $menu->addLink($navItem->getId(), $navItem->getLabel(), $navItem->getLinkDestination(), $params)
                ->addFaIcon($navItem->getIcon());
        }

        $menu->addLink('gamesList', $this->translator->translate('Games list'), 'Games:list', ['siteId' => null])
            ->addFaIcon('list');

        if ($this->user->isLoggedIn()) {
            $menu->addLink('myEvents', $this->translator->translate('My events'), 'Events:', ['siteId' => null])
                ->addFaIcon('calendar');

//            $gamesCollection = $this->user->getUserEntity()->getGames();
//            if ($gamesCollection->count() > 0) {
//                $games = $menu->addLinkGroup('games', 'My Games');
//                $games->addFaIcon('gamepad');
//                foreach($gamesCollection as $game) {
//                    $games->addLink("game{$game->getId()}", $game->getName(), "Homepage:", ['siteId' => $game->getId()])
//                        ->addFaIcon('circle-thin');
//                }
//                if ($this->site->isGlobalSite()) {
//                    $games->expand();
//                }
//            }

            $guildsCollection = $this->user->getUserEntity()->getCommunitySites();
            if ($guildsCollection->count() > 0) {
                $guilds = $menu->addLinkGroup('guilds', $this->translator->translate('My Guilds'));
                $guilds->addFaIcon('fort-awesome');
                foreach ($guildsCollection as $guild) {
                    $guilds->addLink("guild{$guild->getId()}", $guild->getName(), "Homepage:", ['siteId' => $guild->getId()])
                        ->addFaIcon('circle-thin');
                }
                if ($this->site->isGlobalSite()) {
                    $guilds->expand();
                }
            }
        }

        if (!$this->site->isGlobalSite()) {
            $menu->addHeader('siteHeader', $this->site->getName());
            if ($this->site->isGameSite()) {
//                $menu->addLink('gameGuides', 'Guides', 'this')
//                    ->addFaIcon('graduation-cap');
                if ($this->user->isLoggedIn()) {
                    $menu->addLink('createGuild', $this->translator->translate('Create guild'), 'Guild:create')
                        ->addGlyphicon('tower');
                }
                $menu->addLink('guildsList', $this->translator->translate('Guilds list'), 'Guild:list')
                    ->addFaIcon('list');
            }
            if ($this->site->isGuildSite()) {
                if ($this->user->isLoggedIn() && !$guildsCollection->contains($this->site)) {
                    $menu->addLink('joinGuild', $this->translator->translate('Join guild'), 'joinGuild!')
                        ->addFaIcon('sign-in');
                }
                $menu->addLink('events', $this->translator->translate('Events'), 'Events:')
                    ->addFaIcon('calendar');
            }
            foreach($this->siteFacade->getNavigationReader()->getSiteNavigation($this->site) as $navItem) {
                if (!$this->user->isAllowed($navItem->getResource(), 'view')) {
                    continue;
                }
                $menu->addLink($navItem->getId(), $navItem->getLabel(), $navItem->getLinkDestination(), $navItem->getLinkParams())
                    ->addFaIcon($navItem->getIcon());
            }
        }
        if ($this->user->isAllowed($this->site, 'add_section')) {
            $menu->addLink('newSection', $this->translator->translate('New section'), 'Settings:newSection')
                ->addFaIcon('plus');
        }

        return $menu;
    }

    protected function createComponentSiteCarousel() : Carousel
    {
        return $this->carouselFactory->create();
    }

    public function handleJoinGuild()
    {
        if ($this->site->isGuildSite() && $this->user->isLoggedIn()) {
            $this->siteFacade->getSiteWriter()->joinGuild($this->site);
        }
        $this->redirect('this');
    }

}