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

namespace Kufer\KuferTools\Hausmeister\Raumreservierung;

class FormularAbsendenUseCase extends \Kufer\KuferTools\Basis\UseCase\ToolUseCase
{
  private $dbwKursorte;
  const RESERVIERUNG_ERFOLGREICH_VERSENDET = 'success';
  const UEBERMITTLUNG_FEHLGESCHLAGEN = 'failed';

  public function __construct($request)
  {
    parent::__construct($request, new FormularAbsendenResponse());
    $this->dbwKursorte = new \DBWorkKursorte(1, true);
  }

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

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

  public static function getToolSectionId()
  {
    return 448;
  }

  public function action()
  {
    \SessionOperations::setVarValueWTIntern('raumres_absender', $this->request->absender);
    $statusCode = $this->sendEmail();

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

  private function sendEmail()
  {
    $ansprechpartnerAry = \JsonOperations::getFromStringBase64($this->toolSettings->ansprechpartner);
    $empfaenger = $ansprechpartnerAry[$this->request->empfaengerId];
    $raeume = $this->dbwKursorte->getKursOrteByIds($this->request->raumAry);
    $hausmeister = \SessionOperations::getCurrentWebToolsHausmeister();

    
    $emailBody = 'Datum:' . "\r\n";
    $emailBody .= $this->request->datum . "\r\n\r\n";

    $emailBody .= 'Uhrzeit von - bis:' . "\r\n";
    $emailBody .= $this->request->uhrVon . ' - ' . $this->request->uhrBis . "\r\n\r\n";

    $emailBody .= 'Raum/Räume:' . "\r\n";
    foreach ($raeume as $raum) {
      $emailBody .= $raum->mktext . "\r\n";
    }
    $emailBody .= "\r\n";

    $emailBody .= 'Kostenstelle:' . "\r\n";
    $emailBody .= $this->request->kostenstelle . "\r\n\r\n";

    $emailBody .= 'Kostenträger:' . "\r\n";
    $emailBody .= $this->request->kostentraeger . "\r\n\r\n";

    $emailBody .= 'Zweck:' . "\r\n";
    $emailBody .= $this->request->zweck . "\r\n\r\n";

    if ($this->request->ausstattung !== '') {
      $emailBody .= 'Ausstattung:' . "\r\n";
      $emailBody .= $this->request->ausstattung . "\r\n\r\n";
    }

    $emailBody .= 'Absender-E-Mail-Adresse:' . "\r\n";
    $emailBody .= $this->request->absender . "\r\n\r\n";

    
    $emailTitle = 'Raumreservierung aus den KuferTools';
    $mailAdress = $empfaenger->email;
    $user = \SessionOperations::getCurrentWebToolsUser();

    $email = new \Kufer\Mail\EMail\EMailPlaintext($emailTitle, $emailBody);
    $email->addKuferToolsFooter('Raumreservierung', $user);

    $emailSender = \Kufer\Mail\Sender\EMailSenderFactory::create();
    $emailEinrichtungRecipient = new \Kufer\Mail\Sender\Settings\Recipient\Einrichtung\EMailEinrichtungRecipientCustom($mailAdress);

    if ($emailSender->sendFromAdresseToEinrichtung($email, $hausmeister, $emailEinrichtungRecipient)->hasSentAnyMails()) {
      return self::RESERVIERUNG_ERFOLGREICH_VERSENDET;
    } else {
      return self::UEBERMITTLUNG_FEHLGESCHLAGEN;
    }
  }

  protected function getResponse($pStatusCode)
  {
    $this->handleResponseStrategies();
    $this->response->statusCode = $pStatusCode;

    return $this->response;
  }

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

class FormularAbsendenRequest
{
  public $datum;
  public $uhrVon;
  public $uhrBis;
  public $raumAry;
  public $kostenstelle;
  public $kostentraeger;
  public $zweck;
  public $ausstattung;
  public $empfaengerId;
  public $absender;
  public $isRefresh;
  public $isXsrfOk;
}

class FormularAbsendenResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  public $statusCode;
  public $menuPunkte;
  public $user;
}