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

namespace Kufer\Web\KuferTools\Mitarbeiter\TeilnehmerInfo\Detailsreiter\BelegteKurse\BelegteKurseFilter;

class BelegteKurseFilterUseCase extends \Kufer\Web\KuferTools\Basis\UseCase\ToolUseCase
{
  public function __construct($request)
  {
    parent::__construct($request, new BelegteKurseFilterResponse());
  }

  public static function getToolId()
  {
    return \ToolsAPI::TOOL_TEILNEHMERINFO_MA;
  }

  public static function getToolSectionId()
  {
    return 43;
  }

  public function hasAccess($pToolSectionId = null)
  {
    if (!\Kufer\Web\KuferTools\Basis\UseCase\ToolUseCase::hasAccess(411)) {
      return false;
    }
    if (!$this->isRechtAllowed('411.1.2')) {
      return false;
    }

    return true;
  }

  public function action()
  {
    return $this->getResponse();
  }

  protected function getResponse()
  {
    $this->response->tnr = $this->request->tnr;
    $this->response->showTermineDatum = $this->showDatumAllowed();
    $this->response->showTermineUhrzeit = $this->showUhrzeitAllowed();
    
    
    $this->response->showTermineDozent = $this->showDozentAllowed();
    
    
    $this->response->showTermineDozRadioButtons = $this->showDozRadioButtons();
    $this->response->showTermineOrt = $this->showOrtAllowed();
    $this->response->showTermineBemerkung = $this->showBemerkungAllowed();
    $this->response->showTermineStatus = $this->showStatusAllowed();
    list($this->response->datVon, $this->response->datBis) = $this->getDatumVonBis();

    return $this->response;
  }

  
  private function getDatumVonBis(): array
  {
    $actualDate = \DateTimeOperations::getDateNow();
    $dbwHlSem = new \DBWorkHLSemester();
    $semester = $dbwHlSem->getSemesterByDate($actualDate);

    $beginnDatum = '';
    $endeDatum = '';

    $filterFields = \SessionOperations::getVarValueWTIntern('filterBelegteKurse_tool11', null);
    if (!is_null($filterFields)) {
      if ($filterFields['datumBis'] != null) {
        $beginnDatum = $filterFields['datumBis'];
      }
      if ($filterFields['datumVon'] != null) {
        $endeDatum = $filterFields['datumVon'];
      }
    } elseif ($this->toolSettings->useZeitraumVorbelegung) {
      $beginnDatum = $this->toolSettings->zeitraumVorbelegung_ende_dat;
      $endeDatum = $this->toolSettings->zeitraumVorbelegung_beginn_dat;
    } elseif ($semester !== false) {
      $beginnDatum = $semester->bis;
      $endeDatum = $semester->von;
    }

    return array($endeDatum, $beginnDatum);
  }

  private function showDatumAllowed()
  {
    $columnsToCheck = array('datum', 'datum_kurz', 'datum_jahrkurz', 'datum_komplettkurz', 'datum_komplettkurz_jahrkurz', 'datum_komplettlang');

    return $this->showSettingAllowed($columnsToCheck);
  }

  private function showUhrzeitAllowed()
  {
    $columnsToCheck = array('zeit_beg', 'zeit_ende');

    return $this->showSettingAllowed($columnsToCheck);
  }

  
  private function showDozRadioButtons()
  {
    return $this->getToolId() === \ToolsAPI::TOOL_KURSUEBERSICHT_DOZ;
  }

  
  private function showDozentAllowed()
  {
    $columnsToCheck = array('name', 'vorname', 'name_vorname_ohne_komma', 'namevorname', 'titel_name_vorname', 'titel_vorname_name', 'vorname_name_ohne_komma');

    return $this->showSettingAllowed($columnsToCheck, 'dozent');
  }

  
  private function showOrtAllowed()
  {
    $columnsToCheck = array('ort');

    return $this->showSettingAllowed($columnsToCheck);
  }

  private function showBemerkungAllowed()
  {
    $columnsToCheck = array('bemerkungen');

    return $this->showSettingAllowed($columnsToCheck);
  }

  private function showStatusAllowed()
  {
    $columnsToCheck = array('ausfall');

    return $this->showSettingAllowed($columnsToCheck);
  }

  
  private function showSettingAllowed($columnsToCheck, $datamodul = 'kurstag')
  {
    foreach ($columnsToCheck as $columnToCheck) {
      if ($this->columnExists($datamodul, $columnToCheck)) {
        return true;
      }
    }

    return false;
  }

  
  private function columnExists($pDatamodulName, $pFieldName)
  {
    $columns = $this->getColumns($this->toolSettings);
    foreach ($columns as $column) {
      if ($column->datamodul === $pDatamodulName && $column->fieldname === $pFieldName) {
        return true;
      }
    }

    return false;
  }

  public static function getColumns(\ToolSettings $toolSettings, ?int $toolSectionId = null): array
  {
    static $columns = null;
    if (is_null($columns)) {
      $columns = parent::getColumns($toolSettings, $toolSectionId);
    }

    return $columns;
  }
}

class BelegteKurseFilterRequest
{
  public $tnr;
}

class BelegteKurseFilterResponse extends \Kufer\Web\KuferTools\Basis\UseCase\ToolResponse
{
  public $knr;
  public $tnr;
  public $datVon;
  public $datBis;
  public $showTermineDatum;
  public $showTermineUhrzeit;
  public $showTermineDozent;
  public $showTermineDozRadioButtons;
  public $showTermineOrt;
  public $showTermineBemerkung;
  public $showTermineStatus;
}