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

namespace Kufer\KuferTools\Basis\GebaeudeGrafik;

abstract class GebaeudeGrafikUseCase extends \Kufer\KuferTools\Basis\UseCase\ToolUseCase
{
  
  protected $gebaeudegrafik;

  public function __construct($request, $response = null)
  {
    parent::__construct($request, $response ?? new GebaeudeGrafikResponse());

    $this->gebaeudegrafik = new \Kufer\KuferTools\Utils\GebaeudeGrafik\GebauedeGrafik(
      $this->getToolId(),
      $this->toolSettings,
      (($this->request->startTime ?? '') !== '') ? $this->request->startTime : ($this->toolSettings->gebaeudeGrafikStartUhrzeit ?? ''),
      (($this->request->endTime ?? '') !== '') ? $this->request->endTime : ($this->toolSettings->gebaeudeGrafikEndeUhrzeit ?? ''),
      $this->request->datVon,
      $this->request->dateForm,
      $this->request->markieren,
      $this->getUser(),
      $this->request->filterGebaeude
    );
  }

  public function action()
  {
    if (!$this->gebaeudegrafik->containsDataToDisplay()) {
      $this->response->noData = true;
    } else {
      $this->response->raumsteps = $this->gebaeudegrafik->setRaumPanelSettings();
    }

    return $this->getResponse();
  }

  abstract protected function getKurse4GebaeudeGrafik();

  abstract protected function kannAufKursdetailsVerlinktWerden($kurstag, $kurseByNr = null);

  abstract protected function generateLink($kurstag, $kurseByNr = null);

  abstract protected function isKursDetailsAllowed();

  protected function getFunctionGenerateKursDetailsLink()
  {
    $kurseByNr = $this->getKurse4GebaeudeGrafik();

    return function($kurstag) use ($kurseByNr) {
      if ($this->kannAufKursdetailsVerlinktWerden($kurstag, $kurseByNr)) {
        return $this->generateLink($kurstag, $kurseByNr);
      }

      return '';
    };
  }

  protected function getFunctionShowBemerkungenInsteadOfKursTitel()
  {
    return function($kurstag) {
      return \DBWorkBASysSettings::isIHK() && \StringOperations::startsWith($kurstag->knr, '1000');
    };
  }

  public function getPlanungsLink()
  {
    return 'index.php?tool_id=' . $this->getToolId() . '&action=veranstaltungsPlanung&rnr=';
  }

  protected function getResponse()
  {
    $user = $this->getUser();
    $menuBar = new \MenuBar();
    $menuBar->setMenuPunkte4User($user);
    $this->response->user = $user;
    $this->response->menuPunkte = $menuBar->getMenuPunkte();
    $this->response->kursdetailslinkFunction = $this->getFunctionGenerateKursDetailsLink();
    $this->response->medienanzeigen = $this->toolSettings->medienanzeigen1;
    
    $this->response->showDozent = $this->toolSettings->showDozentInGebaeudeGrafik;
    $this->response->showKursNr = $this->toolSettings->showKursNrInGebaeudeGrafik;
    $this->response->showKursTitel = $this->toolSettings->showKursTitelInGebaeudeGrafik;
    $this->response->showBemerkungenInsteadOfKursTitelFunction = $this->getFunctionShowBemerkungenInsteadOfKursTitel();

    return $this->gebaeudegrafik->fillResponseObject($this->response, $user);
  }
}

class GebaeudeGrafikRequest
{
  public $datVon;
  public $format;
  public $startTime;
  public $endTime;
  public $dateForm;
  public $markieren;
  public $filterGebaeude;
}

class GebaeudeGrafikResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  public $user;
  public $menuPunkte;

  public $datVon;
  public $uhrVon;
  public $uhrBis;
  public $vhsname;
  public $gebaeude;

  public $raumsteps;
  public $timesteps;

  public $kursdetailslinkFunction;
  public $medienanzeigen;
  public $farbeLegends;
  public $farbeLegendsHeader = '';
  public $noData = false;
  public $showBemerkungenInsteadOfKursTitelFunction;

  public $filterGebaeude;
}