<?php

declare(strict_types=1);

namespace App\CoreModule\Presenters;

use App\CoreModule\Model\EventManager;
use App\Presenters\BasePresenter;
use Nette\Application\AbortException;
use Nette\Application\BadRequestException;
use Nette\Application\UI\Form;
use Nette\Database\UniqueConstraintViolationException;
use Nette\Utils\ArrayHash;
use Nette\Utils\Strings;

class EventPresenter extends BasePresenter {

	public $eventManager;

	public function __construct(EventManager $eventManager)
	{
		parent::__construct();
		$this->eventManager = $eventManager;
	}

	public function renderDefault($url = null) {

		if (!$url) {
			$this->redirect('Event:list');		
		}

		if (is_numeric($url)) {

			if ($this->user->isInRole('admin')) {
				if (!($event = $this->eventManager->idToURLAdmin($url)))
					$this->error();
			} else {
				if (!($event = $this->eventManager->idToURL($url)))
					$this->error();
			}

		} else {

			if ($this->user->isInRole('admin')) {
				if (!($event = $this->eventManager->getEventAdmin($url)))
					$this->error();
			} else {
				if (!($event = $this->eventManager->getEvent($url)))
					$this->error();
			}
		}

		$this->template->event = $event; 
		if ($this->user->isLoggedIn()) $this->template->username = $this->user->identity->name;
	}

	public function renderList() {	

		if ($this->user->isInRole('admin')) 
			$this->template->events = $this->eventManager->getEventsAdmin();
		else 
			$this->template->events = $this->eventManager->getEvents();

		if ($this->user->isLoggedIn()) $this->template->username = $this->user->identity->name;

	}

	public function renderEditor() {	
		if ($this->user->isLoggedIn()) $this->template->username = $this->user->identity->name;
	}

	public function actionRemove($url = null)
	{
		$this->eventManager->removEevent($url);
		$this->flashMessage('Událost byla úspěšně odstraněna.');
		$this->redirect('Event:list');
	}

	public function actionEditor($url = null)
	{
		if ($url) {
			if (!($event = $this->eventManager->getEventAdmin($url)))
				$this->flashMessage('Událost nebyla nalezena.'); 
			else $this['editorForm']->setDefaults($event); 
		}
	}

	protected function createComponentEditorForm() {
	
		$form = $this->formFactory->create();
		$form->addHidden('event_id')->setHtmlAttribute('class','form-control form');
		$form->addText('name', 'Titulek:')->setRequired()->setHtmlAttribute('class','form-control form');
		$form->addText('url', 'URL')->setRequired()->setHtmlAttribute('class','form-control form');
		$form->addCheckbox('otevreno', ' Aktivní?')->setHtmlAttribute('class','form');
		$form->addTextArea('popis')->setHtmlAttribute('class','form-control form');
		$form->addSubmit('save', 'Uložit událost')->setHtmlAttribute('class','btn btn-secondary form-btn-b');
		$form->onSuccess[] = function (Form $form, ArrayHash $values) {
			try {
				$this->eventManager->saveEvent($values);
				$this->flashMessage('Událost byla úspěšně uložena.');
				$this->redirect('Event:', $values->url);
			} catch (UniqueConstraintViolationException $e) {
				$this->flashMessage('Událost s touto URL adresou již existuje.');
			}
		};

		return $form;
	}
}
