<?php


namespace App\FrontModule\Presenters;


use App\Components\Forms\BootstrapForm;
use App\Components\Forms\Factories\INewsCreateFormFactory;
use App\Components\Forms\Factories\INewsEditFormFactory;
use App\Components\Forms\NewsCreateForm;
use App\Components\Forms\NewsEditForm;
use App\Model\Entities\Components\News\News;
use App\Model\Entities\Components\News\NewsContainer;
use App\Model\Exceptions\Db\RecordNotFoundException;
use Nette\Http\IResponse;

class NewsPresenter extends FrontBasePresenter
{
    /** @var  NewsContainer */
    protected $newsContainer;
    /** @var  News */
    protected $news;

    /** @var  INewsCreateFormFactory */
    protected $newsCreateFormFactory;
    /** @var  INewsEditFormFactory */
    protected $newsEditFormFactory;

    public function injectForms(INewsCreateFormFactory $newsCreateFormFactory, INewsEditFormFactory $newsEditFormFactory)
    {
        $this->newsCreateFormFactory = $newsCreateFormFactory;
        $this->newsEditFormFactory = $newsEditFormFactory;
    }

    public function actionList($id)
    {
        try {
            $this->newsContainer = $this->siteFacade->getNewsReader()->findContainerById($id);
        } catch (RecordNotFoundException $e) {
            $this->error('Page not found.');
        }

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

        $this->pageHeader = $this->newsContainer->getName();
    }

    public function renderList()
    {
    	$this->template->newsContainer = $this->newsContainer;
        $this->template->newsList = $this->siteFacade->getNewsReader()->getNews($this->newsContainer);
    }

    public function actionCreate($id)
    {
        try {
            $this->newsContainer = $this->siteFacade->getNewsReader()->findContainerById($id);
        } catch (RecordNotFoundException $e) {
            $this->error('Page not found.');
        }

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

        $this->pageHeader = 'Create news';
        $this->breadcrumbNewsList();
    }

    public function actionEdit($id)
    {
        try {
            $this->news = $this->siteFacade->getNewsReader()->findNewsById($id);
        } catch (RecordNotFoundException $e) {
            $this->error('Page not found.');
        }

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

        $this->pageHeader = 'Edit news';
        $this->newsContainer = $this->news->getContainer();
        $this->breadcrumbNewsList();
    }

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

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


        $this->pageHeader = $this->news->getTitle();
        $this->newsContainer = $this->news->getContainer();
        $this->breadcrumbNewsList();
    }

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

    /**
     * @return BootstrapForm
     */
    protected function createComponentNewsCreateForm()
    {
        $component = $this->newsCreateFormFactory->create($this->newsContainer);
        $component->onSave[] = function (NewsCreateForm $form, News $news) {
            $this->redirect('News:edit', $news->getId());
        };
        $component->onPublish[] = function (NewsCreateForm $form, News $news) {
            if ($news->getContent()) {
                $this->redirect('News:view', $news->getId());
            } else {
                $this->redirect('News:list', $news->getContainer()->getId());
            }
        };

        return $component;
    }

    /**
     * @return BootstrapForm
     */
    protected function createComponentNewsEditForm()
    {
        $component = $this->newsEditFormFactory->create($this->news);
        $component->onSave[] = function (NewsEditForm $form, News $news) {
            $this->redirect('News:edit', $news->getId());
        };
        $component->onPublish[] = function (NewsEditForm $form, News $news) {
            if ($news->getContent()) {
                $this->redirect('News:view', $news->getId());
            } else {
                $this->redirect('News:list', $news->getContainer()->getId());
            }
        };

        return $component;
    }

    protected function breadcrumbNewsList()
    {
        $this->breadcrumb->addNode('news', $this->newsContainer->getName(), 'News:list', $this->newsContainer->getId());
    }
}