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

namespace Kufer\Web\KuferTools\Basis\Kursdetails\Detailsreiter\Dokumente;

use Kufer\Web\Classes\Stream\ArrayStream;

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

  public function action()
  {
    return $this->getResponse();
  }

  protected function getResponse()
  {
    try {
      $this->response->datDoc = $this->getDokumentenListe();
      $groupedByFach = $this->toolSettings->reiterDokumenteGroupByFach;
      $groupedByPerson = $this->toolSettings->reiterDokumenteGroupByPerson;
      if ($groupedByFach || $groupedByPerson) {
        $datDocGrouped = (new ArrayStream($this->response->datDoc))
          ->groupByMultipleValues(function(\Dat_doc $dat_doc) use ($groupedByFach, $groupedByPerson): array {
            $key = array();
            if ($groupedByFach) {
              $key[] = empty($dat_doc->ktFachId) ? 0 : $dat_doc->ktFachId;
            }

            if ($groupedByPerson) {
              $key[] = empty($dat_doc->ktErstelltVonId) ? 0 : ($dat_doc->ktErstelltVonTyp . $dat_doc->ktErstelltVonId);
            }

            return $key;
          });

        $sortFns = array();
        if ($groupedByFach) {
          $sortFns[] = static function($a, $b): int {
            
            $a = !is_array($a) ? $a : \ArrayOperations::findRecursive(\FunctionOperations::instanceOf(\Dat_doc::class), $a);
            
            $b = !is_array($b) ? $b : \ArrayOperations::findRecursive(\FunctionOperations::instanceOf(\Dat_doc::class), $b);

            $a = $a->getKtFach();
            $b = $b->getKtFach();
            $aNull = is_null($a);
            $bNull = is_null($b);
            if ($aNull || $bNull) {
              return ($aNull ? 1 : 0) - ($bNull ? 1 : 0);
            }

            return strcmp($a->fach, $b->fach);
          };
        }
        if ($groupedByPerson) {
          $sortFns[] = static function($a, $b): int {
            
            $a = !is_array($a) ? $a : \ArrayOperations::findRecursive(\FunctionOperations::instanceOf(\Dat_doc::class), $a);
            
            $b = !is_array($b) ? $b : \ArrayOperations::findRecursive(\FunctionOperations::instanceOf(\Dat_doc::class), $b);

            $a = $a->getKtErstelltVon();
            $b = $b->getKtErstelltVon();
            $aNull = is_null($a);
            $bNull = is_null($b);
            if ($aNull || $bNull) {
              return ($aNull ? 1 : 0) - ($bNull ? 1 : 0);
            }

            return strcmp($a->getVarValue('name_ggf_vorname'), $b->getVarValue('name_ggf_vorname'));
          };
        }

        $datDocGrouped = (new ArrayStream($datDocGrouped))->multiSort(...$sortFns);

        $this->response->datDocGrouped = $datDocGrouped;
        $this->response->groupedByFach = $groupedByFach;
        $this->response->groupedByPerson = $groupedByPerson;
      }
    } catch (\Exception $ex) {
      $this->response->dmzNichtErreichbar = true;
    }
    $this->response->kurs = $this->getKurs($this->getKursId());
    $this->response->uploadAllowed = $this->isUploadAllowed();

    return $this->response;
  }

  private function getDokumentenListe()
  {
    $datDoc = $this->getDatDocObj();

    return \DatDocAPI::getDokumentenListeFromDmz($datDoc);
  }

  protected function getDatDocObj()
  {
    $result = new \Dat_doc();
    $result->knr = $this->getKursId();

    return $result;
  }

  protected function isUploadAllowed(): bool
  {
    return false;
  }
}

class DokumenteResponse extends \Kufer\Web\KuferTools\Basis\UseCase\ToolResponse
{
  public $datDoc;
  public $datDocGrouped;
  public $kurs;
  public $dmzNichtErreichbar;
  public $uploadAllowed;
  
  public $groupedByFach;
  
  public $groupedByPerson;
}