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

namespace Kufer\KuferTools\Basis\KursDetails\Feedback;

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

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

    return true;
  }

  public function action()
  {
    $kurs = $this->getKurs($this->getKursnummer());

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

  private function getMaxGroesseProDatei($zentralSettings)
  {
    $fileSize = \ConstantOperations::getMaxFileSize4Upload($zentralSettings->uploadmaxsize);

    return \MathOperations::getBytesUmrechnung($fileSize);
  }

  private function getMaxGroesseAllerDateien($zentralSettings)
  {
    $fileSizeAllerDateien = \ConstantOperations::getMaxFileSize4Upload(5 * ($zentralSettings->uploadmaxsize));

    return \MathOperations::getBytesUmrechnung($fileSizeAllerDateien);
  }

  protected function getResponse($kurs)
  {
    $user = $this->getUser();
    $this->handleResponseStrategies();
    $this->response->kurs = $kurs;
    $this->response->toolId = $this->getToolId();
    $this->response->action = $this->getAction();
    $this->response->betreff = $this->getBetreff($kurs, $user);
    $this->response->requestId = $this->getXsrfRequestedId();

    if (isset($this->request->belegId)) {
      $this->response->belegId = $this->request->belegId;
    }

    $this->response->sendEMailToVerwMitarbeiter = $this->shouldSendToVerwaltendenMitarbeiter($kurs);
    $this->response->sendEMailToPlanendMitarbeiter = $this->shouldSendToPlanenendenMitarbeiter($kurs);
    $this->response->sendEMailToEinrichtung = $this->shouldSendToEinrichtung($kurs);
    $this->response->sendEMailToDozent = $this->shouldSendCopy();

    $zentralSettings = \ToolSettingsAPI::getToolSettings(0);
    $this->response->maxGroesseProDatei = $this->getMaxGroesseProDatei($zentralSettings);
    $this->response->maxGroesseAllerDateien = $this->getMaxGroesseAllerDateien($zentralSettings);

    $allgSettings = \AllgSettingsAPI::getAllgSettings();
    $this->response->vhsname = $allgSettings->vhsname ?? 'Einrichtung';

    return $this->response;
  }

  protected function getBetreff(\Kurs $kurs, \Benutzer $user)
  {
    return $kurs->getVarValue('titel_zeile1') . ' - ' . $user->vorname . ' ' . $user->name;
  }

  protected function getXsrfRequestedId()
  {
    return $this->getRequestedId();
  }

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

  protected function shouldSendToVerwaltendenMitarbeiter(\Kurs $kurs): bool
  {
    return ($this->toolSettings->stornoBenachrichtigungsMailAnVerwMa ?? false)
      && $kurs->getVarValue('verwaltung_ma') !== '';
  }

  protected function shouldSendToPlanenendenMitarbeiter(\Kurs $kurs): bool
  {
    return ($this->toolSettings->stornoBenachrichtigungsMailAnPlanMa ?? false)
      && $kurs->hpm !== '';
  }

  protected function shouldSendToEinrichtung(\Kurs $kurs)
  {
    return !$this->shouldSendToVerwaltendenMitarbeiter($kurs)
        && !$this->shouldSendToPlanenendenMitarbeiter($kurs);
  }

  protected function shouldSendCopy(): bool
  {
    return $this->toolSettings->feedbackBenachrichtigungsMailAnDozent ?? false;
  }

  abstract protected function getKursnummer();

  abstract protected function getAction(): string;
}

class FeedbackFormularRequest
{
  public $knr;
}

class FeedbackFormularResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  public $kurs;
  public $menuPunkte;
  public $user;
  public $action;
  public $sendEMailToVerwMitarbeiter;
  public $sendEMailToPlanendMitarbeiter;
  public $sendEMailToEinrichtung;
  public $sendEMailToDozent;
  public $vhsname;
}