<?php

declare(strict_types=1);

namespace App\CoreModule\Presenters;

use App\Forms\SignInFormFactory;
use App\Forms\SignUpFormFactory;
use App\Presenters\BasePresenter;
use Nette\Application\AbortException;
use Nette\Application\UI\Form;
use Nette\Utils\ArrayHash;


class AdministrationPresenter extends BasePresenter {

	private $signInFactory;

	private $signUpFactory;

	public function __construct(SignInFormFactory $signInFactory, SignUpFormFactory $signUpFactory) {
		parent::__construct();
		$this->signInFactory = $signInFactory;
		$this->signUpFactory = $signUpFactory;
	}

	public function actionLogin() {
		if ($this->user->isLoggedIn()) $this->redirect('Administration:');
	}

	public function actionRegister() {
		if ($this->user->isLoggedIn()) $this->redirect('Administration:');
	}

	public function actionLogout() {
		$this->user->logout();
		$this->redirect('login');
	}

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

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

	protected function createComponentLoginForm() {
		return $this->signInFactory->create(function () {
			$this->flashMessage('Byl jste úspěšně přihlášen.');
			$this->redirect('Administration:');
		});
	}

	protected function createComponentRegisterForm() {
		return $this->signUpFactory->create(function (Form $form, ArrayHash $values) {
			$this->flashMessage('Byl jste úspěšně zaregistrován.');
			$this->user->login($values->username, $values->password); 
			$this->redirect('Administration:');
		});
	}
}
