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

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

abstract class ShowFilterTermineUseCase extends \Kufer\Web\KuferTools\Basis\UseCase\ToolUseCase
{
  public function __construct($request, $response = null)
  {
    parent::__construct($request, \is_null($response) ? new ShowFilterTermineResponse() : $response);
  }

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

  protected function getResponse()
  {
    $this->response->knr = $this->request->knr;
    $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();
    $this->response->termineDozentEigeneChecked = isset($this->toolSettings->eigeneTermineDozent) && $this->toolSettings->eigeneTermineDozent;
    $this->response->termineDozentChecked = isset($this->toolSettings->eigeneTermineDozent) && !$this->toolSettings->eigeneTermineDozent;

    return $this->response;
  }

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

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

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

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

  
  private function showDozRadioButtons()
  {
    if ($this->showKurstageNurVonEingeloggtemDozent()) {
      return false;
    }

    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 showKurstageNurVonEingeloggtemDozent()
  {
    $tool0Settings = \ToolSettingsAPI::getToolSettings(0);
    $wtUser = \SessionOperations::getCurrentWebToolsUser();

    return $tool0Settings->kurstageNurVonEingeloggtemDozent && $wtUser->dnr > 0;
  }

  
  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);
  }

  
  protected 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 ShowFilterTermineRequest
{
  public $knr;
}

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