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

namespace Kufer\KuferTools\Basis\Kursdetails\DetailsReiter\Termine;

abstract class TermineUseCase extends \Kufer\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\KuferTools\Utils\DatamodulHelper\KursTageHelper\KurstageZuKurs
  {
    return new \Kufer\KuferTools\Utils\DatamodulHelper\KursTageHelper\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(array($this, 'aktualisiereKursort'), $pKurstage);
  }

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

    return $pKurstag;
  }

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

    return $this->response;
  }

  protected function getResponseStrategies()
  {
    return array(
        new \Kufer\KuferTools\Utils\ResponseStrategies\ResponseWss($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;
  public $changedKurstageIds = array();
  public $eintraegeBearbeitet = false;
}

class TermineResponse extends \Kufer\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 = '';
}