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

namespace Kufer\Web\KuferTools\Allgemein\EinstellungenUser\ZeigeEinstellungen;

class ZeigeEinstellungenUseCase extends \Kufer\Web\KuferTools\Basis\UseCase\ToolUseCase
{
  public function __construct($request)
  {
    parent::__construct($request, new ZeigeEinstellungenResponse());
  }

  public function hasAccess($pToolSectionId = null)
  {
    $tool0settings = \ToolSettingsAPI::getToolSettings(0);

    return $tool0settings->showUserSettings;
  }

  public static function getToolId()
  {
    return -5;
  }

  public static function getToolSectionId()
  {
    return -5;
  }

  public function action()
  {
    $user = $this->getUser();
    $userSettings = $this->getUserSettings($user);

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

  protected function getResponse($userSettings)
  {
    $this->handleResponseStrategies();

    $this->response->showSortKurse = true;
    $this->response->showSortKurse2 = true;
    $this->response->showMoeglichstWenigeEmails = false;

    if (\DBWorkBASysSettings::isKornwestheimEbz()) {
      $this->response->showSortKurse = false;
      $this->response->showSortKurse2 = false;
      $this->response->showMoeglichstWenigeEmails = true;
    }

    if (\SessionOperations::isCurrentUserMitveranstalter()) {
      $tool8Settings = \ToolSettingsAPI::getToolSettings(8);
      $this->response->showSettingsAnmeldebestaetigung = true;
      $this->response->zahlungInfo = $tool8Settings->zahlung_info;
      $this->response->mailAnhangVorhanden = $tool8Settings->mail_anhang != '';
      $this->response->mailAnhangDateiname = $tool8Settings->mail_anhang_dateiname;
      $this->response->maxFileSize = $this->getMaxFileSize();
    }

    $this->response->userSettings = $userSettings;
    $this->response->statusCode = $this->request->statusCode;

    return $this->response;
  }

  protected function getMaxFileSize()
  {
    $tool0Settings = \ToolSettingsAPI::getToolSettings(0);
    $fileSize = \ConstantOperations::getMaxFileSize4Upload($tool0Settings->uploadmaxsize);

    return \MathOperations::getBytesUmrechnung($fileSize);
  }

  
  private function getUserSettings($user)
  {
    $dbwUserSettings = new \DBWorkWebUsersSettings(1, true);

    return $dbwUserSettings->getSettingsByUser($user->getIdentifier());
  }

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

class ZeigeEinstellungenRequest
{
  public $statusCode;
}

class ZeigeEinstellungenResponse extends \Kufer\Web\KuferTools\Basis\UseCase\ToolResponse
{
  public $user;
  public $menuPunkte = array();
  public $showSortKurse = false;
  public $showSortKurse2 = false;
  public $showMoeglichstWenigeEmails = false;
  public $showSettingsAnmeldebestaetigung = false;
  public $zahlungInfo = '';
  public $mailAnhangVorhanden = false;
  public $mailAnhangDateiname = '';
  public $maxFileSize = '';

  public $userSettings;
  public $statusCode;
}