<?php

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

namespace Kufer\Web\Classes\Api\SearchRestriction;


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

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

  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_dat = $parts[0];
      $pToObject->beginn_dat = $parts[1];
    }
  }

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

    return \DateTimeOperations::isValidDate($parts[0]) && \DateTimeOperations::isValidDate($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_katbeginndatintervallfilter_select:%%sys_datum%%-%%sys_datum_monatsende%%;diesen Monat;%%sys_datum_naechstmonatanfang%%-%%sys_datum_naechstmonatende%%;nächsten Monat;%%sys_datum_2monatanfang%%-%%sys_datum_2monatende%%;übernächsten Monat:_:Kursbeginn%}';
    }
    $filterValue = $this->getFilterValue();
    $exp = new \ExpBasis();
    for ($i = 0; $i < $anzahlZeitenTeile; $i += 2) {
      $currentVal = $zeiten[$i];
      $currentVal = str_replace(array('%%sys_', '%%'), array('{%sys_', '%}'), $currentVal);
      $currentVal = $exp->renderTemplate($currentVal, array(new \Datamodul()));
      if ($this->isFormatValid($currentVal)) {
        $selected = $currentVal == $filterValue;
        $options[] = '<option value="' . $currentVal . '"' . ($selected ? ' ' . \HTMLOperations::getEmptyAttribute('selected') : '')
                . '>' . \CleanOperations::cleanStringForHtml($zeiten[$i + 1]) . '</option>';
      }
    }
    $onChange = (!$nojs ? ' onchange="this.form.submit();"' : '');

    return \HTMLOperations::CreateSelectTag(
      $options,
      $this->requestName,
      'kw_' . $this->requestName . 'class',
      'kw_' . $this->requestName . 'id',
      '',
      $onChange
    );
  }

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

  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::isValidDate($parts[0]) && \DateTimeOperations::isValidDate($parts[1]);
  }
}