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

namespace Kufer\KuferTools\Utils\WssField;

abstract class WssFieldMultiselect extends WssField
{
  abstract protected function createNewValueDatamodulArray($newValues, $datamodul);

  abstract protected function getPrefix();

  abstract protected function getTextFieldname();

  abstract protected function getValueFieldname();

  abstract protected function getDatamoduleForAvailableItems();

  protected function getMultiselectHeight()
  {
    return '4';
  }

  protected function getFieldBearbeitbar($datamodulWert, $isFormular, string $requestPrefix = '')
  {
    $multiselect = $this->getMultiselect($datamodulWert);
    $buttons = $this->getButtonsForMultiselect();
    $divAttrs = array(
        'class' => $this->getWssFieldNameWithPrefix() . ' editList_container',
        'data-availableitems' => \JsonOperations::encodeBase64($this->getAvailableOptionsAsArrays())
        );

    return \HTMLOperations::wrapInDiv($divAttrs, $multiselect . $buttons);
  }

  protected function getFieldNichtBearbeitbar($datamodul, $isFormular, string $requestPrefix = '')
  {
    $html = '';
    $textFieldName = $this->getTextFieldname();
    $fieldname = $this->getFieldName();
    foreach ($datamodul->{$fieldname} as $optionObject) {
      $html .= \HTMLOperations::wrapInSpan(array('class' => $this->getWssFieldNameWithPrefix()), $optionObject->{$textFieldName});
      $html .= BR_WT;
    }

    return $html;
  }

  private function getMultiselect($datamodul)
  {
    $options = $this->getOptionsForMultiselect($datamodul);

    return \HTMLOperations::CreateSelectTag(
      $options,
      $this->getWssFieldNameWithPrefix() . '[]',
      'editList_items form-control form-control-multiple-fix multiSelectWithJs ' . $this->getMandatoryClass($this->datamodulColumn),
      'editList_items',
      $this->getMultiselectHeight(),
      '',
      true
    );
  }

  protected function getOptionsForMultiselect($datamodul)
  {
    $options = array();
    $textFieldName = $this->getTextFieldname();
    $valueFieldName = $this->getValueFieldname();
    $fieldname = $this->getFieldName();

    if (!empty($datamodul->{$fieldname})) {
      foreach ($datamodul->{$fieldname} as $value) {
        $options[] = \HTMLOperations::createOptionTag($value->{$textFieldName}, $value->{$valueFieldName}, true);
      }
    }

    return $options;
  }

  private function getAvailableOptionsAsArrays()
  {
    $datamodulArray = $this->getDatamoduleForAvailableItems();
    $text = $this->getTextFieldname();
    $value = $this->getValueFieldname();
    $result = array();
    foreach ($datamodulArray as $datamodul) {
      $result[$datamodul->{$value}] = $this->htmlEncode($datamodul->{$text});
    }

    return $result;
  }

  private function getButtonsForMultiselect()
  {
    $searchIcon = \HTMLOperations::getFontAwesomeIconITag('search');
    $openDialogButton = \HTMLOperations::wrapInButton(array('class' => 'editList_openDialog'), $searchIcon);

    $trashIcon = \HTMLOperations::getFontAwesomeIconITag('trash');
    $deleteItemButton = \HTMLOperations::wrapInButton(array('class' => 'editList_deleteSelected'), $trashIcon);

    return $openDialogButton . $deleteItemButton;
  }

  public function getChangedInfo($columnValues, $changedDm, $changedFields): array
  {
    if ($this->datamodulColumn->accesslevel == 2 && $this->datamodulColumn->canedit == 1) {
      $datamodul = $this->getDatamodulName();
      $fieldname = $this->getFieldName();
      if (isset($columnValues[$datamodul][$fieldname])) {
        $origValue = $changedDm->getVarValueEx($fieldname, $datamodul);
        $newValue = $columnValues[$datamodul][$fieldname] != '' ? $columnValues[$datamodul][$fieldname] : array(); 
        $datamodulArray = $this->createNewValueDatamodulArray($newValue, $changedDm);
        if (count($origValue) !== count($newValue)) {
          $changedDm->{$fieldname} = $datamodulArray;
          $changedFields[] = $fieldname;
        } else {
          foreach ($origValue as $origElement) {
            if (!\in_array($origElement->getVarValue($this->getValueFieldname()), $newValue)) {
              $changedDm->{$fieldname} = $datamodulArray;
              $changedFields[] = $fieldname;
              break;
            }
          }
        }
      }
    }

    return array('changed' => $changedDm, 'changedFields' => $changedFields);
  }

  protected function getWssFieldNameWithPrefix()
  {
    return $this->getPrefix() . '_' . $this->getFieldName();
  }

  public function isValid($value, bool $checkPflichtfelder = true): bool
  {
    return true;
  }

  public function inPost(string $requestPrefix = ''): bool
  {
    return $this->datamodulColumn->canedit;
  }
}