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


namespace Kufer\KuferTools\Hausmeister\Raumbelegung\MedienUebersicht;

class MedienUseCase extends \Kufer\KuferTools\Basis\UseCase\ToolUseCase
{
  private $dbwkurstage;
  private $dbwKursorte;
  private $zugeordneteGebaeudeNamen;
  private $medienuebersicht;

  public function __construct($pRequest, $pDbwKurstage, $pDbwKursorte)
  {
    parent::__construct($pRequest, new MedienUebersichtResponse());
    $this->dbwkurstage = $pDbwKurstage;
    $this->dbwKursorte = $pDbwKursorte;

    $this->medienuebersicht = new \Kufer\KuferTools\Hausmeister\Raumbelegung\MedienUebersicht\MedienUebersicht();
  }

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

  

  public function action()
  {
    $user = $this->getUser();

    $this->zugeordneteGebaeudeNamen = $this->getGebaeude4User($user);

    $kurstage = $this->medienuebersicht->getKurstageMitMedienOderVerpflegung($this->request->vonDatum, $this->request->bisDatum);

    return $this->getResponse($user, $kurstage);
  }

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

  public static function getToolSectionId()
  {
    return 561;
  }

  
  private function getGebaeude4User($pUser)
  {
    if (empty($this->dbwKursorte->fromobject) && empty($this->dbwKursorte->toobject)) {
      $this->dbwKursorte->newSearch();
    }
    
    $nurzugeordneteorte = isset($this->toolSettings->nurzugeordneteorte) ? $this->toolSettings->nurzugeordneteorte : false;
    $raumVerfuegbarkeit = isset($this->toolSettings->raumNichtVerfuegbar) ? $this->toolSettings->raumNichtVerfuegbar : false;

    return $this->dbwKursorte->getRelevanteGebaude($pUser->id, $pUser->admin, $nurzugeordneteorte, $raumVerfuegbarkeit);
  }

  protected function getResponse($pUser, $pKurstage)
  {
    $columns = $this->getColumns();
    $this->response->kurstage = $pKurstage;
    $this->response->user = $pUser;
    $this->response->columns = $columns;
    $this->response->vonDatum = $this->request->vonDatum;
    $this->response->bisDatum = $this->request->bisDatum;
    
    \Kufer\KuferTools\Utils\Referrer::aktualisiereReferrerInSession($this->getToolId(), $this->request->referer, $this->request->refererLabel, 'medien_ref');
    $this->response->referer = \Kufer\KuferTools\Utils\Referrer::getReferrerFromSession($this->getToolId(), 'index.php?tool_id=5', 'medien_ref');
    $this->response->refererLabel = \Kufer\KuferTools\Utils\Referrer::getReferrerLabelFromSession($this->getToolId(), 'Zurück', 'medien_ref');
    
    $this->response->zugeordneteGebaeudeNamen = $this->zugeordneteGebaeudeNamen;

    $this->setDatumsWerte2Response();
    $menuBar = new \MenuBar();
    $menuBar->setMenuPunkte4User($pUser);
    $this->response->menuPunkte = $menuBar->getMenuPunkte();

    return $this->response;
  }

  private function setDatumsWerte2Response()
  {
    $aktDat = \DateTimeOperations::getDateNow();
    $this->response->heuteDatum = $aktDat;
    $this->response->morgenDatum = \DateTimeOperations::getNextDate($aktDat, 1);
    
    $this->response->beginDieseWocheDatum = \DateTimeOperations::getFirstDayOfWeek($aktDat);
    $this->response->endDieseWocheDatum = \DateTimeOperations::getNextDate($this->response->beginDieseWocheDatum, 6);
    $this->response->beginNaechsteWocheDatum = \DateTimeOperations::getNextDate($this->response->beginDieseWocheDatum, 7);
    $this->response->endNaechsteWocheDatum = \DateTimeOperations::getNextDate($this->response->endDieseWocheDatum, 7);
  }
}

class MedienUebersichtRequest
{
  public $vonDatum;
  public $bisDatum;
  public $referer;
  public $refererLabel;
}

class MedienUebersichtResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  public $kurstage;
  public $vonDatum;
  public $bisDatum;
  public $columns;
  public $user;
  public $buttons;
  public $menuPunkte;
  public $heuteDatum;
  public $morgenDatum;
  public $endDieseWocheDatum;
  public $beginNaechsteWocheDatum;
  public $endNaechsteWocheDatum;
  public $referer;
  public $refererLabel;
  public $zugeordneteGebaeudeNamen;
}