<?php


namespace App\FrontModule\Presenters;



use App\Components\Forms\BootstrapForm;
use App\Components\Forms\Factories\IForumCreateFormFactory;
use App\Components\Forms\Factories\INewForumPostFormFactory;
use App\Components\Forms\Factories\ITopicCreateFormFactory;
use App\Components\Forms\ForumCreateForm;
use App\Components\Forms\NewForumPostForm;
use App\Components\Forms\TopicCreateForm;
use App\Model\Entities\Components\Forum\Forum;
use App\Model\Entities\Components\Forum\Topic;
use App\Model\Exceptions\Db\RecordNotFoundException;
use Nette\Http\IResponse;

class ForumPresenter extends FrontBasePresenter
{
    /** @var  Forum */
    protected $forum;
    /** @var  Topic */
    protected $topic;

    /** @var  IForumCreateFormFactory */
    protected $forumCreateFormFactory;
    /** @var  ITopicCreateFormFactory */
    protected $topicCreateFormFactory;
    /** @var  INewForumPostFormFactory */
    protected $newForumPostFormFactory;

    public function injectForms(IForumCreateFormFactory $forumCreateFormFactory, ITopicCreateFormFactory $topicCreateFormFactory, INewForumPostFormFactory $newForumPostFormFactory)
    {
        $this->forumCreateFormFactory = $forumCreateFormFactory;
        $this->topicCreateFormFactory = $topicCreateFormFactory;
        $this->newForumPostFormFactory = $newForumPostFormFactory;
    }

    public function actionView($id)
    {
        try {
            $this->forum = $this->siteFacade->getForumReader()->findForumById($id);
        } catch (RecordNotFoundException $e) {
            $this->error('Page not found.');
        }

        if (!$this->user->isAllowed($this->forum, 'view')) {
            $this->error(null, IResponse::S403_FORBIDDEN);
        }

        $this->pageHeader = $this->forum->getName();
        $this->breadcrumb->resetLastInsertedNode();
        $forum = $this->forum;
        while ($forum = $forum->getParentForum()) {
            $this->breadcrumb->prependLastInsertedNode("forum{$forum->getId()}", $forum->getName(), 'Forum:view', $forum->getId());
        }
    }

    public function renderView()
    {
        $this->template->forum = $this->forum;
    }

    /**
     * @param $id
     */
    public function actionTopic($id)
    {
        try {
            $this->topic = $this->siteFacade->getForumReader()->findTopicById($id);
        } catch (RecordNotFoundException $e) {
            $this->error('Page not found,');
        }

        if (!$this->user->isAllowed($this->topic->getForum(), 'view')) {
            $this->error(null, IResponse::S403_FORBIDDEN);
        }

        $this->pageHeader = $this->topic->getTitle();
        $this->breadcrumb->resetLastInsertedNode();
        $forum = $this->topic->getForum();
        while ($forum) {
            $this->breadcrumb->prependLastInsertedNode("forum{$forum->getId()}", $forum->getName(), 'Forum:view', $forum->getId());
            $forum = $forum->getParentForum();
        }
    }

    public function renderTopic()
    {
        $this->template->topic = $this->topic;
    }

    public function actionAddSubforum($id)
    {
        try {
            $this->forum = $this->siteFacade->getForumReader()->findForumById($id);
        } catch (RecordNotFoundException $e) {
            $this->error('Page not found.');
        }

        if (!$this->user->isAllowed($this->forum, 'create_subforum')) {
            $this->error(null, IResponse::S403_FORBIDDEN);
        }

        $this->pageHeader = 'Add subforum';
        $this->breadcrumb->resetLastInsertedNode();
        $forum = $this->forum;
        while ($forum) {
            $this->breadcrumb->prependLastInsertedNode("forum{$forum->getId()}", $forum->getName(), 'Forum:view', $forum->getId());
            $forum = $forum->getParentForum();
        }
    }

    public function actionAddTopic($id)
    {
        try {
            $this->forum = $this->siteFacade->getForumReader()->findForumById($id);
        } catch (RecordNotFoundException $e) {
            $this->error('Page not found.');
        }

        if (!$this->user->isAllowed($this->forum, 'create_topic')) {
            $this->error(null, IResponse::S403_FORBIDDEN);
        }

        $this->pageHeader = 'Add topic';
        $this->breadcrumb->resetLastInsertedNode();
        $forum = $this->forum;
        while ($forum) {
            $this->breadcrumb->prependLastInsertedNode("forum{$forum->getId()}", $forum->getName(), 'Forum:view', $forum->getId());
            $forum = $forum->getParentForum();
        }
    }

    /**
     * @return ForumCreateForm
     */
    protected function createComponentAddSubforumForm() : ForumCreateForm
    {
        $component = $this->forumCreateFormFactory->create($this->forum);
        $component->onSave[] = function () {
            $this->redirect('Forum:view', $this->forum->getId());
        };

        return $component;
    }

    protected function createComponentAddTopicForm() : TopicCreateForm
    {
        $component = $this->topicCreateFormFactory->create($this->forum);
        $component->onSave[] = function (BootstrapForm $form, Topic $topic) {
            $this->redirect('Forum:topic', $topic->getId());
        };

        return $component;
    }

    protected function createComponentNewPostForm() : NewForumPostForm
    {
        $component = $this->newForumPostFormFactory->create($this->topic);
        $component->onSave[] = function () {
            $this->redirect('this');
        };

        return $component;
    }
}