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

namespace Kufer\KuferTools\Allgemein\Raumanzeige\Terminuebersicht;

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

  public static function getToolId()
  {
    return 19;
  }

  public static function getToolSectionId()
  {
    return 292;
  }

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

  private function getAlleKurstage()
  {
    $searchAPI = new \KurstagSucheAPI();
    $zentralSettings = \ToolSettingsAPI::getToolSettings(0);
    if (!$this->request->einstieg || !$zentralSettings->keepFilterSettings) {
      if (isset($this->request->searchParams) && !is_null($this->request->searchParams)) {
        $searchAPI->setSearchParamsToSession($this->request->searchParams);
      }
    }
    $kthelper = new \Kufer\KuferTools\Utils\DatamodulHelper\KursTageHelper\WasIstWoKurstage($this->toolSettings, $this->getUser());

    return $kthelper->getDatamoduleMitFilter($this->getToolId());
  }

  protected function isFilterGesetzt()
  {
    $suchApi = new \KurstagSucheAPI();

    return $suchApi->isFilterGesetzt($this->getToolId());
  }

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

    $kthelper = new \Kufer\KuferTools\Utils\DatamodulHelper\KursTageHelper\WasIstWoKurstage($this->toolSettings, $this->getUser());
    $dbwKursetage = $kthelper->getFilter($this->getToolId());

    $this->response->kurstage = $this->response->blaettern->getItemsOnCurrentPage();
    $this->response->datamodule = $this->response->kurstage;

    $this->response->datum_akt = $dbwKursetage->fromobject->datum;
    $this->response->datum_bis = $dbwKursetage->toobject->datum;
    $this->response->datum_vor = \DateTimeOperations::getDateDaysBefore($dbwKursetage->fromobject->datum, -1);
    $this->response->datum_zurueck = \DateTimeOperations::getDateDaysBefore($dbwKursetage->fromobject->datum, 1);

    $this->response->showFilterLeerenButton = $this->isFilterGesetzt();

    $this->response->uebersichtAlsTabelle = isset($this->toolSettings->kurstageUebersichtAlsTabelle) && $this->toolSettings->kurstageUebersichtAlsTabelle;

    return $this->response;
  }

  protected function getResponseStrategies()
  {
    return array(
        new \Kufer\KuferTools\Utils\ResponseStrategies\ResponseWss($this),
        new \Kufer\KuferTools\Utils\ResponseStrategies\ResponseMitRahmen($this),
        new \Kufer\KuferTools\Utils\ResponseStrategies\ResponseBlaettern($this, $this->getAlleKurstage(), $this->toolSettings->kurstageproseite, $this->request->seite)
    );
  }
}

class TerminUebersichtRequest
{
  public $seite = 1;
  
  public $einstieg;
  public $searchParams;
}

class TerminUebersichtResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  public $kurstage = array();
  public $datamodule = array();
  public $columns = array();
  public $menuPunkte;
  public $user;
  public $datum_akt;
  public $datum_bis;
  public $datum_vor;
  public $datum_zurueck;
  public $blaettern;
  public $showFilterLeerenButton;
  public $uebersichtAlsTabelle;

  public $panelButtonCallBacks = array();
  public $showCheckbox = false;
  public $verlinkePanelTitle = false;
}