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

namespace Kufer\KuferTools\Utils;

class WssHandler
{
  private $user;
  private $toolSectionId;

  
  private $changedInfos = array();

  
  private $columns = array();

  public function __construct($toolSectionId, $user)
  {
    $this->user = $user;
    $this->toolSectionId = $toolSectionId;
  }

  public function getColumnValuesFromPost(string $requestPrefix = '', string $cachePrefix = '', bool $recache = false)
  {
    $result = array();
    $columns = $this->getColumnsCached($cachePrefix, $recache);
    $wssFactory = new WssField\WssFactory();

    foreach ($columns as $column) {
      
      
      $wssField = $wssFactory->getWss($column);

      $wssFieldInPost = $wssField->inPost($requestPrefix);
      if ($wssFieldInPost) {
        $datamodul = $wssField->getDatamodulName();
        if (!isset($result[$datamodul])) {
          $result[$datamodul] = array();
        }
        $result[$datamodul][$wssField->getFieldName()] = $wssField->getValueFromPost($requestPrefix);
      }
    } 

    return $result;
  }

  public function checkColumnValues($columnValues, $checkPflichtfelder = true, string $cachePrefix = '', bool $recache = false)
  {
    $wrongFields = array();

    $wssFactory = new WssField\WssFactory();

    
    
    

    $columns = $this->getColumnsCached($cachePrefix, $recache);
    foreach ($columns as $column) {
      $wssField = $wssFactory->getWss($column);

      $datamodul = $wssField->getDatamodulName();
      $fieldname = $wssField->getFieldName();

      $value = '';
      if (isset($columnValues[$datamodul], $columnValues[$datamodul][$fieldname])) {
        $value = $columnValues[$datamodul][$fieldname];
      }

      if (!$wssField->isValid($value, $checkPflichtfelder)) {
        $wrongFields[] = $wssField;
      }
    }

    return $wrongFields;
  }

  public function getChangedFieldnames($origDm, $columnValues, string $cachePrefix = '', bool $recache = false)
  {
    $infos = $this->getChangedInfoCached($origDm, $columnValues, $cachePrefix, $recache);

    return $infos['changedFields'];
  }

  public function getChangedDm($origDm, $columnValues, string $cachePrefix = '', bool $recache = false)
  {
    $infos = $this->getChangedInfoCached($origDm, $columnValues, $cachePrefix, $recache);

    return $infos['changed'];
  }

  private function getChangedInfoCached($origDm, $columnValues, string $cachePrefix = '', bool $recache = false)
  {
    
    if ($recache || !array_key_exists($cachePrefix, $this->changedInfos)) {
      $this->changedInfos[$cachePrefix] = $this->getChangedInfo($origDm, $columnValues, $cachePrefix, $recache);
    }

    return $this->changedInfos[$cachePrefix];
  }

  private function getChangedInfo($origDm, $columnValues, string $cachePrefix = '', bool $recache = false)
  {
    $columns = $this->getColumnsCached($cachePrefix, $recache);
    $changedDm = clone($origDm);

    $changedFields = array();

    $wssFactory = new WssField\WssFactory();

    foreach ($columns as $column) {
      $wssField = $wssFactory->getWss($column);
      $changedInfo = $wssField->getChangedInfo($columnValues, $changedDm, $changedFields);
      $changedDm = $changedInfo['changed'];
      $changedFields = $changedInfo['changedFields'];
    }

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

  public function getColumnsCached(string $cachePrefix = '', bool $recache = false)
  {
    if ($recache || !array_key_exists($cachePrefix, $this->columns)) {
      $this->columns[$cachePrefix] = $this->getColumns();
    }

    return $this->columns[$cachePrefix];
  }

  private function getColumns()
  {
    if (!is_null($this->user) && !$this->user->isEmpty()) {
      $wssInFiles = new WssInFiles();

      return $wssInFiles->getWss($this->toolSectionId, $this->user);
    } else {
      return array();
    }
  }

  
  public static function getErrorMessage($pWrongFields)
  {
    $result = '';
    if (count($pWrongFields) > 0) {
      $result .= 'Das Formular wurde nicht korrekt ausgefüllt. Bitte überprüfen Sie folgende Felder:';
      $result .= '<ul>';
      foreach ($pWrongFields as $wssField) {
        $column = $wssField->getDmColumnMitDefaultBeruecksichtigt();
        $result .= '<li>' . $column->title . '</li>';
      }
      $result .= '</ul>';
    }

    return $result;
  }

  
  public function getFieldsInReadableFormat($pFields, string $cachePrefix = '', bool $recache = false)
  {
    $columns = $this->getColumnsCached($cachePrefix, $recache);
    $columnsWithKeys = \ArrayOperations::setAttributAsKey($columns, 'fieldname');
    $result = array();

    foreach ($pFields as $field) {
      if (isset($columnsWithKeys[$field])) {
        $result[$field] = $columnsWithKeys[$field]->title;
      }
    }

    return $result;
  }

  
  public static function getFieldsInReadableFormatNoColumns($pFields)
  {
    $mapping = array('datum' => 'Datum', 'ue' => 'UE', 'zeit_beg' => 'Beginn', 'minuten' => 'Minuten', 'pause' => 'Pause',
        'zeit_ende' => 'Ende', 'ausfall' => 'Status', 'ort' => 'Kursort', 'bemerkungen' => 'Bemerkung');
    $result = array();

    foreach ($pFields as $field) {
      if (isset($mapping[$field])) {
        $result[$field] = $mapping[$field];
      } else {
        $result[$field] = $field;
      }
    }

    return $result;
  }
}