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

namespace Kufer\KuferTools\Basis\PdfListen\TnListen\PrintTnListen;


abstract class PrintTnListenBySettingsController extends PrintTnListenController
{
  abstract protected function getToolId();

  protected function getRequestObj()
  {
    $anzahlFusszeilen = 5; 
    $requestObj = new PrintTnListenBySettingsRequest();
    $requestObj->id = \RequestOperations::getVarValue('nr'); 
    $requestObj->listName = \RequestOperations::getVarValue('liste', ''); 
    
    $requestObj->dnr = \RequestOperations::getVarValue('dnr', ''); 
    
    $requestObj->ktVon = \PostOperations::getVarValue('kt_von');
    $requestObj->ktBis = \PostOperations::getVarValue('kt_bis');
    $requestObj->nurKurstageFilter = \PostOperations::getVarValue('nurkurstagefilter');
    
    $requestObj->postSettings = $this->_getPostSettings($anzahlFusszeilen, $requestObj->listName, $requestObj->id, $requestObj->nurKurstageFilter);

    return $requestObj;
  }

  protected function getPresenter($pUseCaseResponse)
  {
    return new PrintTnListenPresenter($pUseCaseResponse);
  }

  
  private function _getPostSettings($pAnzahlFusszeilen, $pListname, $pKnr, bool $nurKurstageFilter = false): ?\webtools\lists\TNSettings
  {
    
    $listObj = \TNListeFactory::create($pListname, $pKnr, $this->getToolId());

    $obj = $listObj->settings;

    if (!$nurKurstageFilter) {
      
      
      if (\PostOperations::isDefined('format')) {
        
        $obj->druckbereich->hochformat = \PostOperations::getVarValue('format') == 'H';
      }

      $obj->druckbereich->auffuellen = \PostOperations::isDefined('auffuellen');
      $obj->druckbereich->auffuellArt = \PostOperations::getVarValue('auffuellart', 'S');
      $obj->druckbereich->namegross = \PostOperations::isDefined('namegross');
      
      $obj->caption = '';
      if (\PostOperations::isDefined('ueberschrift')) {
        $obj->caption = \PostOperations::getVarValue('ueberschrift');
      }

      
      $obj->umfang = new \webtools\lists\Umfang();
      if (\PostOperations::isDefined('umfang')) {
        $obj->umfang->filter = \PostOperations::getVarValue('umfang');
      }
      if (\PostOperations::isDefined('customStatus')) {
        $obj->umfang->customStatus = \PostOperations::getVarValue('customStatus');
      }
      
      $obj->fusszeilen = array();
      for ($i = 1; $i <= $pAnzahlFusszeilen; $i++) {
        $fusszeile = new \webtools\lists\Fusszeile();
        $fusszeile->text = \PostOperations::getVarValue("textfusszeile$i");
        $fusszeile->size = \PostOperations::getVarValue("sizefusszeile$i");
        $fusszeile->bold = \PostOperations::isDefined("boldfusszeile$i");
        $obj->fusszeilen[$i - 1] = $fusszeile;
      }

      $obj->antwortboxen = array();
      for ($i = 1; $i <= 5; $i++) {
        $obj->antwortboxen[] = \PostOperations::getVarValue('antwortbox' . $i, '');
      }

      $obj->textObenRechts = \PostOperations::getVarValue('textObenRechts', '');
      $obj->textUnten = \PostOperations::getVarValue('textUnten', '');

      
      $obj->tnDarstellung = new \webtools\lists\TNDarstellung();
      $vars = get_object_vars($listObj->settings->tnDarstellung);
      foreach ($vars as $name => $wert) {
        if ($listObj->getTNDarstellungEnabled($name)) {
          $tnname = 'tn_' . $name;
          $obj->tnDarstellung->{$tnname} = \PostOperations::isDefined($tnname);
        }
      }
      
      $obj->spaltenanordnung = new \webtools\lists\Spaltenanordnung();
      $vars = get_object_vars($listObj->settings->spaltenanordnung);
      foreach ($vars as $name => $wert) {
        if (!\StringOperations::endsWith($name, '_text') && $listObj->getSpaltenAnordnungEnabled($name)) {
          $spname = 'sp_' . $name;
          $obj->spaltenanordnung->{$spname} = \PostOperations::isDefined($spname);
          $obj->spaltenanordnung->{$spname . '_text'} = \PostOperations::getVarValue($spname . '_text');
        }
      }
      
      $obj->kopfbereich = new \webtools\lists\Kopfbereich();
      $vars = get_object_vars($listObj->settings->kopfbereich);
      foreach ($vars as $name => $wert) {
        $kbname = 'kb_' . $name;
        $obj->kopfbereich->{$kbname} = \PostOperations::isDefined($kbname);
      }
      
      $obj->bemerkung = '';
      if (\PostOperations::isDefined('bemerkung')) {
        $obj->bemerkung = \PostOperations::getVarValue('bemerkung');
      }
      
      $obj->fusszeile = \PostOperations ::getVarValue('fusszeile', LISTEN_FUSSZEILE_OHNE);
      
      $obj->sortierung = \PostOperations::getVarValue('sortierung');
    } else {
      return null;
    }

    
    
    return $obj;
  }

  
}