<?php

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

namespace classes\addfunctions\htmloperations {

  
  interface FreifeldType
  {
    public function getCssClass();

    public function getChecked($pValue);

    public function getType();

    public function getValue($pValue);

    public function getMaxLength();

    
    public function getHtml($pValue, $pPerson = '', $pVorbelegung = '', $pSelectDefaultOption = '');

    public function init($pBindingTarget, $pPrefix, $pSuffix, $pIndivid = '');
  }

  class FreifeldTypeDefault implements FreifeldType
  {
    
    protected $completeType = '';
    
    protected $myHl;
    protected $prefix = '';
    protected $suffix = '';
    protected $bindingTarget = ''; 
    protected $individ = '';

    public function __construct($pFeld)
    {
      $this->myHl = $pFeld;
    }

    public function init($pBindingTarget, $pPrefix, $pSuffix, $pIndivid = '')
    {
      $this->prefix = $pPrefix;
      $this->suffix = $pSuffix;
      $this->bindingTarget = $pBindingTarget;
      $this->individ = $pIndivid;
    }

    public function getCssClassForType()
    {
      return '';
    }

    public function getChecked($pValue)
    {
      return false;
    }

    public function getType()
    {
      return '';
    }

    public function getValue($pValue)
    {
      return $pValue;
    }

    public function getMaxLength()
    {
      return '';
    }

    public function getCssClass()
    {
      return $this->getCssClassBase() . $this->getCssClassForType();
    }

    public function getHtml($pValue, $pPerson = '', $pVorbelegung = '', $pSelectDefaultOption = '')
    {
      $this->person = $pPerson;
      $checked = $this->getChecked($pValue);
      $individ = $this->getIndivid($this->getDataBinding());

      return \HTMLOperations::CreateInputTag(
        $this->getName(),
        $this->getType(),
        $this->getId(),
        $this->getCssClass(),
        $this->getValue($pValue),
        $individ,
        $checked,
        false,
        $this->getMaxLength(),
        $this->getPlaceholder($pVorbelegung)
      );
    }

    protected function getIndivid($part)
    {
      $result = $part;
      if (strlen($this->individ) > 0) {
        if (strlen($result) > 0) {
          $result = $this->individ . ' ' . $result;
        } else {
          $result = $this->individ;
        }
      }

      return $result;
    }

    protected $person = '';

    protected function getName()
    {
      return $this->prefix . $this->person . '_' . $this->suffix . $this->myHl->id;
    }

    protected function getId()
    {
      return $this->getName() . 'id';
    }

    protected function getCssClassBase()
    {
      return $this->prefix . ' ' . $this->prefix . '_' . $this->suffix . $this->myHl->id;
    }

    protected function getDataBinding()
    {
      return 'data-bind="value: ' . $this->bindingTarget . $this->suffix . $this->myHl->id . '"';
    }

    protected function getPlaceholder($pString)
    {
      return $pString;
    }
  }

  class FreifeldTypeC extends FreifeldTypeDefault
  {
    public function getCssClassForType()
    {
      return ' freifeld_checkbox';
    }

    public function getChecked($pValue)
    {
      return ($pValue == 'W');
    }

    public function getType()
    {
      return 'checkbox';
    }

    public function getValue($pValue)
    {
      return 'W';
    }

    protected function getDataBinding()
    {
      return 'data-bind="checked: ' . $this->bindingTarget . $this->suffix . $this->myHl->id . ', checkedValue: \'W\'"';
    }

    protected function getPlaceholder($pString)
    {
      return '';
    }
  }

  class FreifeldTypeT extends FreifeldTypeDefault
  {
    public function getCssClassForType()
    {
      return ' freifeld_text';
    }

    public function getType()
    {
      return 'text';
    }

    public function getMaxLength()
    {
      return (int)\StringOperations::substr($this->myHl->typ, 1);
    }
  }

  class FreifeldTypeSelect extends FreifeldTypeDefault
  {
    public function getCssClassForType()
    {
      return ' freifeld_select';
    }

    public function getType()
    {
      return 'select';
    }

