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

namespace Kufer\KuferTools\Basis\PdfListen\DruckListenAllgemein\DruckListen;


abstract class DruckListenAllgemeinUseCase extends \Kufer\KuferTools\Basis\PdfListen\PrintPdfListen\PrintPdfListenUseCase
{
  protected function getResponse()
  {
    $this->response = parent::getResponse();
    $kursIds = explode(':', $this->request->id);
    $pdfListeObj = $this->_getPdfDrucklistenObj();
    $user = $this->getUser();

    if (($this->getSendInfoSuccessFromSession($kursIds) != true) && ($this->sollInfoAutomatischAnEinrichtungGesendetWerden())) {
      $nachrichtAnvhsVersenden = $this->createNachrichtAnVHSVersenden();

      $this->response->sendInfoSuccess = $nachrichtAnvhsVersenden->getSendInfoAnVHS($this->toolSettings, $pdfListeObj, $this->request->listName, $kursIds, $user);
      $this->merkeNachrichtSchonVersendet($kursIds);
    }

    return $this->response;
  }

  abstract protected function createNachrichtAnVHSVersenden(): \Kufer\KuferTools\Basis\PdfListen\DruckListenAllgemein\NachrichtAnVHSVersenden;

  protected function sollInfoAutomatischAnEinrichtungGesendetWerden()
  {
    return isset($this->toolSettings->automatischeInfoAnEinrichtungBeiListendruck) && $this->toolSettings->automatischeInfoAnEinrichtungBeiListendruck;
  }

  protected function merkeNachrichtSchonVersendet($pNeueKursIds)
  {
    $alteKursIdsArray = \SessionOperations::getVarValueWTIntern('Kurseliste' . '_' . $this->request->listName, array());
    $MergeVonAlteUndNeueKursIDs = array_unique(array_merge($alteKursIdsArray, $pNeueKursIds));

    \SessionOperations::setVarValueWTIntern('Kurseliste' . '_' . $this->request->listName, $MergeVonAlteUndNeueKursIDs);
    \SessionOperations::setVarValueWTIntern('sendInfoSuccess' . '_' . $this->request->listName, $this->response->sendInfoSuccess);
  }

  protected function getSendInfoSuccessFromSession($pKursIds)
  {
    $result = false;
    $kurseFromSession = \SessionOperations::getVarValueWTIntern('Kurseliste' . '_' . $this->request->listName, array());
    $sendInfoSucessFromSession = \SessionOperations::getVarValueWTIntern('sendInfoSuccess' . '_' . $this->request->listName, false);

    if ($sendInfoSucessFromSession == false) {
      $result = false;
    } elseif (count(array_diff($pKursIds, $kurseFromSession)) === 0) {
      $result = true;
    }

    return $result;
  }
}


class DruckListenAllgemeinRequest
{
  public $id;
  public $listeName;
  public $sendInfoSuccess;
}

class DruckListenAllgemeinResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  public $sendInfoSuccess;
}