<?php

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

namespace Kufer\Web\Classes\Api\SearchRestriction;

class SearchRestrictionKatWotageFilter extends SearchRestriction
{
  public function __construct()
  {
    $this->requestName = 'katwotagefilter';
    $this->sessionName = 'katwotagefilter';
  }

  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');
    }

    $katWotageFilter = \RequestOperations::getVarValue($this->requestName);
    if ($katWotageFilter !== '') {
      if (is_array($katWotageFilter)) {
        if ($katWotageFilter[0] === '__reset__') {
          $this->reset();
        } else {
          
          $wotagAry = array(1, 2, 3, 4, 5, 6, 7);
          foreach ($katWotageFilter as $wotag) {
            if (is_numeric($wotag) && 1 <= $wotag && $wotag <= 7) {
              unset($wotagAry[$wotag - 1]);
            }
          }
          \SessionOperations::setVarValueFilter($this->sessionName, $wotagAry);
        }
      } else {
        
        $this->reset();
      }
    }
    $inSession = \SessionOperations::getVarValueFilter($this->sessionName);
    if (is_array($inSession)) {
      $pFromObject->wochentage_suche = implode(',', $inSession);
    }
  }

  public function getFilterValue()
  {
    $fromKurs = new \Kurs();
    $this->applyRestriction($fromKurs, new \Kurs());
    if (isset($fromKurs->wochentage_suche)) {
      return $fromKurs->wochentage_suche;
    } else {
      return '';
    }
  }

  public function getSet()
  {
    return $this->getSetShort();
  }

  public function getSetLong()
  {
    $arr = \LanguageOperations::getNameofDays();

    return $this->toItemSet($arr);
  }

  public function getSetShort()
  {
    $arr = \LanguageOperations::getNameofDaysShort();

    return $this->toItemSet($arr);
  }

  private function toItemSet($pSet)
  {
    $result = array();
    foreach ($pSet as $index => $item) {
      $result[] = $index . ' ' . $item;
    }

    return $result;
  }
}

class SearchRestrictionKatWotageRendererSelect extends SearchRestrictionRendererSelectBase
{
  public function __construct($pFilter = null)
  {
    parent::__construct(new SearchRestrictionKatWotageFilter());
  }

  public function render($pVarName, $pVarParam = '', $pVarParamZwei = '')
  {
    if (\StringOperations::strpos($pVarName, '_lang') !== false) {
      $bezeichnungen = \LanguageOperations::getNameofDays();
    } else {
      $bezeichnungen = \LanguageOperations::getNameofDaysShort();
    }
    $string = new \StringObj($pVarName);
    $nojs = $string->endsWith('_nojs');
    $options = array();
    if ($pVarParam != '') {
      $options[] = '<option value="__reset__">' . \CleanOperations::cleanStringForHtml($pVarParam) . '</option>';
    } else {
      $options[] = '<option value="__reset__">- keine Auswahl -</option>';
    }

    $filterInversInSession = $this->getFilterValue();
    if (\StringOperations::strpos($filterInversInSession, ',') === false) {
      $nothingSelected = true;
    } else {
      $nothingSelected = false;
    }
    $filterInvers = explode(',', $filterInversInSession);

    foreach ($bezeichnungen as $key => $bezeichnung) {
      $selected = !in_array($key, $filterInvers) && !$nothingSelected;
      $options[] = '<option value="' . $key . '"' . ($selected ? ' ' . \HTMLOperations::getEmptyAttribute('selected') : '')
              . '>' . $bezeichnung . '</option>';
    }
    $onChange = (!$nojs ? ' onchange="this.form.submit();"' : '');

    return \HTMLOperations::CreateSelectTag(
      $options,
      'katwotagefilter[]',
      'kw_katwotagefilterclass',
      'kw_katwotagefilterid',
      '',
      $onChange
    );
  }
}

class SearchRestrictionKatWotageRendererCb extends SearchRestrictionRendererCheckboxBase
{
  public function __construct($pFilter = null)
  {
    parent::__construct(new SearchRestrictionKatWotageFilter());
  }

  protected function getCleanOptionValFromItem($pItem)
  {
    return \StringOperations::substr($pItem, 0, 1);
  }

  protected function getCleanOptionTextFromItem($pItem)
  {
    return \CleanOperations::cleanStringForHtml(\StringOperations::substr($pItem, 2));
  }

  public function getFilterValue()
  {
    $filterInvers = $this->filter->getFilterValue();

    return explode(',', $filterInvers);
  }

  protected function isSelected($itemVal, $filterVal)
  {
    
    return !parent::isSelected($itemVal, $filterVal);
  }

  protected function getSet($pVarName = '', $pVarParam = '')
  {
    if (\StringOperations::strpos($pVarName, '_lang') !== false) {
      return $this->filter->getSetLong();
    } else {
      return $this->filter->getSet();
    }
  }
}

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