<?php

declare(strict_types=1);

namespace App\CoreModule\Presenters;


use App\CoreModule\Model\ContactManager;
use App\CoreModule\Model\AdressManager;
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;


class ContactPresenter extends BasePresenter {

	public $contactManager;

	public $adressManager;

	public function __construct(ContactManager $contactManager, AdressManager $adressManager) {
		parent::__construct();
		$this->contactManager = $contactManager;
		$this->adressManager = $adressManager;
	}

	public function renderDefault($url = null) {

		if (!($contact = $this->contactManager->getContact($url)))
			$this->error(); 

		$this->template->contact = $contact; 
		$this->template->contactManager = $this->contactManager;

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

	public function actionRemove($url = null, $id = null) {	
		$this->contactManager->removeContact($url);
		$this->flashMessage('Kontakt byl úspěšně odstraněn.');
		$this->redirect('Adress:', $id);
	}

	public function actionEditor($url) {
		if ($url) {
			if (!($contact = $this->contactManager->getContact($url))) 
				$this->flashMessage('Kontakt nebyl nalezen.'); 
			else $this['editorForm']->setDefaults($contact); 
		}
	}

	public function actionEditorAdmin($url) {
		if ($url) {
			if (!($contact = $this->contactManager->getContactAdmin($url))) 
				$this->flashMessage('Kontakt nebyl nalezen.'); 
			else $this['editorForm']->setDefaults($contact); 
		}
	}

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

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

	public function actionNew($url) {
		if ($this->user->isLoggedIn()) $this->template->username = $this->user->identity->name;
			if (!($adress = $this->adressManager->getAdress($url)))
				$this->flashMessage('Adresář neexistuje.'); 
			else $this['newForm']; 
	}

	protected function createComponentNewForm() {
		$form = $this->formFactory->create();
		$form->addHidden('contact_id');
		$form->addHidden('adress_id');
		$form->addHidden('salt');
		$form->addHidden('created_at');
		$form->addText('name', 'Jméno')->setRequired()->setHtmlAttribute('class','form-control form');
		$form->addEmail('mail', 'E-mail')->setRequired()->setHtmlAttribute('class','form-control form');
		if ($this->user->isInRole('admin')) {
			$form->addCheckbox('active', ' Aktivní?')->setDefaultValue(1)->setHtmlAttribute('class','form');
		} else {
			$form->addHidden('active')->setDefaultValue(1);
		}
		$form->addSubmit('save', 'Uložit kontakt')->setHtmlAttribute('class','btn btn-secondary form-btn');

		$form->onSuccess[] = [$this, "editorFormSucceeded"];

		return $form;
	}

	protected function createComponentEditorForm() {

		$form = $this->formFactory->create();
		$form->addHidden('contact_id');
		$form->addHidden('adress_id');
		$form->addHidden('salt');
		$form->addHidden('created_at');
		$form->addText('name', 'Jméno')->setRequired();
		$form->addEmail('mail', 'E-mail')->setRequired();
		$form->addSubmit('save', 'Uložit kontakt');
		if ($this->user->isInRole('admin')) {
			$form->addCheckbox('active', 'Aktivní')->setDefaultValue(1);
		} else {
			$form->addHidden('active')->setDefaultValue(1);
		}

		$form->onSuccess[] = [$this, "editorFormSucceeded"];

		return $form;
	}

	public function editorFormSucceeded(Form $form, ArrayHash $values) {
		
		$zpet_id = $values->adress_id;
		$adress_id = $this->getParameter('url');

		$this->contactManager->provedZapis($form, $adress_id, $values);

		$this->flashMessage('Kontakt byl úspěšně uložen.');
		if (empty($zpet_id)) {
			$this->redirect('Adress:list');
		} else {
			$this->redirect('Adress:', $zpet_id);
		}
		
	}
	
}
