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

namespace Kufer\Web\KuferTools\Veranstalter\KursUebersichtDerMitarbeiter\KursUebersicht\Detailsreiter\InfoAnDoz;

use Kufer\Mail\EMail\EMailPlaintext;
use Kufer\Mail\EMail\IEMail;
use Kufer\Mail\Sender\EMailSenderFactory;
use Kufer\Mail\Sender\Result\EMailSentResult;

class SendInfoAnDozUseCase extends \Kufer\Web\KuferTools\Basis\UseCase\EinzelKursUseCase
{
  public const EMAILVERSAND_SUCCESS = 0;
  public const EMAILVERSAND_FEHLER_BEIM_VERSENDEN = 1;
  public const EMAILVERSAND_UNGUELTIGE_ODER_LEERE_EMAIL = 2;

  public function __construct($request)
  {
    parent::__construct($request, new SendInfoAnDozResponse());
  }

  public static function getToolId()
  {
    return 61;
  }

  public static function getToolSectionId()
  {
    return 722;
  }

  public function hasAccess($pToolSectionId = null)
  {
    $sectId = $this->getToolSectionId();
    if (!\Kufer\Web\KuferTools\Basis\UseCase\ToolUseCase::hasAccess()) {
      return false;
    } elseif (!$this->isRechtAllowed($sectId . '.0.4')) {
      return false;
    } elseif (!$this->request->isXsrfOk && !$this->request->isRefresh) {
      return false;
    }

    return true;
  }

  protected function getAllowedIds()
  {
    $user = $this->getUser();
    $kurshelper = new \Kufer\Web\KuferTools\Utils\DatamodulHelper\Kurse\VeranstalterKurseDerMitarbeiter($this->toolSettings, $user);

    return $kurshelper->getIdsOhneFilter();
  }

  protected function getRequestedId()
  {
    return $this->request->knr;
  }

  public function action()
  {
    $user = $this->getUser();
    $kurshelper = new \Kufer\Web\KuferTools\Utils\DatamodulHelper\Kurse\VeranstalterKurseDerMitarbeiter($this->toolSettings, $user);
    $kurs = $kurshelper->getDatamodulById($this->request->knr);

    $dbwDozenten = new \DBWorkDozenten();
    $dozenten = $dbwDozenten->getDozentenByIDArray($this->request->dozentenIds);

    $this->response->emailStatus = new EMailSentResult(array());
    if ($this->request->isRefresh) {
      $this->response->doppeltVersandt = true;
    } else {
      $this->response->emailStatus = $this->sendInfoAnDoz($dozenten, $this->getUser(), $kurs);
    }

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

  private function sendInfoAnDoz($pDozenten, $user, $kurs)
  {
    return $this->versendeEmails($pDozenten, $user, $kurs);
  }

  protected function getResponse($kurs, $dozenten)
  {
    $this->handleResponseStrategies();
    $this->response->kurs = $kurs;
    $this->response->dozenten = $dozenten;
    $this->response->countDozenten = count($dozenten);

    return $this->response;
  }

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

  private function createEMail(\Benutzer $user, \Kurs $kurs): IEMail
  {
    $result = new EMailPlaintext($this->request->emailBetreff, $this->request->emailBody);
    $result->addKuferToolsFooter(
      'Information an ' . \SettingsFactory::getSettings('SprachSettingsObject')->Dozent['Plural_mit_Artikel']['Dativ'],
      $user,
      $kurs
    );

    return $result;
  }

  private function versendeEmails(array $pDozenten, \Benutzer $user, \Kurs $kurs): EMailSentResult
  {
    $email = $this->createEMail($user, $kurs);
    try {
      $person = \SessionOperations::getCurrentWebToolsDozent();
    } catch (\Exception $ex) {
      $person = \SessionOperations::getCurrentWebToolsPerson();
    }

    $sender = EMailSenderFactory::create();
    if ($person instanceof \Dozent) {
      $result = $sender->sendFromDozentToDozent($email, $person, $pDozenten);
    } else {
      $result = $sender->sendFromPersonToPerson(
        $email,
        $person,
        $pDozenten,
        \SettingsFactory::getSettings('SprachSettingsObject')->Dozent['Plural']['Nominativ']
      );
    }

    return $result;
  }
}

class SendInfoAnDozRequest
{
  public $knr;
  public $dozentenIds = array();
  public $emailBetreff;
  public $emailBody;

  public $isRefresh;
  public $isXsrfOk;
}

class SendInfoAnDozResponse extends \Kufer\Web\KuferTools\Basis\UseCase\ToolResponse
{
  public $menuPunkte;
  public $emailStatus;
  public $kurs;
  public $dozenten;
  public $user;
  public $menuBar;
  public $sentMailStatus = array();
  public $doppeltVersandt = false;
  public $countDozenten;
}