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

namespace Kufer\KuferTools\Dozent\KursUebersicht\Detailsreiter\Termine\TerminAbsagen;

class SendTerminAbsagenUseCase extends \Kufer\KuferTools\Dozent\KursUebersicht\EinzelKursUseCase
{
  public function __construct($request)
  {
    parent::__construct($request, new SendTerminAbsagenResponse());
  }

  public static function getToolId()
  {
    return \ToolsAPI::TOOL_KURSUEBERSICHT_DOZ;
  }

  public static function getToolSectionId()
  {
    return 4;
  }

  public function hasAccess($pToolSectionId = null)
  {
    if (!parent::hasAccess()) {
      return false;
    } elseif (!$this->request->isXsrfOk && !$this->request->isRefresh) {
      return false;
    }

    return true;
  }

  public function action()
  {
    
    


    $kurs = $this->getKurs($this->request->knr);
    $kurstage = $this->getKurstage($kurs);
    if (!$this->request->isRefresh) {
      $emailSuccess = $this->sendEMail($kurs);
    } else {
      $emailSuccess = false;
    }

    return $this->getResponse($kurs, $kurstage, $emailSuccess);
  }

  
  private function sendEMail($kurs): bool
  {
    $dozent = \SessionOperations::getCurrentWebToolsDozent();

    $email = new \Kufer\Mail\EMail\EMailPlaintext(\StringOperations::html_entity_decode(\trim($this->request->emailBetreff)), $this->request->emailBody);
    $email->addKuferToolsFooter('Verlegung/Stornierung von Kurstagen', $this->getUser(), $kurs);

    $emailSender = \Kufer\Mail\Sender\EMailSenderFactory::create();
    $emailEinrichtungRecipient = new \Kufer\Mail\Sender\Settings\Recipient\Einrichtung\EMailEinrichtungRecipientToolsKursSettings($kurs);
    $emailEinrichtungRecipient
      ->setSettingMailToPlanendenMitarbeiter($this->toolSettings->terminstornierungsEmailPlanendMA)
      ->setSettingMailToVerwaltendenMitarbeiter($this->toolSettings->terminstornierungsEmailVerwaltendMA)
      ->setSettingsVhsEMail($this->toolSettings->vhsEmailTool1Section345);

    if ($this->toolSettings->sendMailTerminStornierungKopieAnDozent) {
      
      $emailSender->sendCopyToToolsUser($email, $this->getUser());
    }

    return $emailSender->sendFromDozentToEinrichtung($email, $dozent, $emailEinrichtungRecipient)->hasSentAnyMails();
  }

  private function getKurstage($kurs)
  {
    
    $dbwKurstage = new \DBWorkKurseTageWT(1, true);
    $onlyLoggedInDozent = true;
    $onlyDiffrentKt = false;
    $dbwKurstage->newSearch();
    $dbwKurstage->toobject->nodoztool = true;

    return $dbwKurstage->getKurstagebyKursNR($kurs->id, -1, $onlyLoggedInDozent, $onlyDiffrentKt);
  }

  protected function getResponse($kurs, $belegungen, $emailSuccess)
  {
    $this->handleResponseStrategies();
    $this->response->kurs = $kurs;
    $this->response->belegungen = $belegungen;
    $this->response->success = $emailSuccess;

    return $this->response;
  }

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

class SendTerminAbsagenRequest
{
  public $knr;
  public $emailBetreff;
  public $emailBody;
  public $isRefresh;
  public $isXsrfOk;
}

class SendTerminAbsagenResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  public $kurs;
  public $kurstage;
  public $user;
  public $menuBar;
  public $success;
}