<?php

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

namespace Kufer\Web\Classes\Api\SearchRestriction;


class SearchRestrictionKatUmkreisFilter extends SearchRestriction
{
  public $sessionNameRadius;
  public $requestNameRadius;

  const DEFAULTRADIUS = 10;

  public function __construct()
  {
    $this->sessionName = $this->requestName = 'katumkreisfilterplz';
    $this->sessionNameRadius = $this->requestNameRadius = 'katumkreisfilterradius';
  }

  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 !== '' && $this->isValid($filterKriterium)) {
      \SessionOperations::setVarValueFilter($this->sessionName, $this->cleanFilterKriterium($filterKriterium));
      $radius = \RequestOperations::getVarValue($this->requestNameRadius, self::DEFAULTRADIUS);
      if (is_numeric($radius)) {
        \SessionOperations::setVarValueFilter($this->sessionNameRadius, \FilterBasis::getOnlyNumeric($radius, '.,'));
      }
    } else {
      if (\RequestOperations::isDefined($this->requestName)) { 
        
        $this->reset();
      }
    }
    $f = \SessionOperations::getVarValueFilter($this->sessionName);
    if ($f !== '') {
      $pFromObject->suche_ortUmkreisPlz = $f;
      $pFromObject->suche_ortUmkreisRadius = \SessionOperations::getVarValueFilter($this->sessionNameRadius);
    }
  }

  private function isValid($pFilterKriterium)
  {
    return \StringOperations::strlen(\FilterBasis::getOnlyNumeric($pFilterKriterium)) > 0;
  }

  private function cleanFilterKriterium($pFilterKriterium)
  {
    $filterKriterium = '';
    if (!is_array($pFilterKriterium)) {
      $filterKriterium = \FilterBasis::getOnlyNumeric($pFilterKriterium);
    }

    return $filterKriterium;
  }

  public function reset()
  {
    parent::reset();
    \SessionOperations::setVarValueFilter($this->sessionNameRadius, self::DEFAULTRADIUS);
  }

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

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

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

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

class SearchRestrictionKatUmkreisRenderer extends SearchRestrictionRendererTextBase
{
  public function __construct($pFilter = null)
  {
    parent::__construct(new SearchRestrictionKatUmkreisFilter());
  }

  public function render($pVarName, $pVarParam = '', $pVarParamZwei = '')
  {
    $plzText = 'PLZ';
    $for = 'for="' . $this->filter->getRequestName() . '" ';
    $labelPlz = \HTMLOperations::CreateTag('label', $this->filter->getRequestName() . 'class', '', true, $for) . $plzText . \HTMLOperations::CreateClosingTag('label');
    $renderer = new \Kufer\Web\Classes\Api\SearchRestriction\SearchRestrictionKatUmkreisPlzRenderer();
    $result = $labelPlz . ' ' . $renderer->render($pVarName, $pVarParam, $pVarParamZwei);

    $radiusText = 'Radius';
    $for = 'for="' . $this->filter->requestNameRadius . '" ';
    $labelRadius = \HTMLOperations::CreateTag('label', $this->filter->requestNameRadius . 'class', '', true, $for) . $radiusText . \HTMLOperations::CreateClosingTag('label');
    $rendererRadius = new \Kufer\Web\Classes\Api\SearchRestriction\SearchRestrictionKatUmkreisRadiusRenderer();
    $result .= $labelRadius . ' ' . $rendererRadius->render($pVarName, $radiusText, $pVarParamZwei) . '<span>km</span>';

    return $result;
  }
}

class SearchRestrictionKatUmkreisPlzRenderer extends SearchRestrictionRendererTextBase
{
  public function __construct($pFilter = null)
  {
    parent::__construct(new SearchRestrictionKatUmkreisFilter());
    $this->maxlength = 5;
  }
}


class SearchRestrictionKatUmkreisRadiusRenderer extends SearchRestrictionRendererTextBase
{
  public function __construct($pFilter = null)
  {
    parent::__construct(new SearchRestrictionKatUmkreisFilter());
    $this->maxlength = 3;
  }

  public function render($pVarName, $pVarParam = '', $pVarParamZwei = '')
  {
    $string = new \StringObj($pVarName);
    $nojs = $string->endsWith('_nojs');
    $onChange = (!$nojs ? ' onchange="this.form.submit();"' : '');
    $val = $this->filter->getFilterValueRadius();
    $requestName = $this->filter->requestNameRadius;

    
    if ($val == SearchRestrictionKatUmkreisFilter::DEFAULTRADIUS && $this->filter->getFilterValue() === '') {
      $val = '';
    }

    $itemVal = \FilterBasis::getOnlyNumeric($val);
    $itemText = $this->getCleanOptionTextFromItem($pVarParamZwei !== '' ? $pVarParamZwei : $pVarParam);
    $id = $requestName;

    return \HTMLOperations::CreateInputTag(
      $requestName,
      'text',
      $id,
      $requestName . 'class',
      $itemVal,
      $onChange,
      false,
      false,
      10, 
      $itemText 
    );
  }
}