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

namespace Kufer\KuferTools\Firma\MitarbeiterUebersicht\TerminAdministratorBearbeiten;

class TerminAdministratorAendernUseCase extends \Kufer\KuferTools\Basis\UseCase\ToolUseCase
{
  public const TERMIN_ADMIN_AENDERN_SUCCESS = 'TerminAdminAendernSuccess';
  public const TERMIN_ADMIN_AENDERN_ERROR = 'TerminAdminAendernError';
  public const TERMIN_ADMIN_AENDERN_REFRESH = 'TerminAdminAendernRefresh';

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

  public static function getToolSectionId()
  {
    return 314;
  }

  public static function getToolId()
  {
    return \ToolsAPI::TOOL_MITARBEITERUEBERSICHT_FA; 
  }

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

    return true;
  }

  public function action()
  {
    $xsrfNew = '';
    $statusCode = self::TERMIN_ADMIN_AENDERN_REFRESH;

    if (!$this->request->isRefresh) {
      $xsrfNew = $this->getXsrfNew();
      $statusCode = $this->handleSetOrUnsetAdmin($this->request->belegId, $this->request->setAdmin);
    }

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

  
  protected function handleSetOrUnsetAdmin($pBelegId, $pSetAdmin)
  {
    $dbwBelegung = new \DBWorkBelegung(1, true);
    $origBelegung = $dbwBelegung->getBelegung($pBelegId);
    $changedBelegung = $this->getChangedBelegung($origBelegung, $pSetAdmin);

    return $this->saveBelegung($origBelegung, $changedBelegung);
  }

  
  protected function getChangedBelegung($pOrigBelegung, $pSetAdmin)
  {
    $changedBelegung = clone($pOrigBelegung);
    \Kufer\KuferTools\Utils\Teilnehmer\TeilnehmerMuenchenInlingua::setBelegungTerminadmistrator($changedBelegung, $pSetAdmin);

    return $changedBelegung;
  }

  
  private function saveBelegung($origBelegung, $changedBelegung)
  {
    $success = \BelegungAPI::updateBelegung($changedBelegung, $origBelegung);
    if ($success) {
      $statusCode = self::TERMIN_ADMIN_AENDERN_SUCCESS;
    } else {
      $statusCode = self::TERMIN_ADMIN_AENDERN_ERROR;
    }

    return $statusCode;
  }

  
  protected function getXsrfNew()
  {
    $xsrf = new \Xsrf();
    $key = $xsrf->generateKey4Action($this->request->action, $this->request->belegId);

    return \CleanOperations::cleanStringForHtml($key);
  }

  protected function getResponse($pStatusCode, $pXsrfNew)
  {
    $this->response->statusCode = $pStatusCode;
    $this->response->setAdmin = $this->request->setAdmin;
    $this->response->xsrfNew = $pXsrfNew;

    return $this->response;
  }
}

class TerminAdministratorBearbeitenRequest
{
  public $belegId;
  public $setAdmin;
  public $action;
  public $isRefresh;
  public $isXsrfOk;
}

class TerminAdministratorBearbeitenResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  public $statusCode;
  public $setAdmin;
  public $xsrfNew;
}