<?php
/**
 * @copyright (c) Kufer Software GmbH, Oderstraße 7, D-84453 Mühldorf
 */

namespace Kufer\Web\KuferTools\Basis\AnwesenheitenKurs;

use Kufer\Web\KuferTools\Utils\StatusMessages\StatusMessage;

class AnwesenheitenPresenter extends \Kufer\Web\KuferTools\Basis\Presenter\MitRahmenPresenter
{
  protected function getContent()
  {
    $view = $this->getView();

    if (count($this->useCaseResponse->belKtInfos) == 0) {
      $result = $view->getNoData();
    } else {
      $result = $view->getHeader();
      $result .= $view->getBody();
      $result .= $view->getFooter();
      $result .= $view->getNoColumn();
    }

    return $result;
  }

  protected function getSeitenTitel(): string
  {
    return 'Anwesenheiten für Kurs '
      . $this->useCaseResponse->kurs->nr_anzeige . ' - '
      . $this->useCaseResponse->kurs->getVarValue('kurzbez');
  }

  protected function getView()
  {
    return new AnwesenheitenView($this->getViewModel());
  }

  protected function getViewModel()
  {
    $viewModel = new AnwesenheitenViewModel();
    $viewModel->toolId = $this->useCaseResponse->toolId;
    $viewModel->kurs = $this->useCaseResponse->kurs;
    $viewModel->kursTageInfo = $this->useCaseResponse->kursTageInfo;
    $viewModel->showFilterButton = $this->useCaseResponse->showFilterButton ? '1' : '0';
    $viewModel->zeigeUhrzeit = $this->useCaseResponse->zeigeUhrzeit ? '1' : '0';
    $viewModel->zeigeUE = $this->useCaseResponse->zeigeUE ? '1' : '0';
    $viewModel->zeigeDozent = $this->useCaseResponse->zeigeDozent ? '1' : '0';
    $viewModel->zeigeFaecher = $this->useCaseResponse->zeigeFaecher ? '1' : '0';
    $viewModel->zeigeBemerkung = $this->useCaseResponse->zeigeBemerkung ? '1' : '0';
    $viewModel->zeigeAnwInProzent = $this->useCaseResponse->zeigeAnwInProzent ? '1' : '0';
    $viewModel->zeigeAnwTage = $this->useCaseResponse->zeigeAnwTage ? '1' : '0';
    $viewModel->zeigeAbwTage = $this->useCaseResponse->zeigeAbwTage ? '1' : '0';
    $viewModel->isBearbeitbar = $this->useCaseResponse->isBearbeitbar;
    $viewModel->anwInZukunftBearbeitenErlaubt = $this->useCaseResponse->anwInZukunftBearbeitenErlaubt;
    $viewModel->anwVonAnderenDozentenBearbeitbar = $this->useCaseResponse->anwVonAnderenDozentenBearbeitbar;
    $viewModel->setFilter = $this->useCaseResponse->setFilter;

    $viewModel->messageSuccess = $this->getMessageStringSuccess();
    $viewModel->messageError = $this->getMessageStringError();
    $viewModel->messageWarning = $this->getMessageStringWarning();
    $viewModel->messageInfo = $this->getMessageStringInfo();

    $viewModel->backButtonLabel = $this->useCaseResponse->backButtonLabel;
    $viewModel->backButtonLink = $this->useCaseResponse->backButtonLink;

    $anwLegendeViewModel = new AnwesenheitenLegendeViewModel();
    $anwLegendeViewModel->anwGruendeLegende = $this->useCaseResponse->anwGruendeLegende;
    $viewModel->htmllegende = (new AnwesenheitenLegendeView($anwLegendeViewModel))->show();

    return $viewModel;
  }

  protected function getCssFilesSrcAry(): array
  {
    return array_merge(
      parent::getCssFilesSrcAry(),
      array($this->getCssLinkFromSys('anwesenheiten_tabelle.css'))
    );
  }

