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

/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */
class ErsetzeWsfFeldnamenAnhandDynFeldBez
{
  
  public static function setzeDynamischeBezeichnung($pField)
  {
    $lcdatamodul = \StringOperations::strtolower($pField->datamodul);
    switch ($lcdatamodul) {
      case 'kurszusatztexte':
      case 'kursplanung':
        if (self::isFreidef($pField->fieldname)) {
          return self::mapName4Kursplanung($pField);
        }

        return self::_mapName4Kurs($pField);
      case 'dozent':
        if (self::isKrit($pField->fieldname)) {
          return self::mapName4Dozent($pField);
        }

        return $pField;
      case 'teilnehmer':
        if (self::isKrit($pField->fieldname)) {
          return self::mapName4Teilnehmer($pField);
        }

        return $pField;
      case 'kurs':
        if (self::isKursauswahl($pField->fieldname)) {
          return self::mapName4Kursauswahl($pField);
        }

        return $pField;
      case 'belegung':
        if (self::isHistorieKrit($pField->fieldname)) {
          return self::mapName4Belegung($pField);
        }

        return $pField;
      default:
        return $pField;
    }
  }

  private static function isKursauswahl(string $filename): bool
  {
    return preg_match('/^kursauswahl[0-9]{1,2}$/', $filename) === 1;
  }

  private static function getKursauswahlNumber(string $filename): int
  {
    $matches = array();
    preg_match('/^kursauswahl([0-9]{1,2})$/', $filename, $matches);
    if (count($matches) < 2) {
      return -1;
    }

    return $matches[1];
  }

  private static function isKrit(string $fieldname): bool
  {
    return preg_match('/^krit[0-9]{1,2}$/', $fieldname) === 1;
  }

  private static function getKritNumber(string $fieldname): int
  {
    $matches = array();
    preg_match('/^krit([0-9]{1,2})$/', $fieldname, $matches);
    if (count($matches) < 2) {
      
      return -1;
    }

    return (int)$matches[1];
  }

  private static function isFreidef(string $fieldname): bool
  {
    return preg_match('/^freidef[0-9]{1,2}$/', $fieldname) === 1;
  }

  private static function getFreidefNumber(string $fieldname): int
  {
    $matches = array();
    preg_match('/^freidef([0-9]{1,2})$/', $fieldname, $matches);
    if (count($matches) < 2) {
      return -1;
    }

    return (int)$matches[1];
  }

  private static function isHistorieKrit(string $fieldname): bool
  {
    return preg_match('/^historiekrit[0-9]{1,2}$/', $fieldname) === 1;
  }

  private static function getHistorieKritNumber(string $fieldname): int
  {
    $matches = array();
    preg_match('/^historiekrit([0-9]{1,2})$/', $fieldname, $matches);
    if (count($matches) < 2) {
      return -1;
    }

    return $matches[1];
  }

  private static function mapName4Dozent(\Field $pField): \Field
  {
    static $freidefs = null;
    if (is_null($freidefs)) {
      $dbwFreiwahl = \DBWorkFreiWaehlbareEingabefelder4Dozent::getDefaultInstance();
      $freidefs = $dbwFreiwahl->getFreiWaehlbareFelder();
      $freidefs = \ArrayOperations::setAttributAsKey($freidefs, 'id');
    }

    return self::mapName4Freidef(self::getKritNumber($pField->fieldname), $pField, $freidefs);
  }

  private static function mapName4Teilnehmer(\Field $pField): \Field
  {
    static $freidefs = null;
    if (is_null($freidefs)) {
      $dbwFreiwahl = \DBWorkFreiWaehlbareEingabefelder4Teilnehmer::getDefaultInstance();
      $freidefs = $dbwFreiwahl->getFreiWaehlbareFelder();
      $freidefs = \ArrayOperations::setAttributAsKey($freidefs, 'id');
    }

    return self::mapName4Freidef(self::getKritNumber($pField->fieldname), $pField, $freidefs);
  }

  private static function mapName4Kursauswahl(\Field $pField): \Field
  {
    static $freidefs = null;
    if (is_null($freidefs)) {
      $dbwFreiwahl = \DBWorkFreiWaehlbareEingabefelder4Kurs::getDefaultInstance();
      $freidefs = $dbwFreiwahl->getFreiWaehlbareFelder();
      $freidefs = \ArrayOperations::setAttributAsKey($freidefs, 'id');
    }

    return self::mapName4Freidef(self::getKursauswahlNumber($pField->fieldname), $pField, $freidefs);
  }

  private static function mapName4Kursplanung(\Field $pField): \Field
  {
    static $freidefs = null;
    if (is_null($freidefs)) {
      $dbwFreiwahl = \DBWorkFreiWaehlbareEingabefelder4Kursplanung::getDefaultInstance();
      $freidefs = $dbwFreiwahl->getFreiWaehlbareFelder();
      $freidefs = \ArrayOperations::setAttributAsKey($freidefs, 'id');
    }

    return self::mapName4Freidef(self::getFreidefNumber($pField->fieldname), $pField, $freidefs);
  }

  private static function mapName4Belegung(\Field $pField): \Field
  {
    static $freidefs = null;
    if (is_null($freidefs)) {
      $dbwFreiwahl = \DBWorkFreiWaehlbareEingabefelder4Belegung::getDefaultInstance();
      $freidefs = $dbwFreiwahl->getFreiWaehlbareFelder();
      $freidefs = \ArrayOperations::setAttributAsKey($freidefs, 'id');
    }

    return self::mapName4Freidef(self::getHistorieKritNumber($pField->fieldname), $pField, $freidefs);
  }

  private static function mapName4Freidef(int $num, \Field $field, array $freidefs): \Field
  {
    $conf = $freidefs[$num] ?? null;
    if (!is_null($conf)) {
      $freidefBez = $conf->bezeichnung ?? null;
      if (!is_null($freidefBez)) {
        $field->name = $freidefBez . " ($num)";
      }
    }

    return $field;
  }

  
  private static function _mapName4Kurs($pField)
  {
    $dynFeldAry = self::_getDynFeldAry();
    $wsfDynMap = self::_getWsfDynMap4Kurs();

    return self::_translate($pField, $wsfDynMap, $dynFeldAry);
  }

  
  private static function _getDynFeldAry()
  {
    static $dynFeldBez = null;
    if (is_null($dynFeldBez)) {
      $dbwDynFeldBez = new DBWorkDynFeldBez(1, true);
      $dynFeldBez = $dbwDynFeldBez->getAll();
    }

    return $dynFeldBez;
  }

  
  private static function _getWsfDynMap4Kurs()
  {
    return array('zielgruppe' => 'Kurs Zusatztext 1', 
        'voraussetzungen' => 'Kurs Zusatztext 2',
        'ziel' => 'Kurs Zusatztext 3',
        'methode' => 'Kurs Zusatztext 4',
        'anmelde' => 'Kurs Zusatztext 5',
        'anmeldebedingungen' => 'Kurs Zusatztext 5' 
    );
  }

  
  private static function _translate($pField, $pMapFieldDynBez, $pDynFeldAry)
  {
    $fieldname = StringOperations::strtolower($pField->fieldname);

    if (isset($pMapFieldDynBez[$fieldname])) {
      foreach ($pDynFeldAry as $dynFeld) {
        if ($dynFeld->stdbez == $pMapFieldDynBez[$fieldname]) {
          if (trim($dynFeld->neubez) != '') {
            $pField->name = $dynFeld->neubez;
          }

          return $pField;
        }
      }
    }

    return $pField;
  }
}