<?php

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

namespace Kufer\Web\Classes\Api\SearchRestriction;


class SearchRestrictionKatZeitraumFilter extends SearchRestriction implements SearchRestrictionRenderer
{
  private static $separator = '-';

  public function __construct()
  {
    $this->sessionName = $this->requestName = 'katzeitraumfilter';
  }

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

    $filterFromRequest = \RequestOperations::getVarValue($this->requestName);
    if ($filterFromRequest !== '') {
      if ($this->isFormatValid($filterFromRequest)) {
        \SessionOperations::setVarValueFilter($this->sessionName, $filterFromRequest);
      }
      if ($filterFromRequest === '__reset__') {
        $this->reset();
      }
    }
    $inSession = \SessionOperations::getVarValueFilter($this->sessionName);
    if ($inSession !== '') {
      $parts = $this->splitParts($inSession);
      $pFromObject->beginn = $parts[0];
      $pToObject->beginn = $parts[1];
    }
  }

  
  public static function isFormatValid($pToCheck)
  {
    if (\StringOperations::strpos($pToCheck, static::$separator) === false) {
      return false;
    }
    $parts = static::splitParts($pToCheck);
    if (count($parts) < 2) {
      return false;
    }

    return \DateTimeOperations::isValidTimeString($parts[0]) && \DateTimeOperations::isValidTimeString($parts[1]);
  }

  public static function splitParts($toSplit)
  {
    return explode(static::$separator, $toSplit);
  }

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

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

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

    $zeiten = $this->explodeZeiten($pVarParam);
    $anzahlZeitenTeile = count($zeiten);
    if ($anzahlZeitenTeile % 2 != 0) {
      return 'Bitte prüfen Sie den Parameter! Beispiel: {%sys_katzeitraumfilter_select:00:00-11:59;vormittags;12:00-17:00;nachmittags;17:01-23:59;abends:_:Zeitraum%}';
    }
    $filterValue = $this->getFilterValue();
    for ($i = 0; $i < $anzahlZeitenTeile; $i += 2) {
      if ($this->isFormatValid($zeiten[$i])) {
        $selected = $zeiten[$i] == $filterValue;
        $options[] = '<option value="' . $zeiten[$i] . '"' . ($selected ? ' ' . \HTMLOperations::getEmptyAttribute('selected') : '')
          . '>' . \CleanOperations::cleanStringForHtml($zeiten[$i + 1]) . '</option>';
      }
    }
    $onChange = (!$nojs ? ' onchange="this.form.submit();"' : '');

    return \HTMLOperations::CreateSelectTag($options, 'katzeitraumfilter', 'kw_katzeitraumfilterclass', 'kw_katzeitraumfilterid', '', $onChange);
  }

  public function explodeZeiten($pVarParam)
  {
    return explode(';', $pVarParam);
  }
}

class SearchRestrictionKatZeitraumRendererRadioButtons extends SearchRestrictionRendererRadiobuttonBase
{
  protected static $mySeparator = '|';

  public function __construct($pFilter = null)
  {
    parent::__construct(new SearchRestrictionKatZeitraumFilter());
  }

  protected function getSet($pVarName = '', $pVarParam = '')
  {
    $zeiten = $this->filter->explodeZeiten($pVarParam);
    $anzahlZeitenTeile = count($zeiten);
    if ($anzahlZeitenTeile % 2 != 0) {
      \Logger::logg('Falsches Format beim Parameter des Zeitraumfilters: ' . $pVarParam, 15);

      return 'Bitte prüfen Sie den Parameter! Beispiel: {%sys_katzeitraumfilter_rb:00:00-11:59;vormittags;12:00-17:00;nachmittags;17:01-23:59;abends:_:Zeitraum%}';
    }
    $result = array();
    for ($i = 0; $i < $anzahlZeitenTeile; $i += 2) {
      if ($this->filter->isFormatValid($zeiten[$i])) {
        $result[] = $zeiten[$i] . static::$mySeparator . $zeiten[$i + 1];
      }
    }

    return $result;
  }

  protected function getCleanOptionValFromItem($pItem)
  {
    $parts = explode(static::$mySeparator, $pItem);

    return \CleanOperations::cleanStringForHtml($parts[0]);
  }

  protected function getCleanOptionTextFromItem($pItem)
  {
    $parts = explode(static::$mySeparator, $pItem);

    return \CleanOperations::cleanStringForHtml($parts[1]);
  }

  protected function getLabelClass($requestName)
  {
    if (\DBWorkBASysSettings::isUlmVh()) {
      return $requestName . 'class chckbxs__lbl';
    } else {
      return parent::getLabelClass($requestName);
    }
  }

  protected function arrangeLabelAndInput($openingLabelTag, $itemText, $radio)
  {
    if (\DBWorkBASysSettings::isUlmVh()) {
      return $openingLabelTag . $radio . '<span></span>' . $itemText . \HTMLOperations::CreateClosingTag('label');
    } else {
      return parent::arrangeLabelAndInput($openingLabelTag, $itemText, $radio);
    }
  }
}

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