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

namespace Kufer\KuferTools\Utils\DatamodulHelper\KursTageHelper;

class KurstageZuKurs extends KursTageHelper
{
  protected $request;
  protected $kurs;

  public function __construct($toolSettings, $user, $request, $kurs)
  {
    parent::__construct($toolSettings, $user);
    $this->request = $request;
    $this->kurs = $kurs;
  }

  protected function getKurstageIds($dbwKurseTage)
  {
    return $dbwKurseTage->getOnlyIds(array($dbwKurseTage, 'getKurstagebyKursNR'), array($this->kurs->id, -1, false, false));
  }

  protected function setzeEinstellungen($dbwKurseTage)
  {
    if (isset($this->toolSettings->versteckeKurstageWieImRaumanzeigeprogramm)
      && !empty($this->toolSettings->versteckeKurstageWieImRaumanzeigeprogramm)) {
      $dbwKurseTage->fromobject->ranzprognoanz = true;
    }

    return $dbwKurseTage;
  }

  protected function setzeFilter($dbwKurseTage, $pToolId)
  {
    if ($this->request->termineDatumBereich === 'zwischen') {
      if ($this->request->termineDatumVon !== '') {
        $dbwKurseTage->fromobject->datum = $this->request->termineDatumVon;
      }
      if ($this->request->termineDatumBis !== '') {
        $dbwKurseTage->toobject->datum = $this->request->termineDatumBis;
      }
    }
    
    if ($this->request->termineUhrzeitBereich === 'zwischen') {
      if ($this->request->termineUhrzeitVon !== '') {
        $dbwKurseTage->fromobject->zeit_beg = $this->request->termineUhrzeitVon;
      }
      if ($this->request->termineUhrzeitBis !== '') {
        $dbwKurseTage->toobject->zeit_ende = $this->request->termineUhrzeitBis;
      }
    }
    
    if ($this->request->termineDozent !== '') {
      $dbwKurseTage->fromobject->doz_namevorname = $this->request->termineDozent;
    }
    
    $tool0Settings = \ToolSettingsAPI::getToolSettings(0);
    $wtUser = \SessionOperations::getCurrentWebToolsUser();
    if ($this->request->termineDozentRadio === 'eigene' || ($tool0Settings->kurstageNurVonEingeloggtemDozent == true && $wtUser->dnr > 0)) {
      $dnr = $this->user->dnr;
      $dbwKurseTage->fromobject->dnr = $dnr;
      $dbwKurseTage->toobject->dnr = $dnr;
    }
    
    if ($this->request->termineOrt !== '') {
      $dbwKurseTage->fromobject->ort = $this->request->termineOrt;
    }
    
    if ($this->request->termineBemerkung !== '') {
      $dbwKurseTage->fromobject->text = $this->request->termineBemerkung;
    }
    $dbwKurseTage->fromobject->ausfall = $this->getKtStatus4Filter();

    return $dbwKurseTage;
  }

  private function getKtStatus4Filter()
  {
    if ($this->request->termineStatus === 'nurAusfall') {
      return $this->getKtStatusNichtAusgefallen();
    } elseif ($this->request->termineStatus === 'ohneAusfall') {
      return $this->getKtStatusAusgefallen();
    } else {
      return $this->getKtStatusErlaubt();
    }
  }

  private function getKtStatusErlaubt()
  {
    $ktStatus = $this->toolSettings->ktstatus;
    if (trim($ktStatus) === '') {
      $alleStatus = \ConstantOperations::getKurstagStatus4EinschraenkungAufKTStatus();
      $kuerzel = array_keys($alleStatus);
      $ktStatus = implode('', $kuerzel);
    }

    return $ktStatus;
  }

  private function getKtStatusNichtAusgefallen()
  {
    $erlaubteStatusAry = \StringOperations::str_split($this->getKtStatusErlaubt());
    $anzuzeigendeStatus = array_intersect($erlaubteStatusAry, array('X', 'A'));
    if (count($anzuzeigendeStatus) > 0) {
      return implode('', $anzuzeigendeStatus);
    } else {
      return '#'; 
    }
  }

  private function getKtStatusAusgefallen()
  {
    $erlaubteStatusAry = \StringOperations::str_split($this->getKtStatusErlaubt());
    $anzuzeigendeStatus = array_diff($erlaubteStatusAry, array('X', 'A'));
    if (count($anzuzeigendeStatus) > 0) {
      return implode('', $anzuzeigendeStatus);
    } else {
      return '#'; 
    }
  }
}