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

namespace Kufer\KuferTools\Basis\Ics;

abstract class IcsKalenderUseCase extends \Kufer\KuferTools\Basis\UseCase\ToolUseCase
{
  protected $kursHelper;
  protected $belegungen;

  public function __construct($request)
  {
    parent::__construct($request, new IcsAuswahlResponse());
    $this->belegungen = $this->getBelegungen();
  }

  protected function getBelegungen()
  {
    $user = $this->getUser();
    $dbwBelegung = new \DBWorkBelegung(1, true);

    return $dbwBelegung->getBelegungenbyTeilnehmerNr($user->tnr);
  }

  protected function getKursHelper()
  {
    return \Kufer\KuferTools\Utils\DatamodulHelper\KursHelper\KursHelperFactory::getKursHelperByToolID($this->getToolId(), $this->toolSettings);
  }

  public function hasAccess($pToolSectionId = null)
  {
    if (!parent::hasAccess()) {
      return false;
    }

    return $this->areIdsAllowed();
  }

  protected function areIdsAllowed()
  {
    $allowedKnrs = $this->getAllowedKursnummern();
    foreach ($this->request->kursnummern as $knr) {
      if (!in_array($knr, $allowedKnrs)) {
        return false;
      }
    }

    return true;
  }

  abstract protected function getAllowedKursnummern();

  
  public function action()
  {
    return $this->getResponse();
  }

  abstract protected function getKursTage();

  protected function additionalCheckVonBisDat()
  {
    return false;
  }

  
  protected function getResponse()
  {
    $this->response->kurstage = $this->getKursTage();
    $this->response->timestamp = \StringOperations::formatDateForWebTeam(\DateTimeOperations::getDateNow());
    $this->response->vondat = $this->request->vondat;
    $this->response->bisdat = $this->request->bisdat;

    return $this->response;
  }
}

class IcsKalenderRequest
{
  public $terminbereich;
  public $vondat;
  public $bisdat;
  public $kursnummern;
  public $auchTermineAndererDozenten;
  public $terminnummern;
}

class IcsKalenderResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  public $kursnummern;
  public $timestamp;
  public $kurstage;
  public $vondat;
  public $bisdat;
}