<?php

declare(strict_types=1);

namespace App\Presenters;

use Nette;
use Exception;
use Nette\Application\Responses;

final class LoginPresenter extends BasePresenter
{
    public function renderDefault() {}

    public function actionLoginInstructor(): void
	{
		$httpRequest = $this->getHttpRequest();
		$body = $httpRequest->getRawBody();
		$data = json_decode($body);

		try {
			$userData = $this->personManager->getByEmail($data->email);
			if($userData->count('*') == 0) {
				$this->error('nespravne prihlasovaci udaje', Nette\Http\Response::S401_UNAUTHORIZED);
			}
			$this->template->user->login($data->email, $data->password);		
			$userData = $this->personManager->getById($this->getUser()->getId());
			$transfered = $this->applyTransfer($userData, INSTRUCTOR_BASIC_DATA_TRANSFER, TRANSFER_TO_JSON, DATA_OBJECT);
			$json['instructor']['poznamky'] = '';// poznamky nejsou viditelne pro samozneho uzivatele
			$json = array(
				'logged' => 'instructor',
				'instructor' => $transfered,
				'school' => null
			);
			$this->sendJson($json);	
		} catch (Nette\Security\AuthenticationException $e) {
			$this->error('nespravne prihlasovaci udaje', Nette\Http\Response::S401_UNAUTHORIZED);
		}
	}

	public function actionRegisterInstructor(): void
	{
		$httpRequest = $this->getHttpRequest();
		$body = $httpRequest->getRawBody();
		$data = json_decode($body, true);
		try {
			$transfer = INSTRUCTOR_BASIC_DATA_TRANSFER;
			unset($transfer['ID'], $transfer['opravneni'], $transfer['poznamky'], $transfer['max_role']);
			$dbData = $transfered = $this->applyTransfer($data, $transfer, TRANSFER_TO_DB, DATA_SINGLE_ROW);
			$dbData['heslo'] = $data['password'];
			$userData = $this->personManager->registerInstructor($dbData);
			if($userData == false) {
				$this->error('nepovedlo se vytvorit ucet', Nette\Http\Response::S400_BAD_REQUEST);
			}
			$this->template->user->login($data['email'], $data['password']);
			$userData = $this->personManager->getById($this->getUser()->getId());
			if(!$userData) {
				$this->getUser()->logout(true);
				$this->error('nespravne prihlasovaci udaje', Nette\Http\Response::S401_UNAUTHORIZED);
			}
			$transfered = $this->applyTransfer($userData, INSTRUCTOR_BASIC_DATA_TRANSFER, TRANSFER_TO_JSON, DATA_OBJECT);
			$json['instructor']['poznamky'] = '';// poznamky nejsou viditelne pro samozneho uzivatele
			$json = array(
				'logged' => 'instructor',
				'instructor' => $transfered,
				'school' => null
			);	
			$this->sendJson($json);	
		} catch (Nette\Security\AuthenticationException $e) {
			$this->error('nespravne prihlasovaci udaje', Nette\Http\Response::S400_BAD_REQUEST);
		}
	}

	public function actionLoginSchool(): void
	{
		$httpRequest = $this->getHttpRequest();
		$body = $httpRequest->getRawBody();
		$data = json_decode($body);

		try {
			$schoolData = $this->schoolManager->getByEmail($data->email);
			if($schoolData->count('*') == 0) {
				$this->error('nespravne prihlasovaci udaje', Nette\Http\Response::S401_UNAUTHORIZED);
			}
			$this->template->user->login($data->email, $data->password);			
			$schoolData = $this->schoolManager->getById($this->getUser()->getId());
			$transfered = $this->applyTransfer($schoolData, SCHOOL_BASIC_DATA_TRANSFER, TRANSFER_TO_JSON, DATA_OBJECT);
			$json = array(
				'logged' => 'school',
				'instructor' => null,
				'school' => $transfered
			);
			$this->sendJson($json);	
		} catch (Nette\Security\AuthenticationException $e) {
			$this->error('nespravne prihlasovaci udaje', Nette\Http\Response::S401_UNAUTHORIZED);
		}
	}

	public function actionRegisterSchool(): void
	{
		$httpRequest = $this->getHttpRequest();
		$body = $httpRequest->getRawBody();
		$data = json_decode($body, true);
		try {
			$modifiedTransfer = SCHOOL_BASIC_DATA_TRANSFER;
			unset($modifiedTransfer['ID']);
			$dbData = $this->applyTransfer($data, $modifiedTransfer, TRANSFER_TO_DB, DATA_SINGLE_ROW);

			$dbData['heslo'] = $data['password'];
			$schoolData = $this->schoolManager->registerSchool($dbData);
			if($schoolData == false) {
				$this->error('nepovedlo se vytvorit ucet', Nette\Http\Response::S400_BAD_REQUEST);
			}
			$this->template->user->login($data['email'], $data['password']);
			$schoolData = $this->schoolManager->getById($this->getUser()->getId());
			if(!$schoolData) {
				$this->getUser()->logout(true);
				$this->error('nespravne prihlasovaci udaje', Nette\Http\Response::S401_UNAUTHORIZED);
			}
			$transfered = $this->applyTransfer($schoolData, SCHOOL_BASIC_DATA_TRANSFER, TRANSFER_TO_JSON, DATA_OBJECT);
			$json = array(
				'logged' => 'school',
				'instructor' => null,
				'school' => $transfered
			);
			$this->sendJson($json);	
		} catch (Nette\Security\AuthenticationException $e) {
			$this->error('nespravne prihlasovaci udaje', Nette\Http\Response::S400_BAD_REQUEST);
		}
	}


	public function actionGetUserData(): void
	{
		$notLoggedJson = array(
			'logged' => 'none',
			'instructor' => null,
			'school' => null
		);
		if($this->getUser()->isLoggedIn()) {
			if($this->getUser()->isInRole('instructor')){
				try {
					$userData = $this->personManager->getById($this->getUser()->getId());
				} catch (Exception $e) {
					$this->getUser()->logout(true);
					$this->error('data se nepovedlo ulozit', Nette\Http\Response::S500_INTERNAL_SERVER_ERROR);
				}
				if($userData) {
					$transfered = $this->applyTransfer($userData, INSTRUCTOR_BASIC_DATA_TRANSFER, TRANSFER_TO_JSON, DATA_OBJECT);
					$json['instructor']['poznamky'] = '';// poznamky nejsou viditelne pro samotneho uzivatele
					$json = array(
						'logged' => 'instructor',
						'instructor' => $transfered,
						'school' => null
					);
				} else {
					$this->getUser()->logout(true);
					$json = $notLoggedJson;
				}
			}
			if($this->getUser()->isInRole('school')){
				try {
					$schoolData = $this->schoolManager->getById($this->getUser()->getId());
				} catch (Exception $e) {
					$this->getUser()->logout(true);
					$this->error('data se nepovedlo ulozit', Nette\Http\Response::S500_INTERNAL_SERVER_ERROR);
				}
				if($schoolData) {
					$transfered = $this->applyTransfer($schoolData, SCHOOL_BASIC_DATA_TRANSFER, TRANSFER_TO_JSON, DATA_OBJECT);
					$json = array(
						'logged' => 'school',
						'instructor' => null,
						'school' => $transfered
					);
				} else {
					$this->getUser()->logout(true);
					$json = $notLoggedJson;
				}
			}
			$this->sendJson($json);	
		} else {
			$json = $notLoggedJson;
			$this->sendJson($json);
		}
	}
}
