<?php
/**
 * @copyright (c) Kufer Software GmbH, Oderstraße 7, D-84453 Mühldorf
 */

namespace Kufer\Web\KuferTools\Utils\WssField\Kurs;

class WssFieldKursEinzelveranstaltung extends \Kufer\Web\KuferTools\Utils\WssField\WssField
{
  public function getChangedInfo($columnValues, $changedDm, $changedFields): array
  {
    $changedDmAndFieldTuple = array('changed' => $changedDm, 'changedFields' => $changedFields);

    
    $bereitsGeaendert = count(array_intersect($this->felderEinzelveranstaltungen(), $changedFields)) > 0;

    
    
    if ($this->datamodulColumn->accesslevel == 2 && $this->datamodulColumn->canedit == 1 && !$bereitsGeaendert) {
      $changedDmAndFieldTuple = $this->handleErwachseneTeilnehmer($changedDmAndFieldTuple, $changedDm, $columnValues);
      $changedDmAndFieldTuple = $this->handleKinder($changedDmAndFieldTuple, $changedDm, $columnValues);
    }

    return $changedDmAndFieldTuple;
  }

  private function handleErwachseneTeilnehmer($changedDmAndFieldTuple, $changedDm, $columnValues)
  {
    $ez = new Einzelveranstaltung();
    if (isset($columnValues[$this->getDatamodulName()]['teinzel_u'])) {
      $ez->initBevorzugeUnbekannt(
        $this->getNewValue('teinzel', $changedDm, $columnValues),
        $this->getNewValue('teinzel_m', $changedDm, $columnValues),
        $this->getNewValue('teinzel_w', $changedDm, $columnValues),
        $this->getNewValue('teinzel_u', $changedDm, $columnValues)
      );
    } else {
      $ez->initBevorzugeGesamt(
        $this->getNewValue('teinzel', $changedDm, $columnValues),
        $this->getNewValue('teinzel_m', $changedDm, $columnValues),
        $this->getNewValue('teinzel_w', $changedDm, $columnValues),
        $this->getNewValue('teinzel_u', $changedDm, $columnValues)
      );
    }

    $changedDmAndFieldTuple = $this->changeResult($changedDmAndFieldTuple, 'teinzel', $ez->getTeilnehmerGesamt());
    $changedDmAndFieldTuple = $this->changeResult($changedDmAndFieldTuple, 'teinzel_m', $ez->getTeilnehmerMaennlich());
    $changedDmAndFieldTuple = $this->changeResult($changedDmAndFieldTuple, 'teinzel_w', $ez->getTeilnehmerWeiblich());

    return $this->changeResult($changedDmAndFieldTuple, 'teinzel_u', $ez->getTeilnehmerUnbekannt());
  }

  private function handleKinder($changedDmAndFieldTuple, $changedDm, $columnValues)
  {
    $ez = new Einzelveranstaltung();
    if (isset($columnValues[$this->getDatamodulName()]['keinzel_u'])) {
      $ez->initBevorzugeUnbekannt(
        $this->getNewValue('kinder_ev', $changedDm, $columnValues),
        $this->getNewValue('keinzel_m', $changedDm, $columnValues),
        $this->getNewValue('keinzel_w', $changedDm, $columnValues),
        $this->getNewValue('keinzel_u', $changedDm, $columnValues)
      );
    } else {
      $ez->initBevorzugeGesamt(
        $this->getNewValue('kinder_ev', $changedDm, $columnValues),
        $this->getNewValue('keinzel_m', $changedDm, $columnValues),
        $this->getNewValue('keinzel_w', $changedDm, $columnValues),
        $this->getNewValue('keinzel_u', $changedDm, $columnValues)
      );
    }

    $changedDmAndFieldTuple = $this->changeResult($changedDmAndFieldTuple, 'kinder_ev', $ez->getTeilnehmerGesamt());
    $changedDmAndFieldTuple = $this->changeResult($changedDmAndFieldTuple, 'keinzel_m', $ez->getTeilnehmerMaennlich());
    $changedDmAndFieldTuple = $this->changeResult($changedDmAndFieldTuple, 'keinzel_w', $ez->getTeilnehmerWeiblich());

    return $this->changeResult($changedDmAndFieldTuple, 'keinzel_u', $ez->getTeilnehmerUnbekannt());
  }

  private function getOrigValue($fieldname, $changedDm)
  {
    return $changedDm->getVarValueEx($fieldname, $this->getDatamodulName());
  }

  private function getNewValue($fieldname, $changedDm, $columnValues)
  {
    return $columnValues[$this->getDatamodulName()][$fieldname] ?? $this->getOrigValue($fieldname, $changedDm);
  }

  private function changeResult($changedDmAndFieldTuple, $fieldname, $newValue)
  {
    if ($newValue != $this->getOrigValue($fieldname, $changedDmAndFieldTuple['changed'])) {
      $changedDmAndFieldTuple['changed']->setValue2VarEx($fieldname, $newValue, $this->getDatamodulName());
      $changedDmAndFieldTuple['changedFields'][] = $fieldname;
    }

    return $changedDmAndFieldTuple;
  }

  private function felderEinzelveranstaltungen()
  {
    return array('teinzel', 'teinzel_m', 'teinzel_w', 'teinzel_u', 'kinder_ev', 'keinzel_m', 'keinzel_w', 'keinzel_u');
  }
}