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

namespace Kufer\KuferTools\Basis\Kursdetails\DetailsReiter\Termine\TerminVerlaengernDialog\Wunsch;

use Kufer\KuferTools\Firma\KursUebersicht\KursUebersicht\Detailsreiter\Termine\TerminStornierenDialog\TerminStornierenUtils;

abstract class TerminVerlaengernAbsendenUseCase extends \Kufer\KuferTools\Basis\UseCase\ToolUseCase
{
  
  protected $statusMessageContainer;

  public function __construct($request, $response = null)
  {
    parent::__construct($request, $response ?? new TerminVerlaengernAbsendenResponse());
    $this->statusMessageContainer = \Kufer\KuferTools\Utils\StatusMessages\StatusMessageContainer::getSharedInstance();
  }

  public function hasAccess($pToolSectionId = null)
  {
    if (!parent::hasAccess($pToolSectionId)) {
      return false;
    }
    if (!\DBWorkBASysSettings::isInlinguaMuenchen()) {
      return false;
    }
    if (!$this->request->isXsrfOk && !$this->request->isRefresh) {
      return false;
    }
    if (!$this->hasAccessToKurstag() || $this->canAutoCreateNewKurstag()) {
      return false;
    }

    return true;
  }

  
  abstract protected function createPresenter($response);

  public function action()
  {
    $responseForEMail = $this->getResponseForEMail();
    $presenter = $this->createPresenter($responseForEMail);

    $email = new \Kufer\Mail\EMail\EMailHTML($presenter->getBetreff(), $presenter->present());
    $sender = \Kufer\Mail\Sender\EMailSenderFactory::create();
    $result = $sender->sendFromPersonToPerson($email, \SessionOperations::getCurrentWebToolsPerson(), $this->getEMailRecipients(), 'Firma');
    if (empty($result->getRecipients())) {
      
      $result = $sender->sendFromToolsUserToEinrichtung($email, $this->getUser());
    }

    $this->response->success = $result->hasSentAnyMails();
    if (!$result->hasSentAnyMails()) {
      foreach ($result->getRecipients() as $recipient) {
        $this->statusMessageContainer->addStatusMessageObj(\Kufer\KuferTools\Utils\StatusMessages\StatusMessage::getErrorMessageObj($recipient->getReadableMessageWithRecipientName()));
      }
    } else {
      $msg = 'Eine E-Mail wurde erfolgreich an die Einrichtung versendet';
      $this->statusMessageContainer->addStatusMessageObj(\Kufer\KuferTools\Utils\StatusMessages\StatusMessage::getSuccessMessageObj($msg));
    }

    return $this->response;
  }

  protected function getResponseForEMail()
  {
    $dbwKurse = new \DBWorkKurse();
    $this->response->kurs = $dbwKurse->getKurs($this->request->knr);
    $this->response->toolId = $this->getToolId();

    return $this->response;
  }

  
  protected function getEMailRecipients(): array
  {
    $result = array();

    if (\DBWorkBASysSettings::isInlinguaMuenchen()) {
      $firmenToolSettings = \ToolSettingsAPI::getToolSettings(\ToolsAPI::TOOL_KURSUEBERSICHT_FA);
      $emailPlanung = $firmenToolSettings->terminStonierenEMailPlanung;
      if ($emailPlanung !== '') {
        $result[] = new \Kufer\Mail\Address\EMailAddress('Kursplanung', $emailPlanung);
      }

      $emailFirmendienst = $firmenToolSettings->terminStonierenEMailFirmendienst;
      if ($emailFirmendienst !== '') {
        $result[] = new \Kufer\Mail\Address\EMailAddress('Firmendienst', $emailFirmendienst);
      }
    }

    return $result;
  }

  protected function canAutoCreateNewKurstag(): bool
  {
    return TerminStornierenUtils::canAutoCreateNewKurstag(
      new \DBWorkKurseTage(),
      $this->request->knr
    );
  }

  abstract protected function hasAccessToKurstag(): bool;
}

class TerminVerlaengernAbsendenRequest
{
  public $knr;
  public $isRefresh;
  public $isXsrfOk;
  public $belegId;
}

class TerminVerlaengernAbsendenResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  public $kurs;
  public $success;
}