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

namespace Kufer\Web\KuferTools\Ausgaben\SammelEMail;

use Kufer\Web\KuferTools\Ausgaben\CommonTasks\SendPrepare;

abstract class SammelEMailUseCase extends \Kufer\Web\KuferTools\Basis\UseCase\EinzelKursUseCase
{
  protected $kontaktUser;
  protected $sendPreparer;

  public function __construct($request)
  {
    parent::__construct($request, new SammelEMailResponse());
    $this->sendPreparer = $this->createSendPreparer();
    $this->kontaktUser = array();
  }

  protected function createSendPreparer(): SendPrepare
  {
    return new SendPrepare();
  }

  public function action()
  {
    $errorMessage = '';
    try {
      $kurs = $this->getKurs($this->request->knr);
      $this->kontaktUser = $this->getKontakte();
    } catch (\Exception $e) {
      $errorMessage = $e->getMessage();
    }

    return $this->getResponse($kurs ?? null, $errorMessage);
  }

  
  abstract protected function getKontakte(): array;

  abstract protected function getKontakteResponsePart(): void;

  abstract protected function getRechteKey(): string;

  protected function getResponse($pKurs, $pErrorMessage = '')
  {
    $this->handleResponseStrategies();
    $this->response->kurs = $pKurs;
    $this->response->errorMessage = $pErrorMessage;
    
    $this->response->sendMail2Einrichtung = ($this->toolSettings->sendMail2Einrichtung ?? false) || $this->isSetEinrichtungToolSettings();
    $this->response->sendMail2PlanendenMitarbeiter = $this->toolSettings->sendMail2PlanendenMitarbeiter ?? false;
    $this->response->sendMail2VerwaltendenMitarbeiter = $this->toolSettings->sendMail2VerwaltendenMitarbeiter ?? false;
    $this->response->sendMail2VerantwortendenMitarbeiter = $this->toolSettings->sendMail2VerantwortendenMitarbeiter ?? false;
    $this->response->sendMail2Absender = $this->isSetAbsenderToolsettings();
    $this->response->showEMail = $this->isShowingEMails();

    
    $this->getKontakteResponsePart();

    $this->response->emailInhalt = $this->getEmailInhalt();

    return $this->response;
  }

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

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

    if (!$this->isRechtAllowed($this->getRechteKey())) {
      return false;
    }

    return true;
  }

  
  protected function isSetAbsenderToolsettings(): bool
  {
    switch ($this->getToolId()) {
      case \ToolsAPI::TOOL_KURSUEBERSICHT_DOZ:
        return $this->toolSettings->sendMail2Dozent ?? false;
      case \ToolsAPI::TOOL_KURSPLANUNG_MA:
        return $this->toolSettings->sendMail2Ma ?? false;
      case \ToolsAPI::TOOL_KURSUEBERSICHT_FA:
        return $this->toolSettings->sendMail2Fa ?? false;
    }

    return false;
  }

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

  protected function getEmailInhalt(): string
  {
    return $this->request->emailVorbelegen ? $this->request->emailVorbelegen->getInhalt() : '';
  }

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