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

namespace Kufer\KuferTools\Basis\PdfListen\PrintPdfListen;


abstract class PrintPdfListenBySettingsUseCase extends PrintPdfListenUseCase
{
  
  protected $druckSettingsError;

  public function __construct($pRequest)
  {
    
    parent::__construct($pRequest);
    $this->druckSettingsError = new DruckSettingsErrorHandler();
  }

  
  

  public function action()
  {
    
    if (!$this->_printAllowed4User) {
      $fehlerText = ' Keine Zugriffsberechtigung auf einen Kurs oder mehrere Kurse für Liste ' . $this->request->listName . '.';
      \Logger::logg($fehlerText, 120);
    } else {
      
      
      try {
        $this->listObj->loadSettingsFromReqObj($this->request->postSettings);
        
      } catch (\Exception $ex) {
        

        $this->druckSettingsError->handleError($ex->getMessage(), 51);
      }
    }

    $kursIds = explode(':', $this->request->id);
    $pdfListeObj = $this->_getPdfDrucklistenObj();
    $user = $this->getUser();

    if (($this->getSendInfoSuccessFromSession($kursIds) != true) && ($this->sollInfoAutomatischAnEinrichtungGesendetWerden())) {
      $nachrichtAnvhsVersenden = new \Kufer\KuferTools\Dozent\KursUebersicht\PdfListen\DruckListenAllgemein\NachrichtAnVHSVersenden();

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

    return $this->getResponse();
  }

  

  
  protected function getResponse()
  {
    $this->response = parent::getResponse();
    $this->druckSettingsError->setPostError2Response($this->response);

    return $this->response;
  }

  
  protected function getPrintListenResponse()
  {
    return new PrintPdfListenBySettingsResponse();
  }

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

  private 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);
  }

  private 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 PrintPdfListenBySettingsRequest
{
  public $id;
  public $listName;
  public $dnr;
  
  public $postSettings;
}


class PrintPdfListenBySettingsResponse extends PrintPdfListenResponse
{
  public $postError;
  public $postErrorMessage;
  public $sendInfoSuccess;
}



class DruckSettingsErrorHandler
{
  private $_postError;
  private $_postErrorMessage;

  public function __construct()
  {
    $this->_postError = false;
    $this->_postErrorMessage = '';
  }

  public function handleError($pErrorMessage, $pLogCode)
  {
    \Logger::logg($pErrorMessage, $pLogCode); 
    $this->_postError = true;
    $this->_postErrorMessage = $pErrorMessage;
  }

  public function setPostError2Response($pResponse)
  {
    $pResponse->postError = $this->_postError;
    $pResponse->postErrorMessage = $this->_postErrorMessage;
  }
}