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

namespace Kufer\KuferTools\Mitarbeiter\KursUebersicht\Detailsreiter\Teilnehmer\Mitveranstalter;

class SaveTeilnehmerUseCase extends \Kufer\KuferTools\Basis\UseCase\EinzelKursUseCase
{
  public function __construct($requestObj)
  {
    parent::__construct($requestObj, new TeilnehmerUseCaseResponse());
  }

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

  public static function getToolSectionId()
  {
    return 242;
  }

  protected function getMainReiterId()
  {
    return 377;
  }

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

    return true;
  }

  public function action()
  {
    if ($this->request->isRefresh) {
      return $this->getResponse(null);
    }

    $mitveranstalterApi = new \MitveranstalterAPI();
    $success = $mitveranstalterApi->changeTnCount(
      $this->request->knr,
      $this->request->maennlichInnsbruckChange,
      $this->request->maennlichAuswaertsChange,
      $this->request->weiblichInnsbruckChange,
      $this->request->weiblichAuswaertsChange
    );

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

  private function getResponse($success)
  {
    $this->response->success = $success;
    $this->response->knr = $this->request->knr;

    return $this->response;
  }
}

class SaveTeilnehmerUseCaseRequest
{
  public $knr;
  public $maennlichInnsbruckChange;
  public $maennlichAuswaertsChange;
  public $weiblichInnsbruckChange;
  public $weiblichAuswaertsChange;

  public $isRefresh;
  public $isXsrfOk;
}

class SaveTeilnehmerUseCaseResponse
{
  public $knr;
  public $success;
}