<?php

declare(strict_types=1);

namespace App\CoreModule\Presenters;

use App\Presenters\BasePresenter;
use App\Model\UserManager;
use Nette\Application\AbortException;
use Nette\Application\UI\Form;
use Nette\Mail\IMailer;
use Nette\Mail\Message;
use Nette\Mail\SendException;
use Nette\Utils\ArrayHash;

class SettingsPresenter extends BasePresenter {	

	private $contactEmail;

	private $mailer;

	private $userManager;

	public function __construct($contactEmail, UserManager $userManager, IMailer $mailer) {
		parent::__construct();
		$this->userManager = $userManager;
		$this->contactEmail = $contactEmail;
		$this->mailer = $mailer;
	}

	public function renderList() {

		$this->template->users = $this->userManager->getUsers();

		$this->template->userManager = $this->userManager;

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

	}

	public function actionDefault() {

		if (!($this->user->isLoggedIn()))
			$this->redirect('Administration:login');
	}

	public function actionRemove($id) {

		$this->userManager->removeUser($id);
		$this->flashMessage("Uživatel úspěšně odstraněn!");
		$this->redirect('Settings:list');
	}

	public function actionOpravneni($id) {

		$this->userManager->zmenitOpravneni($id);
		$this->flashMessage("Oprávnění změněno úspěšně!");
		$this->redirect('Settings:list');
	}

	public function renderDefault() {

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

	}

	protected function createComponentSettingsForm() {

		$form = $this->formFactory->create();
		$form->addHidden('username');
		$form->addPassword('password', 'Heslo')->setHtmlAttribute('class','form-control form');
		$form->addPassword('password_repeat', 'Heslo znovu')->setOmitted()->setRequired(false)
			->addRule(Form::EQUAL, 'Hesla nejsou stejná.', $form['password'])->setHtmlAttribute('class','form-control form');
		$form->addSubmit('save', 'Uložit')->setHtmlAttribute('class','btn btn-secondary form-btn');

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

		return $form;
	}

	public function settingsFormSucceeded(Form $form, ArrayHash $values) {
		$overeni = $this->user->identity->name;
		$values->username = $this->user->identity->name;
		$this->userManager->save($values, $overeni);
		$this->redirect(':Core:Administration:logout');
	}
	
	protected function createComponentOpravneniForm() {
		$form = $this->formFactory->create();
		$form->getElementPrototype()->setAttribute('novalidate', true);
		$form->addEmail('email', 'E-mailová adresa')->setRequired()->setHtmlAttribute('class','form-control form');
		$form->addText('name', 'Uživatelské jméno')->setRequired()->setHtmlAttribute('class','form-control form');
		$form->addTextArea('message', 'Žádost')->setRequired()
			->addRule(Form::MIN_LENGTH, 'Žádost musí být minimálně %d znaků dlouhá.', 10)->setHtmlAttribute('class','form-control form');
		$form->addSubmit('send', 'Odeslat')->setHtmlAttribute('class','btn btn-seondary form-btn');
		
		$form->onSuccess[] = function (Form $form, ArrayHash $values) {
			try {
				$mail = new Message;
				$mail->setFrom($values->email)
					->addTo($this->contactEmail)
					->setSubject('Žádost o oprávnění | ' . $values->name)
					->setBody($values->message);
				$this->mailer->send($mail);
				$this->flashMessage('Žádost byla úspěšně odeslána.');
				$this->redirect('this');
			} catch (SendException $e) {
				$this->flashMessage('Žádost se nepodařilo odeslat.');
			}
		};

		return $form;
	}


}
