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

namespace Kufer\Web\KuferTools\Dozent\SammelVertraege\AkzeptierenOderAblehnen\Formular;

use Kufer\Web\KuferTools\Utils\ResponseStrategies\ResponseMitRahmen;
use Kufer\Web\Classes\Datamodul\SammelvertragDatDoc;
use Kufer\Web\KuferTools\Utils\Dozent\SammelVertraegeUtils;


abstract class SammelVertraegeAkzeptierenOderAblehnenUseCase extends \Kufer\Web\KuferTools\Basis\UseCase\ToolUseCase
{
  
  private $sammelvertraege;

  
  public function __construct($request, $response = null)
  {
    parent::__construct($request, $response ?? new SammelVertraegeAkzeptierenOderAblehnenResponse());
  }

  public function hasAccess($pToolSectionId = null)
  {
    if (!parent::hasAccess($pToolSectionId)) {
      return false;
    }

    
    if ($this->getUser()->getDnrIdentifier()->isEmpty(false)) {
      return false;
    }

    if (!\SessionOperations::isDmzBetrieb()) {
      return false;
    }

    if (!$this->request->isXsrfOk) {
      return false; 
    }

    $sammelvertraege = $this->getSammelvertraegeDatDoc();
    if (is_null($sammelvertraege)) {
      return false;
    }

    
    if (empty(array_diff($sammelvertraege->kursIds, $sammelvertraege->akzeptiertKursIds, $sammelvertraege->abgelehntKursIds))) {
      return false;
    }

    return true;
  }

  public function getSammelvertraegeDatDoc(): ?SammelvertragDatDoc
  {
    if (is_null($this->sammelvertraege)) {
      
      $this->sammelvertraege = SammelVertraegeUtils::getSammelVertrag(
        $this->getUser()->getDnrIdentifier(),
        $this->request->id,
        $this->toolSettings->oldVertragPartOfNew
      );
    }

    return $this->sammelvertraege;
  }

  public function action()
  {
    $this->handleResponseStrategies();

    $this->response->sammelvertraege = $this->getSammelvertraegeDatDoc();
    assert(!is_null($this->response->sammelvertraege));
    $this->response->kurse = $this->getKurse();
    $this->response->showInput = $this->shouldShowInfoAnEinrichtungInput();
    $this->response->additionalInfo = $this->request->additionalInfo ?? '';
    $this->response->kurseIds = $this->request->kursIds ?? \ArrayOperations::mapAttribut($this->response->kurse, 'id');

    $this->response->wortSingular = $this->toolSettings->wortSingularVertrag;
    $this->response->wortPlural = $this->toolSettings->wortPluralVertraege;

    return $this->response;
  }

  
  protected function getKurse(): array
  {
    $sammelvertrag = $this->getSammelvertraegeDatDoc();
    $kursIds = array_diff($sammelvertrag->kursIds, $sammelvertrag->akzeptiertKursIds, $sammelvertrag->abgelehntKursIds);
    $dbwKurse = \DBWorkKurseWT::getDefaultInstance();

    return array_map(static function(string $kursId) use ($dbwKurse): \Kurs {
      $result = $dbwKurse->getKurs($kursId);
      if (empty($result->id)) {
        $result->id = $kursId;
        $result->nr_anzeige = $kursId;
      }

      return $result;
    }, $kursIds);
  }

  protected function getResponseStrategies()
  {
    $result = parent::getResponseStrategies();
    $result[] = new ResponseMitRahmen($this);

    return $result;
  }

  abstract protected function shouldShowInfoAnEinrichtungInput(): bool;
}