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

namespace Kufer\Web\KuferTools\Basis\Kursdetails\Detailsreiter\Termine;

abstract class TermineUseCase extends \Kufer\Web\KuferTools\Basis\UseCase\EinzelKursUseCase
{
  public function __construct($request)
  {
    parent::__construct($request, new TermineResponse());
  }

  abstract protected function getMainReiterId();

  abstract protected function getButtons();

  public function hasAccess($pToolSectionId = null)
  {
    
    if (!parent::hasAccess()) {
      return false;
    }
    if (!$this->isRechtAllowed($this->getMainReiterId() . '.1.' . $this->getSubRechteNumber())) {
      return false;
    }

    return true;
  }

  protected function getSubRechteNumber(): int
  {
    return 3;
  }

  public function action()
  {
    $kurs = $this->getKurs($this->getKursNummer());
    $termine = $this->getKurstage($kurs);
    $termine = $this->aktualisiereKurstage($termine);

    return $this->getResponse($kurs, $termine);
  }

  protected function getKursNummer()
  {
    return $this->request->knr;
  }

  protected function getDatamodulHelper($kurs): \Kufer\Web\KuferTools\Utils\DatamodulHelper\KursTage\KurstageZuKurs
  {
    return new \Kufer\Web\KuferTools\Utils\DatamodulHelper\KursTage\KurstageZuKurs($this->toolSettings, $this->getUser(), $this->request, $kurs);
  }

  protected function getKurstage($kurs)
  {
    $kthelper = $this->getDatamodulHelper($kurs);

    return $kthelper->getDatamoduleMitFilter($this->getToolId());
  }

  protected function aktualisiereKurstage($pKurstage)
  {
    return array_map(function($kurstag) {
      return $this->aktualisiereKursort($kurstag);
    }, $pKurstage);
  }

  private function aktualisiereKursort($pKurstag)
  {
    if (!empty($pKurstag->kursort->mobiltel)) {
      $pKurstag->kursort->smsgrafik = '<a class="smsgrafik" href="index.php?tool_id=' . $this->getToolId()
              . '&knr='
              . \CleanOperations::cleanStringForHtml($this->request->knr)
              . '&kurstag_id=' . $pKurstag->id . '&action=smsKursort">'
              . '<i class="fa fa-envelope-o"></i></a>';
    }

    return $pKurstag;
  }

  protected function showTermineLoeschenButton(): bool
  {
    return false;
  }

  protected function getResponse($kurs, $termine)
  {
    $this->handleResponseStrategies();
    $this->response->kurs = $kurs;
    $this->response->termine = $termine;
    $this->response->reiterButtons = $this->getButtons();

    if ($this->showTermineLoeschenButton()) {
      $this->response->termineLoeschenXsrf = $this->getXsrfKey('termineloeschen');
    }

    return $this->response;
  }

  protected function getResponseStrategies()
  {
    return array(
      new \Kufer\Web\KuferTools\Utils\ResponseStrategies\ResponseWss($this),
      new \Kufer\Web\KuferTools\Utils\ResponseStrategies\ResponseWithToolSettings($this)
    );
  }
}

class TermineRequest
{
  public $knr;
  public $termineDatumBereich;
  public $termineDatumVon;
  public $termineDatumBis;
  public $termineUhrzeitBereich;
  public $termineUhrzeitVon;
  public $termineUhrzeitBis;
  public $termineDozent;
  public $termineDozentRadio;
  public $termineOrt;
  public $termineBemerkung;
  public $termineStatus;
  public $failedToUpdateKtAry;

  public $statusCode;
  public $eintraegeGeloescht = false;
  public $pruefeObKursZuKurstagePasst = false;
  public $changedKurstageIds = array();
  public $eintraegeBearbeitet = false;
}

class TermineResponse extends \Kufer\Web\KuferTools\Basis\UseCase\ToolResponse
{
  public $kurs;
  public $termine;
  public $columns;

  public $reiterButtons;
  public $markierenButtons;
  public $hasRecht4KursAnpassen = true;
  public $nichtPassendeKursdaten;
  public $statusCode;
  public $failedToUpdateKtAry = array();
  public $eintraegeGeloescht = false;
  public $eintraegeGeaendert = false;
  public $changedKurstageIds = array();
  public $showInformiereTnDialog = false;
  public $actionInformiereTnDialog = '';
  public $termineLoeschenXsrf = '';
  public $eintraegeBearbeitet;
}