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


class SaveToolSettings extends SaveWebSettings
{
  protected function setSetting($pSetting)
  {
    if ($pSetting->config->type === IT_INFOTEXT) {
      return '';
    }

    $this->fillToolSettingIfNecessary($pSetting);

    if (is_array($pSetting->name)) {
      foreach ($pSetting->name as $name) {
        $this->toolSettingsAry[$pSetting->toolId]->{$name} = $pSetting->value[$name];
      }
    } else {
      $varName = $pSetting->name;
      switch ($pSetting->name) {
        case 'erstell_datetime':  
          LibSettingTools::ToolAktuellesSetting($pSetting);

          return '';

        case 'sortnews':
          list($varSortnews, $varSortnewsAsc) = LibSettingTools::ToolAktuellesSetting($pSetting);
          $this->toolSettingsAry[$pSetting->toolId]->sortnewsasc = (string)$varSortnewsAsc;
          $value = $varSortnews;
          break;

        case 'sortktasc':
        case ($pSetting->name == 'sorttnasc' && !in_array($pSetting->toolId, array(6, 11, 53, 58))): 
        case ($pSetting->name == 'sortdozasc' && !in_array($pSetting->toolId, array(12, 55, 60))):
        case 'sortraeume':
          if ($pSetting->value == '') {
            
            return;
          }
          $value = $this->saveSortSettings($pSetting, $this->toolSettingsAry[$pSetting->toolId]);
          break;

        case 'reiterBeschriftung':
        case 'reiterBeschriftung_raum':
        case 'reiterBeschriftung_kurs':
          $value = LibSettingTools::reiterBeschriftungSetting($pSetting);
          break;

        case 'downloadFuer':
          $downloadFuer = array_keys(array_filter($pSetting->value));
          $value = SerializeOperations::serialize4Settings($downloadFuer);
          break;

        case 'buttonBezeichnungen':
          $value = LibSettingTools::saveButtonBezeichnungenSettings($pSetting);
          break;

        case 'kurskopie_kursFilterVorbelegen':
        case 'kurskopie_kursFilterKursbeginnVorbelegen':
          $isKursKopie = true;
          
        case 'kursFilterVorbelegen':
        case 'kursFilterKursbeginnVorbelegen':
          $isKursKopie = isset($isKursKopie) ? $isKursKopie : false;
          $this->toolSettingsAry[$pSetting->toolId] = $this->setFilterKursSettings($pSetting, $this->toolSettingsAry[$pSetting->toolId], $isKursKopie);
          $value = $pSetting->value;
          break;

        case 'anm_erlaubteZahlarten[]':
          $selectedZahlarten = array_map(function($key, $val) { if ($val) { return $key; }}, array_keys($pSetting->value), $pSetting->value);
          $value = JsonOperations::encodeBase64(array_values(array_filter($selectedZahlarten)));
          $varName = 'anm_erlaubteZahlarten';
          break;

        case 'useZeitraumVorbelegung':
          $value = $pSetting->value;
          if ($value) {
            $datvon = PostOperations::getVarValue('zeitraumVorbelegung_beginn_dat');
            $datbis = PostOperations::getVarValue('zeitraumVorbelegung_ende_dat');
            if (DateTimeOperations::isValidDate($datvon, true)) {
              $this->toolSettingsAry[$pSetting->toolId]->zeitraumVorbelegung_beginn_dat = $datvon;
            }
            if (DateTimeOperations::isValidDate($datbis, true)) {
              $this->toolSettingsAry[$pSetting->toolId]->zeitraumVorbelegung_ende_dat = $datbis;
            }
          }
          break;

        case 'ktstatus':
        case 'colorktstatus':
          if ($pSetting->toolId == \ToolsAPI::TOOL_RAUMANZEIGE && $this->toolSettingsAry[$pSetting->toolId]->abweichendeKtStatus != 1) {
            return;
          }
          $value = $pSetting->value;
          break;

        case 'stornoMinTNanzahlPruefen':
        case 'stornoShowAustrittFeld':
          $toolIds = array(\ToolsAPI::TOOL_KURSPLANUNG_MA, \ToolsAPI::TOOL_TEILNEHMERINFO_MA);
          
        case 'tnIgnoreEintrittDate':
        case 'tnIgnoreAustrittDate':
        case 'anwesenheitenAusKuferSQLNichtBearbeitbar':
          if (!isset($toolIds)) {
            $toolIds = array(\ToolsAPI::TOOL_ANWESENHEIT_DOZ, \ToolsAPI::TOOL_KURSUEBERSICHT_DOZ);
          }

          if (in_array($pSetting->toolId, $toolIds)) {
            $otherToolIds = ArrayOperations::filterReassignKeys($toolIds, function($pToolId) use ($pSetting) { return $pSetting->toolId !== $pToolId; });
            $this->setSettingOtherTools($otherToolIds, $pSetting);
          }
          $value = $pSetting->value;
          break;
        case 'blakSchwelle':
          if (StringOperations::strpos($pSetting->value, '-') !== false) {
            $blakSchwelleArry = explode('-', $pSetting->value);
            $this->toolSettingsAry[$pSetting->toolId]->blakUntereSchwelle = $blakSchwelleArry[0];
            $this->toolSettingsAry[$pSetting->toolId]->blakObereSchwelle = $blakSchwelleArry[1];
            $varName = '';
            $value = '';
          }
          break;
        
        default:
          $value = $pSetting->value;

      } 

      if (isset($this->toolSettingsAry[$pSetting->toolId]->{$varName})) {
        $this->toolSettingsAry[$pSetting->toolId]->{$varName} = $value;
      }
    } 
    if (is_null($this->toolSettingsAry[$pSetting->toolId]->id) || $this->toolSettingsAry[$pSetting->toolId]->id == '') {
      $this->toolSettingsAry[$pSetting->toolId]->id = $pSetting->toolId;
    }
  }

