<?php


namespace App\FrontModule\Presenters;


use App\Components\Forms\Factories\INavItemForumFormFactory;
use App\Components\Forms\Factories\INavItemGalleryFormFactory;
use App\Components\Forms\Factories\INavItemNewsFormFactory;
use App\Components\Forms\NavItemForumForm;
use App\Components\Forms\NavItemGalleryForm;
use App\Components\Forms\NavItemNewsForm;
use App\Model\Entities\Components\Navigation\NavItemForum;
use App\Model\Entities\Components\Navigation\NavItemGallery;
use App\Model\Entities\Components\Navigation\NavItemNews;
use Nette\Http\IResponse;

class SettingsPresenter extends FrontBasePresenter
{
    /** @var  INavItemNewsFormFactory */
    protected $navItemNewsFormFactory;
    /** @var  INavItemGalleryFormFactory */
    protected $navItemGalleryFormFactory;
    /** @var  INavItemForumFormFactory */
    protected $navItemForumFormFactory;

    public function injectForms(INavItemNewsFormFactory $navItemNewsFormFactory, INavItemGalleryFormFactory $navItemGalleryFormFactory, INavItemForumFormFactory $navItemForumFormFactory)
    {
        $this->navItemNewsFormFactory = $navItemNewsFormFactory;
        $this->navItemGalleryFormFactory = $navItemGalleryFormFactory;
        $this->navItemForumFormFactory = $navItemForumFormFactory;
    }

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

        if ($this->action !== 'default') {
            $this->breadcrumb->addNode('gameSettings', 'Settings', 'Settings:');
        }
    }

    public function actionDefault()
    {
        $this->pageHeader = 'Settings';
    }

    public function actionNewSection()
    {
        if (!$this->user->isAllowed($this->site, 'add_section')) {
            $this->error(null, IResponse::S403_FORBIDDEN);
        }

        $this->pageHeader = 'New section';
    }

    public function actionAddNewsSection()
    {
        $this->pageHeader = 'Add news';
        $this->breadcrumbNewSection();
    }

    public function actionAddGallerySection()
    {
        $this->pageHeader = 'Add gallery';
        $this->breadcrumbNewSection();
    }

    public function actionAddForumSection()
    {
        $this->pageHeader = 'Add forum';
        $this->breadcrumbNewSection();
    }


    /**
     * @return NavItemNewsForm
     */
    protected function createComponentNavItemNewsForm() : NavItemNewsForm
    {
        $component = $this->navItemNewsFormFactory->create($this->site);
        $component->onSave[] = function (NavItemNewsForm $form, NavItemNews $itemNews) {
            $this->redirect($itemNews->getLinkDestination(), $itemNews->getLinkParams());
        };

        return $component;
    }

    /**
     * @return NavItemGalleryForm
     */
    protected function createComponentNavItemGalleryForm() : NavItemGalleryForm
    {
        $component = $this->navItemGalleryFormFactory->create($this->site);
        $component->onSave[] = function (NavItemGalleryForm $form, NavItemGallery $itemGallery) {
            $this->redirect($itemGallery->getLinkDestination(), $itemGallery->getLinkParams());
        };

        return $component;
    }

    /**
     * @return NavItemForumForm
     */
    protected function createComponentNavItemForumForm() : NavItemForumForm
    {
        $component = $this->navItemForumFormFactory->create($this->site);
        $component->onSave[] = function (NavItemForumForm $form, NavItemForum $itemForum) {
            $this->redirect($itemForum->getLinkDestination(), $itemForum->getLinkParams());
        };

        return $component;
    }

    protected function breadcrumbNewSection()
    {
        $this->breadcrumb->addNode('gameSettingsNewSection', 'New section', 'Settings:newSection');
    }
}