<?php

declare(strict_types=1);

namespace App\Presenters;

use Nette;
use Exception;

define("FUND_DATA_TRANSFER", array(
	'ID' => 'ID',
	'ID_osoby' => 'personId',
	'ID_kurzu' => 'courseId',
	'castka' => 'amount',
	'popis' => 'description',
	'datum' => 'date',
));

final class FundPresenter extends BasePresenter
{
    public function renderDefault() {

    }

	public function actionCreateFundRecord(): void
	{
		$this->checkUserAccess('zamestnanec');
		$httpRequest = $this->getHttpRequest();
		$body = $httpRequest->getRawBody();
		$json = json_decode($body);	
		$data = $this->applyTransfer($json, FUND_DATA_TRANSFER, TRANSFER_TO_DB, DATA_OBJECT);
		$this->saveLog('fond', 'vlozeni', $data);
		$result = $this->fundManager->createRecord($data);

		if(!$result) {
			$this->error('data se nepovedlo ulozit', Nette\Http\Response::S500_INTERNAL_SERVER_ERROR);
		}

		$json = $this->applyTransfer($result, FUND_DATA_TRANSFER, TRANSFER_TO_JSON, DATA_OBJECT);
		$this->sendJson($json);	
	}

	public function actionUpdateFundRecord(): void
	{
		$this->checkUserAccess('zamestnanec');
		$httpRequest = $this->getHttpRequest();
		$body = $httpRequest->getRawBody();
		$json = json_decode($body);	
		$data = $this->applyTransfer($json, FUND_DATA_TRANSFER, TRANSFER_TO_DB, DATA_OBJECT);
		$this->saveLog('fond', 'uprava', $data);

		try {
			$result = $this->fundManager->updateRecord($data);
		} catch (Exception $e) {
			$this->error('data se nepovedlo ulozit', Nette\Http\Response::S500_INTERNAL_SERVER_ERROR);
		}

		$data = $this->fundManager->getById($json->ID);
		$json = $this->applyTransfer($data, FUND_DATA_TRANSFER, TRANSFER_TO_JSON, DATA_OBJECT);
		$this->sendJson($json);	
	}

	public function actionRemoveFundRecord(): void
	{
		$this->checkUserAccess('zamestnanec');
		$httpRequest = $this->getHttpRequest();
		$recordId = $httpRequest->getQuery('recordId');
		if($recordId) {
			$this->saveLog('fond', 'smazani', array('ID' => $recordId));
			try {
				$result = $this->fundManager->removeRecord($recordId);
			} catch (Exception $e) {
				$this->error('data se nepovedlo ulozit', Nette\Http\Response::S500_INTERNAL_SERVER_ERROR);
			}
			$this->sendJson(array());	
		} else {
			$this->error('nebyl vlozen parametr id', Nette\Http\Response::S400_BAD_REQUEST);
		}	
	}

	public function actionGetMyFund(): void
	{
		$this->checkUserAccess('instruktor');
		if($this->getUser()->isLoggedIn()) {
			$data = $this->fundManager->getByPersonId($this->getUser()->getId());
			$json = $this->applyTransfer($data, FUND_DATA_TRANSFER, TRANSFER_TO_JSON);
			$this->sendJson($json);	
		} else {
			$this->error('uzivatel neni prihlasen', Nette\Http\Response::S403_FORBIDDEN);
		}
	}

	public function actionGetInstructorFund(): void
	{
		$this->checkUserAccess('zamestnanec');
		if($this->getUser()->isLoggedIn()) {
			$httpRequest = $this->getHttpRequest();
			$id = $httpRequest->getQuery('personId');
			if($id) {
				$data = $this->fundManager->getByPersonId($id);
				$json = $this->applyTransfer($data, FUND_DATA_TRANSFER, TRANSFER_TO_JSON);
				$this->sendJson($json);	
			} else {
				$this->error('nebyl vlozen parametr id', Nette\Http\Response::S400_BAD_REQUEST);
			}
		} else {
			$this->error('uzivatel neni prihlasen', Nette\Http\Response::S403_FORBIDDEN);
		}
	}
}
