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

namespace Kufer\Web\KuferTools\Oeffentlich\DoLogin;

class DoLoginUseCase extends \Kufer\Web\KuferTools\Basis\UseCase\UseCase
{
  
  private $loginApi;

  public function __construct($request, $loginApi)
  {
    parent::__construct($request, new DoLoginResponse());
    $this->loginApi = $loginApi;
  }

  public function hasAccess($pToolSectionId = null)
  {
    return true;
  }

  private function getUser()
  {
    return \SessionOperations::getCurrentWebToolsUser();
  }

  public function action()
  {
    if ($this->request->isRefresh || (!PRAESENTATION && !$this->request->isXsrfOk)) {
      $success = !$this->getUser()->isEmpty();

      if ($success) {
        $this->handleBenutzergruppen($this->getUser(), true);
      }
    } else {
      $success = $this->doLogin($this->request->username, $this->request->password);

      if ($success) {
        $this->handleBenutzergruppen($this->getUser());
      }
    }

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

  private function handleBenutzergruppen($user, $isRefresh = false)
  {
    if (!$isRefresh) {
      $this->loginAsStammhoerer();
    }
  }

  private function loginAsStammhoerer()
  {
    $objsettings = \ToolSettingsAPI::getToolSettings(0);
    if ($objsettings->loginStammHoerer) {
      \SessionOperations::logInCurrentToolsTnAsStammhoerer();
    }
  }

  private function doLogin($username, $password)
  {
    if (!$this->checkCaptcha4Login()) {
      $this->merkeIpLoginVersuchFehlerhaft();

      return false;
    }

    try {
      $success = $this->loginApi->doWtLoginNew($username, $password);
    } catch (\Kufer\Web\KuferTools\Utils\LDAP\FailedToCreateLDAPUserException $ex) {
      $success = false;
    }

    if ($success !== true) { 
      $this->merkeIpLoginVersuchFehlerhaft();

      return false;
    }

    return true;
  }

  protected function getResponse($loginSucess)
  {
    $this->response->loginSuccess = $loginSucess;
    $this->setzeMenupunkte();
    $this->response->mitCaptcha = $this->mitCaptcha();

    $this->response->forward_to_tool = $this->request->forward_to_tool ?? null;
    $this->response->forward_to_id = $this->request->forward_to_id ?? null;

    return $this->response;
  }

  private function setzeMenupunkte()
  {
    $menuBar = new \MenuBar();
    $menuBar->setMenuBarFromSettings(array(), 'LOGIN', null, true, true);

    $this->response->menuPunkte = $menuBar->getMenuPunkte();
  }

  private function checkCaptcha4Login()
  {
    $tool0settings = \ToolSettingsAPI::getToolSettings(0);

    $dbwLoginVersuch = new \DBWorkToolsLoginError(1, true);
    if ($tool0settings->captchaLoginFehlerhaft && $dbwLoginVersuch->useCaptcha4CurrentIp()) {
      $captcha = \Kufer\Web\Classes\Captchas\CaptchaFactory::getCaptcha4KuferTools();
      if (!$captcha->isValid(null, true, 'tedit', true)) {
        return false;
      }
    }

    return true;
  }

  private function merkeIpLoginVersuchFehlerhaft()
  {
    $tool0settings = \ToolSettingsAPI::getToolSettings(0);
    if ($tool0settings->captchaLoginFehlerhaft) {
      $dbwLoginVersuch = new \DBWorkToolsLoginError(1, true);
      $dbwLoginVersuch->merkeFehlversuch4CurrentIp();
    }
  }

  private function mitCaptcha()
  {
    $tool0settings = \ToolSettingsAPI::getToolSettings(0);
    if ($tool0settings->captchaLoginFehlerhaft) {
      $dbwLoginVersuch = new \DBWorkToolsLoginError(1, true);

      return $dbwLoginVersuch->useCaptcha4CurrentIp(true);
    } else {
      return false;
    }
  }
}

class DoLoginRequest
{
  public $username;
  public $password;
  public $isRefresh;
  public $isXsrfOk;

  
  public $forward_to_tool;
  
  public $forward_to_id;
}

class DoLoginResponse extends \Kufer\Web\KuferTools\Basis\UseCase\BasisResponse
{
  public $menuPunkte;
  public $loginSuccess;
  public $mitCaptcha;

  
  public $forward_to_tool;
  
  public $forward_to_id;
}