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

namespace Kufer\Web\KuferTools\Allgemein\PersoenlicheDaten\PersoenlicheDaten;

abstract class PersoenlicheDatenUseCase extends \Kufer\Web\KuferTools\Basis\UseCase\ToolUseCase
{
  public function __construct($request)
  {
    parent::__construct($request, new PersoenlicheDatenResponse());
  }

  public static function getToolId()
  {
    return 13;
  }

  abstract protected function isBearbeitbar();

  abstract protected function getPerson($user);

  abstract protected function aenderungenDirektUebermitteln(): bool;

  
  protected function getNichtDirektAenderbareFelder(): array
  {
    $result = array();
    for ($i = 1; $i <= 6; $i++) {
      $result[] = 'foto_' . $i;
      $result[] = 'copyright_' . $i;
    }

    return $result;
  }

  public function action()
  {
    $user = $this->getUser();
    $this->response = $this->getResponse($user);
    $this->updateOrFilterColumns();

    return $this->response;
  }

  protected function updateOrFilterColumns(): void
  {
    if ((\SessionOperations::isDmzBetrieb() || !\SessionOperations::isReplikat())
      && $this->aenderungenDirektUebermitteln()) {
      $nichtAenderbareFelder = $this->getNichtDirektAenderbareFelder();
      if (empty($nichtAenderbareFelder)) {
        return;
      }

      $this->response->columns = array_filter(
        $this->response->columns,
        static function(\Column $column) use ($nichtAenderbareFelder): bool {
          if (in_array($column->fieldname, $nichtAenderbareFelder)) {
            return false;
          }

          return true;
        }
      );
    }
  }

  protected function getResponse($user)
  {
    $this->handleResponseStrategies();
    if ($this->request->changedPerson) {
      $this->response->person = $this->request->changedPerson;
    } else {
      $this->response->person = $this->getPerson($user);
    }
    $this->response->origPerson = $this->getPerson($user);
    $this->response->statusCode = $this->request->statusCode;

    if ($this->toolSettings->showFreeField && trim($this->toolSettings->freeField) != '') {
      $this->response->freeField = $this->toolSettings->freeField;
    }

    $this->response->wrongFields = $this->request->wrongFields;
    $this->response->passwort = $this->request->passwort;
    $this->response->bearbeitbar = $this->isBearbeitbar();
    $tool0Settings = $this->toolSettingsApi->getToolSettings(0);
    $this->response->emailAendernErfordertPwEingabe = $tool0Settings->emailAendernErfordertPwEingabe;
    $this->response->aenderungenDirektUebermitteln = (\SessionOperations::isDmzBetrieb() || !\SessionOperations::isReplikat())
      && $this->aenderungenDirektUebermitteln();

    return $this->response;
  }

  protected function getResponseStrategies()
  {
    return array(
        new \Kufer\Web\KuferTools\Utils\ResponseStrategies\ResponseMitRahmen($this),
        new \Kufer\Web\KuferTools\Utils\ResponseStrategies\ResponseWss($this),
        new \Kufer\Web\KuferTools\Utils\ResponseStrategies\ResponseWithToolSettings($this)
    );
  }
}

class PersoenlicheDatenRequest
{
  public $statusCode;
  public $changedPerson;
  public $wrongFields;
  public $passwort = '';
}

class PersoenlicheDatenResponse extends \Kufer\Web\KuferTools\Basis\UseCase\ToolResponse
{
  public $toolSection;
  public $toolSettings;
  public $columns;
  public $user;
  public $menuPunkte = array();
  public $person;
  public $origPerson;
  public $statusCode;
  public $freeField = '';
  public $bearbeitbar = false;
  public $wrongFields;
  public $passwort = '';
  public $emailAendernErfordertPwEingabe = false;
  public $aenderungenDirektUebermitteln = false;
}