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

namespace Kufer\KuferTools\Basis\Kursdetails\DetailsReiter\Termine\TerminBearbeiten\KursAnpassenNachTerminBearbeiten;

use Kufer\KuferTools\Basis\BenachrichtigungNachBearbeiten\BenachrichtigungUtils;

abstract class KursAnpassenNachTerminBearbeitenUseCase extends \Kufer\KuferTools\Basis\UseCase\EinzelKursUseCase
{
  public function __construct($request, $response = null)
  {
    parent::__construct($request, $response ?? new KursAnpassenNachTerminBearbeitenResponse());
  }

  public function hasAccess($pToolSectionId = null)
  {
    
    if (!parent::hasAccess()) {
      return false;
    }
    if (!$this->request->isXsrfOk && !$this->request->isRefresh) {
      return false;
    }

    return true;
  }

  

  protected function getDataToChange()
  {
    $data = array();
    
    $data[] = $this->createItem('stundenanz', $this->request->stundenanz, $this->request->stundenanz_check);

    $data[] = $this->createItem('ende', $this->request->ende, $this->request->ende_check);

    $data[] = $this->createItem('dauer', $this->request->dauer, $this->request->dauer_check);

    $data[] = $this->createItem('beginn', $this->request->beginn, $this->request->beginn_check);

    $data[] = $this->createItem('beginn_dat', $this->request->beginn_dat, $this->request->beginn_dat_check);

    $data[] = $this->createItem('ende_dat', $this->request->ende_dat, $this->request->ende_dat_check);

    return $data;
  }

  protected function createItem(string $name, $newvalue, bool $checked)
  {
    $result = new class() {
      public $name;
      public $newvalue;
      public $checked;
    };

    $result->name = $name;
    $result->newvalue = $newvalue;
    $result->checked = $checked;

    return $result;
  }

  
  protected function preKursUpdateHook($data, $kurs)
  {
    return array($data, $kurs);
  }

  public function action()
  {
    if (!$this->request->isRefresh) {
      $kurs = $this->getKurs($this->request->knr);
      $origKurs = clone $kurs;

      
      

      $data = $this->getDataToChange();
      $rechte = $this->getRechte4KursBearbeiten();
      $nichtPassendeKursdaten = \PasstKursZuKurstageAPI::passenKursdatenZuKurstagen($kurs->id);
      foreach ($data as $key => $field) {
        if (!isset($rechte[$field->name]) || !$rechte[$field->name]) {
          unset($data[$key]);
        } else {
          
          foreach ($nichtPassendeKursdaten as $kursDaten) {
            if ($kursDaten['name'] == $field->name) {
              if (!$kursDaten['changeable']) {
                $field->newvalue = $kursDaten['newvalue'];
              }
            }
          } 
        } 
      } 

      list($data, $kurs) = $this->preKursUpdateHook($data, $kurs);
      $success = \PasstKursZuKurstageAPI::updateKurs($data, $kurs);
      if ($success) {
        $this->sendeBenachrichtigung($origKurs, $kurs, $data);
      }
    } else {
      $success = true;
    }

    return $this->getResponse($success);
  }

  
  abstract protected function getRechte4KursBearbeiten(): array;

  
  protected function sendeBenachrichtigung($pOrigKurs, $pChangedKurs, $pChangedData)
  {
    $changedFields = \ArrayOperations::mapAttribut($pChangedData, 'name');
    $changedFieldsLabels = $this->getChangedFieldsInReadableFormat($changedFields);
    $footerText = 'Kurs Bearbeiten nach Änderung von Terminen';
    $user = $this->getUser();

    BenachrichtigungUtils::sendeBenachrichtigungEmailKursdaten(
      array(),
      $pOrigKurs,
      $pChangedKurs,
      $changedFields,
      $changedFieldsLabels,
      $footerText,
      $user,
      $this->getToolId(),
      $this->getToolSectionId()
    );
  }

  
  protected function getChangedFieldsInReadableFormat($pChangedFields)
  {
    $excludeFields = array('bearbeiter');
    $mapping = array('stundenanz' => 'UE', 'beginn' => 'Uhrzeit von', 'ende' => 'Uhrzeit bis', 'dauer' => 'Dauer', 'beginn_dat' => 'Datum von', 'ende_dat' => 'Datum bis');
    $result = array();

    foreach ($pChangedFields as $field) {
      if (\in_array($field, $excludeFields)) {
        continue;
      }

      $result[$field] = $mapping[$field] ?? $field;
    }

    return $result;
  }

  protected function getResponse($success)
  {
    $this->response->success = $success;

    return $this->response;
  }
}

class KursAnpassenNachTerminBearbeitenRequest
{
  public $knr;

  public $stundenanz_check;
  public $stundenanz;
  public $ende_check;
  public $ende;
  public $dauer_check;
  public $dauer;
  public $beginn_check;
  public $beginn;
  public $beginn_dat_check;
  public $beginn_dat;
  public $ende_dat_check;
  public $ende_dat;
}

class KursAnpassenNachTerminBearbeitenResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  public $success = true; 
}