    protected function getPlaceholder($pString)
    {
      return '';
    }

    protected function getDataBinding($pSelectDefaultOption = '')
    {
      $options = \ArrayOperations::mapAttribut($this->myHl->hinterlegung, 'text');
      $selectDefaultOption = ($pSelectDefaultOption != '' ? $pSelectDefaultOption : \HTMLOperations::getPrefix4FreifeldDefaultOption() . $this->myHl->bezeichnung);
      $result = 'data-bind="optionsCaption: \'' . \HTMLOperations::getText4FreifeldDefaultOption(
        $selectDefaultOption,
        $this->myHl->id
      ) . '\', ';
      $result .= 'options: [';
      $result .= \StringOperations::convertArray2InList($options);
      $result .= '], value: ' . $this->bindingTarget . $this->suffix . $this->myHl->id . '"';

      return $result;
    }

    public function getHtml($pValue, $pPerson = '', $pVorbelegung = '', $pSelectDefaultOption = '')
    {
      $this->person = $pPerson;
      $individ = '';
      $options = '';
      $up = new \URLParameterHauptmenue();
      if (\SessionOperations::isNeuesAnmeldeverfahren() && $up->kathaupt == 500) {
        $individ = $this->getDataBinding($pSelectDefaultOption);
      } else {
        $options = \HTMLOperations::getOptionsForFreifeld($pValue, $this->myHl->id, $this->myHl->hinterlegung, $pSelectDefaultOption);
      }
      $individ = $this->getIndivid($individ);

      return \HTMLOperations::CreateSelectTag($options, $this->getName(), $this->getCssClass(), $this->getId(), '', $individ);
    }
  }

  class FreifeldTypeD extends FreifeldTypeDefault
  {
    public function getCssClassForType()
    {
      return ' datepicker date val_date';
    }

    public function getMaxLength()
    {
      return 10;
    }

    public function getType()
    {
      return 'text';
    }
  }

  class FreifeldTypeZ extends FreifeldTypeDefault
  {
    public function getCssClassForType()
    {
      return ' freifeld_zahl val_int';
    }

    public function getMaxLength()
    {
      return 15;
    }

    public function getType()
    {
      return 'number';
    }
  }

  class FreifeldHelper
  {
    
    private $myType;

    
    public function initByFeld($pFeld, $pBindingTarget, $pPrefix, $pSuffix, $pIndivid = '')
    {
      $typ = \StringOperations::strtoupper(\StringOperations::substr($pFeld->typ, 0, 1));
      switch ($typ) {
        case SQL_FREIDEF_TYP_CHECKBOX:
          $this->myType = new FreifeldTypeC($pFeld);
          break;
        case SQL_FREIDEF_TYP_TEXT:
          if (count($pFeld->hinterlegung) > 0) {
            $this->myType = new FreifeldTypeSelect($pFeld);
          } else {
            $this->myType = new FreifeldTypeT($pFeld);
          }
          break;
        case SQL_FREIDEF_TYP_DATE:
          $this->myType = new FreifeldTypeD($pFeld);
          break;
        case SQL_FREIDEF_TYP_NUMBER:
          $this->myType = new FreifeldTypeZ($pFeld);
          break;
        default:
          $this->myType = new FreifeldTypeDefault($pFeld);
      }
      $this->myType->init($pBindingTarget, $pPrefix, $pSuffix, $pIndivid);
    }

    public function getCssClass()
    {
      return $this->myType->getCssClass();
    }

    public function getChecked($pValue)
    {
      return $this->myType->getChecked($pValue);
    }

    public function getType()
    {
      return $this->myType->getType();
    }

    public function getValue($pValue)
    {
      return $this->myType->getValue($pValue);
    }

    public function getMaxLength()
    {
      return $this->myType->getMaxLength();
    }

    public function getHtml($pValue, $pPerson = '', $pVorbelegung = '', $pSelectDefaultOption = '')
    {
      return $this->myType->getHtml($pValue, $pPerson, $pVorbelegung, $pSelectDefaultOption);
    }
  }
}