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

namespace Kufer\Web\KuferTools\Basis\Kursdetails;

class KursDetailsPresenter extends \Kufer\Web\KuferTools\Basis\Presenter\MitRahmenPresenter
{
  protected function getView()
  {
    return new KursDetailsView($this->getViewModel());
  }

  protected function getViewModel()
  {
    $viewModel = new KursDetailsViewModel();
    $viewModel->kurs = $this->useCaseResponse->kurs;
    $viewModel->kurs->nextKnr = $this->useCaseResponse->nextKnr;
    $viewModel->kurs->previousKnr = $this->useCaseResponse->previousKnr;
    $viewModel->kurs->datumLetzterKurstag = $this->useCaseResponse->datumLetzterKurstag;
    $viewModel->kurs->backLink = $this->useCaseResponse->backLink;
    $viewModel->kurs->backButtonLabel = $this->useCaseResponse->backButtonLabel;
    $viewModel->kurs->ueberschrift = $this->useCaseResponse->ueberschrift;

    
    if (isset($this->useCaseResponse->statusMessageContainer) && !is_null($this->useCaseResponse->statusMessageContainer)) {
      $statusMessageContainer = $this->useCaseResponse->statusMessageContainer;
    } else {
      $statusMessageContainer = new \Kufer\Web\KuferTools\Utils\StatusMessages\StatusMessageContainer();
    }
    $statusMessageContainer->setStatusMessages($viewModel);

    $viewModel->kurs->reiterButtons = $this->getButtonHtml();
    $viewModel->kurs->toolId = $this->useCaseResponse->toolId;
    
    $viewModel->reiterHtml = $this->getReiter();

    return $viewModel;
  }

  protected function getSeitenTitel(): string
  {
    return $this->useCaseResponse->ueberschrift ?? '';
  }

  private function getReiter()
  {
    $viewModel = new ReiterViewModel();
    $viewModel->kurs = $this->useCaseResponse->kurs;
    $reiterAry = $this->useCaseResponse->reiter;
    
    
    
    
    foreach ($reiterAry as $reiter) {
      $reiter->belegId = $reiter->belegId != '' ? '&belid=' . $reiter->belegId : '';
      $reiter->knr = $reiter->knr != '' ? '&knr=' . $reiter->knr : '';
      $reiter->rnr = $reiter->rnr != '' ? '&rnr=' . $reiter->rnr : '';
    }
    $viewModel->reiter = $reiterAry;
    $view = new ReiterView($viewModel);

    return $view->show();
  }

  
  protected function getJSDeclarations(): string
  {
    $result = parent::getJSDeclarations();
    $result .= \JsOperations::getDeclarationString('KuferTools.Knr', $this->useCaseResponse->kurs->id);
    $result .= \JsOperations::getDeclarationString('KuferTools.KursTyp', $this->useCaseResponse->kurs->typ);
    
    $result .= \JsOperations::getDeclarationString('KuferTools.OeffneReiter', $this->useCaseResponse->oeffneReiter);
    $result .= \JsOperations::getDeclarationString('KuferTools.ActionMerkeReiter', 'merkeKursDetailsReiter');

    $result .= \JsOperations::getDeclarationBoolean('KuferTools.IsBO', \DBWorkBASysSettings::isBadOeynhausen());
    $result .= \JsOperations::getDeclarationBoolean('KuferTools.IsMvhs', \DBWorkBASysSettings::isMVHS());
    $result .= 'registerNamespace("KuferTools.KursDetails");' . "\n";

    return $result;
  }

  
  protected function getJsFilesSrcAryWeitere(): array
  {
    return array(
      $this->getJsFileFromSys('collapse.js'),
      $this->getJsFileFromSys('mnlisten.js'),
      $this->getJsFileFromSys('detailsseite.js'),
      $this->getJsFileFromSys('kursdetails.js'),
      $this->getJsFileFromJQuery('galleria/galleria.min.js', \CodingOperations::getEncoding4ExternalKuferToolsJsFiles()),
      $this->getJsFileFromOldTools('pdfdruck.js'),
      $this->getJsFileFromSys('pdfdruck.js'),
      $this->getJsFileFromSys('filter.js'),
      $this->getJsFileFromSys('datatables.js'),
      $this->getJsFileFromSys('kursdozenten.js')
    );
  }

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

  protected function getButtonHtml()
  {
    $buttonleiste = new \Kufer\Web\KuferTools\Basis\Button\Buttonleiste();

    return $buttonleiste->getHtmlButtonleiste($this->useCaseResponse->reiterButtons, 'kursdetails_buttons');
  }
}