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


class SaveToolTnListSettings extends SaveToolSettings
{
  
  private $TNListSettingsDb;

  
  private $list;

  
  private $listId;

  
  private $toolId;

  
  private $toolSettings;

  
  private $druckTnListeSettingsAry;

  
  public function __construct($pTNListSettingsDb, $pList, $pListId, $pToolId)
  {
    $this->TNListSettingsDb = $pTNListSettingsDb;
    $this->list = $pList;
    $this->listId = $pListId;
    $this->toolId = $pToolId;
    $this->toolSettings = ToolSettingsAPI::getToolSettings($this->toolId);
    $this->druckTnListeSettingsAry = SerializeOperations::unserialize($this->toolSettings->druckTnListeSettings);
    if (!is_array($this->druckTnListeSettingsAry)) {
      $this->druckTnListeSettingsAry = array();
    }
  }

  protected function fillToolSettingIfNecessary($pSetting)
  {
    
    
  }

  public function saveSettings($pSettings)
  {
    foreach ($pSettings as $setting) {
      $this->setSetting($setting);
    }

    $settingNames = array_map(\FunctionOperations::getAttribute('name'), $pSettings);
    if (!in_array('padding', $settingNames) && in_array('auffuellen', $settingNames)) {
      $setting = $this->getSettings($this->listId, $this->toolId, $this->list);
      if (!$setting) {
        $setting = new TNListSettings();
      }

      $setting->padding = \PostOperations::isDefined('auffuellen') ? 'M' : ' ';
      $this->TNListSettingsDb->setListSettings($setting);
    }

    $this->saveSettingsToFile();
  }

  protected function saveSettingsToFile()
  {
    $this->TNListSettingsDb->saveListSettings($this->listId, $this->toolId);

    
    $this->toolSettings->druckTnListeSettings = SerializeOperations::serialize4Settings($this->druckTnListeSettingsAry);
    ToolSettingsAPI::setToolSettings($this->toolSettings);
  }

  protected function setSetting($pSetting)
  {
    if ($pSetting->name !== 'druckTnListeSettings') {
      $toolSettings = $this->getSettings($this->listId, $pSetting->toolId, $this->list);
      if (!$toolSettings) {
        $toolSettings = new TNListSettings();
      }

      $varName = $pSetting->name;
      $bitValuedNamen = array('teilnehmer' => 1, 'columns' => 2, 'header' => 3);

      if ($varName === 'auffuellen') {
        return;
      }

      if (is_array($pSetting->name)) {
        foreach ($pSetting->name as $name) {
          if (isset($pSetting->value[$name])) {
            $toolSettings->{$name} = $pSetting->value[$name];
          }
        }
      } elseif (array_key_exists(trim($pSetting->name), $bitValuedNamen)) {
        $value = (string)$this->ValuestoBitmask($pSetting->value);
        $toolSettings->{$varName} = $value;
      } else {
        if ($varName === 'padding') {
          $auffuellen = PostOperations::isDefined('auffuellen');
          $toolSettings->{$varName} = $auffuellen ? $pSetting->value : ' ';
        } else {
          $value = $pSetting->value;
          $toolSettings->{$varName} = $value;
        }
      }
      $this->TNListSettingsDb->setListSettings($toolSettings);
    } else {
      $this->druckTnListeSettingsAry['vorKursende' . $this->listId] = PostOperations::getVarValue('vorKursende' . $this->listId);
      $this->druckTnListeSettingsAry['nachKursende' . $this->listId] = PostOperations::getVarValue('nachKursende' . $this->listId);
      $this->druckTnListeSettingsAry['vorKursbeginn' . $this->listId] = PostOperations::getVarValue('vorKursbeginn' . $this->listId);
      $this->druckTnListeSettingsAry['nachKursbeginn' . $this->listId] = PostOperations::getVarValue('nachKursbeginn' . $this->listId);
      $this->druckTnListeSettingsAry['tnListeBezeichnung' . $this->listId] = PostOperations::getVarValue('tnListeBezeichnung' . $this->listId);
      $this->druckTnListeSettingsAry['anwAnzeigen' . $this->listId] = PostOperations::isDefined('anwAnzeigen' . $this->listId);
      $this->druckTnListeSettingsAry['nurkurstagefilter' . $this->listId] = PostOperations::isDefined('nurkurstagefilter' . $this->listId);
      (new \Kufer\Web\Classes\Stream\ArrayStream(array_keys(get_object_vars($this->list->settings->spaltenanordnung))))
        ->filter(\FunctionOperations::argsWithPlaceholder(\StringOperations::class . '::endsWith', \FunctionOperations::placeholder(), '_text'))
        ->each(function(string $str): void {
          $this->druckTnListeSettingsAry[$str . $this->listId] = \PostOperations::getVarValue($str . $this->listId);
        });
    }
  }

  private function getSettings($pListId, $pToolId, $pList)
  {
    $toolSettings = $this->TNListSettingsDb->getListSettings($pListId, $pToolId);
    if ($toolSettings) {
      return $toolSettings;
    } else {
      static $settings = null;
      if (is_null($settings)) {
        $settings = $pList->settings->toTNListSettings();
      }

      return $settings;
    }
  }
}