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

namespace Kufer\Web\KuferTools\Basis\Kursdetails\Detailsreiter\Termine\TerminVerlaengernDialog;

use Kufer\Web\KuferTools\Firma\KursUebersicht\Detailsreiter\Termine\TerminStornierenDialog\TerminStornierenUtils;


abstract class TerminVerlaengernDialogUseCase extends \Kufer\Web\KuferTools\Basis\UseCase\ToolUseCase
{
  
  protected $dbwKursetage;

  public function __construct($request)
  {
    parent::__construct($request, new TerminVerlaengernDialogResponse());
    $this->dbwKursetage = $this->getDbwKurstage();
  }

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

    return true;
  }

  public function action()
  {
    $autoCreateAllowed = $this->canAutoCreateNewKurstag();
    $datumNeueKurstage = null;
    if ($autoCreateAllowed) {
      $datumNeueKurstage = TerminStornierenUtils::getDatumNeueKurstageForExtension($this->request->knr);
    }

    return $this->getResponse($autoCreateAllowed, $datumNeueKurstage);
  }

  
  protected function getDbwKurstage()
  {
    return \DBWorkKurseTage::getDefaultInstance(1, true);
  }

  
  protected function getResponse($autoCreateAllowed, $datumNeueKurstag)
  {
    $this->response->belegId = $this->request->belegId;
    $this->response->knr = $this->request->knr;
    $this->response->fristErreicht = $autoCreateAllowed;
    $this->response->datumNeueKurstage = $datumNeueKurstag;
    $this->response->toolId = $this->getToolId();

    return $this->response;
  }

  protected function canAutoCreateNewKurstag(): bool
  {
    return TerminStornierenUtils::canAutoCreateNewKurstag(
      $this->dbwKursetage,
      $this->request->knr
    );
  }

  abstract protected function hasAccessToKurstag(): bool;
}

class TerminVerlaengernDialogRequest
{
  public $belegId;
  public $knr;
}

class TerminVerlaengernDialogResponse extends \Kufer\Web\KuferTools\Basis\UseCase\ToolResponse
{
  public $belegId;
  public $knr;
  public $fristErreicht;
  public $datumNeueKurstage;
  public $toolId;
}