  private function saveSortSettings($pSetting, $pToolsettings)
  {
    if ($pSetting->name == 'sortdozasc') {
      if ($pSetting->value == 'nameauf') {
        $pToolsettings->sortdoz = 'name';

        return true;
      } elseif ($pSetting->value == 'nameab') {
        $pToolsettings->sortdoz = 'name';

        return false;
      }
    }

    $asc = true;

    switch ($pSetting->value) {
      
      case 'nrauf':  $pToolsettings->sortkt = 'kt.datum'; $asc = true; break;
      case 'nrab':  $pToolsettings->sortkt = 'kt.datum'; $asc = false; break;
      case 'nameauf':  $pToolsettings->sorttn = 't.name'; $asc = true; break;
      case 'nameab':  $pToolsettings->sorttn = 't.name'; $asc = false; break;
      case 'anmeldedatumauf':  $pToolsettings->sorttn = 'b.anm_datum'; $asc = true; break;
      case 'anmeldedatumab':  $pToolsettings->sorttn = 'b.anm_datum'; $asc = false; break;
      case 'idauf':  $pToolsettings->sorttn = 'b.id'; $asc = true; break;
      case 'idab':  $pToolsettings->sorttn = 'b.id'; $asc = false; break;
      case 'firmennameauf': $pToolsettings->sorttn = 'fa.name, fa.vorname, t.name, t.vorname'; $asc = true; break;
      case 'firmennameab': $pToolsettings->sorttn = 'fa.name, fa.vorname, t.name, t.vorname'; $asc = false; break;
      case 'kurstageauf':  $pToolsettings->sortkt = 'knr'; $asc = true; break;
      case 'kurstageab':  $pToolsettings->sortkt = 'knr'; $asc = false; break;
      case 'beginnauf':  $pToolsettings->sortkt = 'datum'; $asc = true; break;
      case 'beginnab':  $pToolsettings->sortkt = 'datum'; $asc = false; break;

      
      case 'raeumeauf':
        $pToolsettings->sortraeumeasc = true;

        return 'raum_nr';
      case 'raeumeab':
        $pToolsettings->sortraeumeasc = false;

        return 'raum_nr';
      case 'bezlangauf':
        $pToolsettings->sortraeumeasc = true;

        return 'mktext';
      case 'bezlangab':
        $pToolsettings->sortraeumeasc = false;

        return 'mktext';
    }

    return $asc;
  }

  protected function ValuestoBitmask($pSettings)
  {
    $bitmask = 0;
    $i = 0;
    foreach ($pSettings as $settingvalue) {
      if ($settingvalue) {
        $bitmask |= pow(2, $i);
      }
      $i++;
    }

    return $bitmask;
  }

  protected function setSettingOtherTools($pToolIds, $pSetting): void
  {
    foreach ($pToolIds as $toolId) {
      $objsettings4OtherTool = ToolSettingsAPI::getToolSettings($toolId);
      $objsettings4OtherTool->{$pSetting->name} = $pSetting->value;
      ToolSettingsAPI::setToolSettings($objsettings4OtherTool);
    }
  }
}