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

namespace Kufer\KuferTools\Basis\Kursdetails\DetailsReiter\FeedbackBogen;

use Kufer\KuferTools\Utils\FeedbackHelper;

abstract class SendeFeedbackFragenUseCase extends \Kufer\KuferTools\Basis\UseCase\EinzelKursUseCase
{
  public const FEEDBACKBOGEN_SEND_SUCCESS = 'feedBackBogenVersandErfolgreich';
  public const FEEDBACKBOGEN_SEND_ERROR = 'feedBackBogenVersandFehler';
  public const ERROR_BROWSER_REFRESH = 'browserRefresh';

  
  protected $userNr;

  
  
  protected $doAnonym = false;

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

  abstract protected function getRechteKey();

  abstract protected function getBogenTyp();

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

    return true;
  }

  public function action()
  {
    $kurs = $this->getKurs($this->request->knr);
    $user = $this->getUser();
    $statusCode = $this->handleFeedbackAbsenden();

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

  protected function shouldOnlyUseWebDatabase(): bool
  {
    $connInfo = new \DBConnectionInfo();
    $tool0Settings = \ToolSettingsAPI::getToolSettings(0);

    return ($tool0Settings->useFeedbackOnlyInReplikatsDb ?? false)
      && $connInfo->db_replikat
      && $connInfo->db_typ == \DBTypeEnum::MYSQLI;
  }

  
  protected function handleFeedbackAbsenden()
  {
    if (!$this->request->isRefresh) {
      $feedbackBogen = \SerializeOperations::unserialize(\SessionOperations::getVarValueWTIntern('feedback_Bogen'));
      $bogenId = $feedbackBogen->id;
      if ($this->request->isPressedButtonAbsenden) {
        
        
        $feedbackCode = $this->createFeedbackCode($bogenId);
        if ($feedbackCode === false) {
          return self::FEEDBACKBOGEN_SEND_ERROR;
        }

        $userNr = $this->doAnonym ? -1 : $this->userNr;

        $feedBackAbgesendet = \FeedBackAPI::sendFeedBack(
          $bogenId,
          $this->request->knr,
          \SessionOperations::getVarValueWTIntern('feedback_Antworten'),
          $userNr,
          $feedbackCode,
          $this->getBogenTyp(),
          $this->shouldOnlyUseWebDatabase() ? \FeedBackAPI::FEEDBACK_DATA_ONLY_WEB : \FeedbackAPI::FEEDBACK_DATA_DEFAULT
        );
        if ($feedBackAbgesendet) {
          $this->resetSessionVars();

          return self::FEEDBACKBOGEN_SEND_SUCCESS;
        } else {
          return self::FEEDBACKBOGEN_SEND_ERROR;
        }
      } 
      else {
        \Logger::logg('Fehler beim Absenden des Feedbackbogens: Der Buttonclick zum Senden wurde manipuliert. Antworten aus dem Feedbackbogen wurden nicht gespeichert.', 50);

        return self::FEEDBACKBOGEN_SEND_ERROR;
      }
    } 
    
    
    else {
      return self::ERROR_BROWSER_REFRESH;
    }
  }

  

  
  protected function createFeedbackCode($pBogenId)
  {
    return new \FeedBackCode();
  }

  protected function getResponse($kurs, $pStatusCode, $pUser)
  {
    $this->handleResponseStrategies();
    $this->response->kurs = $kurs;

    $this->response->statusCode = $pStatusCode;
    $this->response->backLink = $this->getBackLink();

    return $this->response;
  }

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

  protected function getBackLink()
  {
    return 'index.php?tool_id=' . $this->getToolId() . '&amp;action=kursdetails&amp;knr=' . $this->request->knr;
  }

  protected function resetSessionVars()
  {
    FeedbackHelper::resetFeedbackSessionVars();
  }
}

class SendeFeedbackFragenRequest 
{
  public $knr;
  public $isRefresh;
  public $isXsrfOk;
}

class SendeFeedbackFragenResponse
{
  public $kurs;
  public $statusCode;
  public $user;
  public $backLink;
}