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

namespace Kufer\KuferTools\Ausgaben\SammelEMail;

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

abstract class SammelEMailUseCase extends \Kufer\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();

  abstract protected function getKontakteResponsePart();

  abstract protected function getRechteKey();

  protected function getResponse($pKurs, $pErrorMessage = '')
  {
    $this->handleResponseStrategies();
    $this->response->kurs = $pKurs;
    $this->response->errorMessage = $pErrorMessage;
    
    $this->response->sendMail2Einrichtung = (isset($this->toolSettings->sendMail2Einrichtung) && $this->toolSettings->sendMail2Einrichtung != '') || $this->isSetEinrichtungToolSettings();
    $this->response->sendMail2PlanendenMitarbeiter = isset($this->toolSettings->sendMail2PlanendenMitarbeiter) && $this->toolSettings->sendMail2PlanendenMitarbeiter != '';
    $this->response->sendMail2VerwaltendenMitarbeiter = isset($this->toolSettings->sendMail2VerwaltendenMitarbeiter) && $this->toolSettings->sendMail2VerwaltendenMitarbeiter != '';
    $this->response->sendMail2Absender = $this->isSetAbsenderToolsettings();

    
    $this->getKontakteResponsePart();

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

    return $this->response;
  }

  protected function getResponseStrategies()
  {
    return array(
        new \Kufer\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()
  {
    switch ($this->getToolId()) {
      case \ToolsAPI::TOOL_KURSUEBERSICHT_DOZ:
        return isset($this->toolSettings->sendMail2Dozent) && !empty($this->toolSettings->sendMail2Dozent);
      case \ToolsAPI::TOOL_KURSPLANUNG_MA:
        return isset($this->toolSettings->sendMail2Ma) && !empty($this->toolSettings->sendMail2Ma);
      case \ToolsAPI::TOOL_KURSUEBERSICHT_FA:
        return isset($this->toolSettings->sendMail2Fa) && !empty($this->toolSettings->sendMail2Fa);
    }

    return false;
  }

  
  protected function isSetEinrichtungToolSettings()
  {
    return false;
  }

  protected function getEmailInhalt()
  {
    $result = '';
    if (isset($this->request->emailVorbelegen)) {
      $result .= $this->request->emailVorbelegen->getInhalt();
    }

    return $result;
  }
}

class SammelEMailResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  public $kurs;
  public $user;
  public $menuBar;
  public $errorMessage;
  public $sendMail2Einrichtung = false;
  public $sendMail2PlanendenMitarbeiter = false;
  public $sendMail2VerwaltendenMitarbeiter = false;
  public $sendMail2Absender = false;
  public $emailInhalt;
}