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

namespace Kufer\Web\KuferTools\Dozent\Anwesenheiten\Anwesenheitseingabe\KursAbschliessen;

class SendKursAbschliessenDialogPresenter extends \Kufer\Web\KuferTools\Basis\Presenter\Presenter
{
  public function present()
  {
    $view = $this->getView();

    if ($this->useCaseResponse->success) {
      $dialog = $view->show();
    } else {
      $dialog = $view->getNoData();
    }

    $json = new class() {
      public $success;
      public $dialog_msg;
    };

    $json->success = (bool)$this->useCaseResponse->success;
    $json->dialog_msg = \StringOperations::utf8_encode($dialog);
    $json->errors = \array_map(array(\StringOperations::class, 'utf8_encode'), $this->useCaseResponse->errors);

    return json_encode($json);
  }

  public function setRequestHeader()
  {
    header('Content-Type: application/json; charset=' . UTF_8);
  }

  protected function getViewModel()
  {
    $result = new SendKursAbschliessenDialogViewModel();
    $result->kurstag = $this->useCaseResponse->kurstag;
    $result->errors = $this->useCaseResponse->errors;
    $result->optionalCommentInput = $this->useCaseResponse->optionalCommentInput;
    $result->speichernInNotizenSuccess = $this->useCaseResponse->speichernInNotizenSuccess;

    return $result;
  }

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