<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */

namespace Kufer\KuferTools\Basis\Kursdetails;

class KursDetailsPresenter extends \Kufer\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\KuferTools\Utils\StatusMessages\StatusMessageContainer();
    }
    $statusMessageContainer->setStatusMessages($viewModel);

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

    return $viewModel;
  }

  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()
  {
    $result = parent::getJSDeclarations();
    $result .= \JsOperations::getDeclarationString('KuferTools.Knr', $this->useCaseResponse->kurs->id);
    
    $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()
  {
    return array($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 getButtonHtml()
  {
    $buttonleiste = new \Kufer\KuferTools\Basis\Button\Buttonleiste();

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