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

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

class BelegteKurseFilterUseCase extends \Kufer\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\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()
  {
    $dbwBelegung = new \DBWorkBelegung(1, true);
    $dbwBelegung->newSearch();
    
    
    $actualDate = \DateTimeOperations::getDateNow();
    $dbwHlSem = new \DBWorkHLSemester();
    $semester = $dbwHlSem->getSemesterByDate($actualDate);

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

    return array($dbwBelegung->toobject->kurs->ende_dat, $dbwBelegung->toobject->kurs->beginn_dat);
  }

  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();
    foreach ($columns as $column) {
      if ($column->datamodul === $pDatamodulName && $column->fieldname === $pFieldName) {
        return true;
      }
    }

    return false;
  }

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

    return $columns;
  }
}

class BelegteKurseFilterRequest
{
  public $tnr;
}

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