  protected function getJsFilesSrcAryWeitere(): array
  {
    return array(
      $this->getJsFileFromSys('anwesenheitenFuerKurs.js'),
      $this->getJsFileFromSys('filter.js')
    );
  }

  
  protected function getJSDeclarations(): string
  {
    $result = parent::getJSDeclarations();
    $result .= \JsOperations::getDeclarationString('KuferTools.Knr', $this->useCaseResponse->kurs->id);
    $result .= \JsOperations::getDeclarationString('KuferTools.Dnr', $this->useCaseResponse->user->dnr);
    $result .= \JsOperations::getDeclarationString('KuferTools.Dguid', (string)$this->useCaseResponse->user->dnr_guid);

    $result .= \JsOperations::getDeclarationJsonJustStrings('KuferTools.KursTageInfo', $this->useCaseResponse->kursTageInfo);
    $result .= \JsOperations::getDeclarationJsonJustStrings('KuferTools.BelKtInfos', $this->useCaseResponse->belKtInfos);
    $result .= \JsOperations::getDeclarationJsonJustStrings('KuferTools.BelIdsOrder', \array_keys($this->useCaseResponse->belKtInfos));
    $result .= \JsOperations::getDeclarationJsonJustStrings('KuferTools.BelKtInfosOrig', $this->useCaseResponse->belKtInfos);
    $result .= \JsOperations::getDeclarationJsonJustStrings('KuferTools.AnwGruende', $this->useCaseResponse->anwGruende);
    $result .= \JsOperations::getDeclarationString('KuferTools.Xsrf', $this->useCaseResponse->xsrf);
    $result .= \JsOperations::getDeclarationBoolean('KuferTools.isBearbeitbar', $this->useCaseResponse->isBearbeitbar);
    $result .= \JsOperations::getDeclarationBoolean('KuferTools.anwInZukunftBearbeitenErlaubt', $this->useCaseResponse->anwInZukunftBearbeitenErlaubt);
    $result .= \JsOperations::getDeclarationBoolean('KuferTools.anwVonAnderenDozentenBearbeitbar', $this->useCaseResponse->anwVonAnderenDozentenBearbeitbar);
    $result .= \JsOperations::getDeclarationJsonJustStrings('KuferTools.BearbeiterKuferTools', $this->useCaseResponse->bearbeiterKuferTools);
    $result .= \JsOperations::getDeclarationBoolean('KuferTools.AnwesenheitenAusKuferSQLNichtBearbeitbar', $this->useCaseResponse->anwesenheitenAusKuferSQLNichtBearbeitbar);

    return $result;
  }

  private function getMessageStringSuccess()
  {
    if (isset($this->useCaseResponse->statusMessageContainer) && !is_null($this->useCaseResponse->statusMessageContainer)) {
      return $this->useCaseResponse->statusMessageContainer->getStatusMessage(StatusMessage::MSG_BOX_SUCCESS);
    } else {
      return '';
    }
  }

  private function getMessageStringWarning()
  {
    if (isset($this->useCaseResponse->statusMessageContainer) && !is_null($this->useCaseResponse->statusMessageContainer)) {
      return $this->useCaseResponse->statusMessageContainer->getStatusMessage(StatusMessage::MSG_BOX_WARNING);
    } else {
      return '';
    }
  }

  private function getMessageStringError()
  {
    if (isset($this->useCaseResponse->statusMessageContainer) && !is_null($this->useCaseResponse->statusMessageContainer)) {
      return $this->useCaseResponse->statusMessageContainer->getStatusMessage(StatusMessage::MSG_BOX_ERROR);
    } else {
      return '';
    }
  }

  private function getMessageStringInfo()
  {
    if (isset($this->useCaseResponse->statusMessageContainer) && !is_null($this->useCaseResponse->statusMessageContainer)) {
      return $this->useCaseResponse->statusMessageContainer->getStatusMessage(StatusMessage::MSG_BOX_INFO);
    } else {
      return '';
    }
  }
}