<?php

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

namespace Kufer\Web\Classes\Api\SearchRestriction;

class SearchRestrictionKatKursauswahlFilter extends SearchRestriction
{
  private $lfdnr;
  private $freifeld;

  public function __construct($lfdnr = 1)
  {
    
    
    $this->lfdnr = $lfdnr == 10 ? 0 : $lfdnr;
    $this->sessionName = $this->requestName = 'katkursauswahl' . $this->lfdnr . 'filter';
    $felder = \DBWorkFreiWaehlbareEingabefelder4Kurs::getDefaultInstance()->getFreiWaehlbareFelder();
    if ($this->lfdnr == 0) {
      $this->freifeld = $felder[9];
    } else {
      $this->freifeld = $felder[$this->lfdnr - 1];
    }
  }

  public function applyRestriction($pFromObject, $pToObject)
  {
    if ($pFromObject == null || $pToObject == null || !($pFromObject instanceof \Kurs) || !($pToObject instanceof \Kurs)) {
      throw new \InvalidArgumentException('pFromObject oder pToObject ist kein Kurs');
    }
    $filterKriterium = \RequestOperations::getVarValue($this->requestName, '');
    if ($filterKriterium !== '') {
      if ($this->isReset($filterKriterium)) {
        $this->reset();
      } else {
        \SessionOperations::setVarValueFilter($this->sessionName, $this->cleanFilterKriterium($filterKriterium));
      }
    }
    $krit = \SessionOperations::getVarValueFilter($this->sessionName);
    if ($krit !== '') {
      $pFromObject->{'kursauswahl' . $this->lfdnr} = $krit;
    }
  }

  protected function isReset($pString)
  {
    if (is_array($pString)) {
      return count($pString) === 1 && $this->isReset($pString[0]);
    }

    return $pString === '__reset__';
  }

  private function cleanFilterKriterium($pFilterKriterium)
  {
    $set = $this->getSet();
    if (count($set) > 0) {
      $possibleValues = \ArrayOperations::mapAttribut($set, 'text');

      if (is_array($pFilterKriterium)) {
        if (count($pFilterKriterium) === 1 && $this->isReset($pFilterKriterium[0])) {
          $filterKriterium = '';
        } else {
          $filterKriterium = array();
          foreach ($pFilterKriterium as $item) {
            if (!$this->isReset($item) && in_array($item, $possibleValues)) {
              $filterKriterium[] = $item;
            }
          }
        }
      } else {
        if (in_array($pFilterKriterium, $possibleValues)) {
          $filterKriterium = $pFilterKriterium;
        } else {
          $filterKriterium = '';
        }
      }
    } else {
      $filterKriterium = $pFilterKriterium;
    }

    return $filterKriterium;
  }

  public function getFilterValue()
  {
    $this->applyRestriction(new \Kurs(), new \Kurs());

    return \SessionOperations::getVarValueFilter($this->sessionName);
  }

  public function getSet()
  {
    if ($this->freifeld->typ === 'C') {
      $yesOption = new \Freifeld();
      $yesOption->text = 'W';

      return array($yesOption);
    }

    return $this->freifeld->hinterlegung;
  }

  
  public function isCheckbox()
  {
    return $this->freifeld->typ === 'C';
  }
}

class SearchRestrictionKatKursauswahlRendererSelect extends SearchRestrictionRendererSelectBase
{
  private $varParam = '';

  public function __construct($pFilter = null)
  {
    if (is_numeric($pFilter)) {
      $pFilter = new SearchRestrictionKatKursauswahlFilter($pFilter);
    } elseif (! $pFilter instanceof SearchRestrictionKatVeranartFilter) {
      throw new \InvalidArgumentException('$pFilter is not a SearchRestrictionKatVeranartFilter');
    }
    parent::__construct($pFilter);
  }

  protected function getSet($pVarName = '', $pVarParam = '')
  {
    $this->varParam = $pVarParam;

    return $this->filter->getSet();
  }

  protected function getCleanOptionValFromItem($pItem)
  {
    return \CleanOperations::cleanStringForHtml($pItem->text);
  }

  protected function getCleanOptionTextFromItem($pItem)
  {
    if ($this->filter->isCheckbox()) {
      return \CleanOperations::cleanStringForHtml($this->varParam);
    }

    return \CleanOperations::cleanStringForHtml($pItem->text);
  }
}

class SearchRestrictionKatKursauswahlRendererCheckbox extends SearchRestrictionRendererCheckboxBase
{
  private $varParam = '';

  public function __construct($pFilter = null)
  {
    if (is_numeric($pFilter)) {
      $pFilter = new SearchRestrictionKatKursauswahlFilter($pFilter);
    } elseif (! $pFilter instanceof SearchRestrictionKatVeranartFilter) {
      throw new \InvalidArgumentException('$pFilter is not a SearchRestrictionKatVeranartFilter');
    }
    parent::__construct($pFilter);
  }

  protected function getSet($pVarName = '', $pVarParam = '')
  {
    $this->varParam = $pVarParam;

    return $this->filter->getSet();
  }

  protected function getCleanOptionValFromItem($pItem)
  {
    return \CleanOperations::cleanStringForHtml($pItem->text);
  }

  protected function getCleanOptionTextFromItem($pItem)
  {
    if ($this->filter->isCheckbox()) {
      return \CleanOperations::cleanStringForHtml($this->varParamZwei != '' ? $this->varParamZwei : $this->varParam);
    }

    return \CleanOperations::cleanStringForHtml($pItem->text);
  }
}

class SearchRestrictionKatKursauswahlRendererCheckboxAsList extends \Kufer\Web\Classes\Api\SearchRestriction\SearchRestrictionKatKursauswahlRendererCheckbox
{
  public function __construct($pFilter = null)
  {
    parent::__construct($pFilter);
    $this->itemDecorator = new ItemDecoratorList();
    $this->decorator = new DecoratorList('kw-katkursauswahllist');
  }
}