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

namespace Kufer\KuferTools\Basis\Raumbelegung\Raumdetails;

use Kufer\KuferTools\Basis\Kursdetails\DetailsReiter\MerkeReiter\MerkeReiterUseCase as MerkeReiterUseCase;
use Kufer\Web\Classes\Dbfilter\KursorteSQLFilter;
use Kufer\Web\KuferTools\Utils\FilterSearch\KursorteUebersichtSearchFilter;
use Kufer\Web\KuferTools\Utils\FilterSettings\IFilterSettings;

abstract class RaumDetailsUseCase extends \Kufer\KuferTools\Basis\UseCase\ToolUseCase
{
  private $dbwKursorte;

  public function __construct($request)
  {
    parent::__construct($request, new RaumDetailsResponse());
    $this->dbwKursorte = new \DBWorkKursorte();
  }

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

    if (!is_numeric($this->request->raumNr) || !$this->hasAccesesToId($this->request->raumNr)) {
      return false;
    }

    return true;
  }

  public function action()
  {
    \Kufer\KuferTools\Utils\Referrer::resetReferrerInSession($this->getToolId());
    $user = $this->getUser();
    $kursort = $this->getKursort($this->request->raumNr);

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

  protected function getKursort($raumNr)
  {
    return $this->dbwKursorte->getKursort($raumNr);
  }

  abstract protected function createFilterSettings(): IFilterSettings;

  protected function getAlleIdsMitFilter(): array
  {
    $filter = KursorteSQLFilter::create();

    $filterSettings = $this->createFilterSettings();
    $filter = $filterSettings->applySettings($filter);

    $filterSearch = new KursorteUebersichtSearchFilter();

    $filterSearch->applySearch($filter, $filterSearch->getSearchMapFromSession($this->getToolId()));

    $dbwKursorte = \DBWorkKursorte::getDefaultInstance();
    $dbwKursorte->orderby = $this->toolSettings->sortraeume;
    $dbwKursorte->orderbyasc = $this->toolSettings->sortraeumeasc;

    return $dbwKursorte->findIdsByFilter($filter);
  }

  protected function hasAccesesToId(int $id): bool
  {
    $filter = KursorteSQLFilter::create();

    $filterSettings = $this->createFilterSettings();
    $filter = $filterSettings->applySettings($filter);

    $dbw = \DBWorkKursorte::getDefaultInstance();
    $filter->equalsId($id);

    return $dbw->existsByFilter($filter);
  }

  protected function getResponse($kursort, $user)
  {
    $this->handleResponseStrategies();
    $this->response->kursort = $kursort;
    $this->setBenachbarteRaeumeToResponse($kursort, $user);

    $this->response->showTermineButton = $this->isRechtAllowed('339.1.1');
    $this->response->showKurseButton = $this->isRechtAllowed('339.1.2');
    $this->response->showRaumdetailsButton = $this->isRechtAllowed('339.1.3');

    $this->response->reiter = $this->getReiter();
    $this->response->showFunktionenButton = $this->response->showTermineButton
            || $this->response->showKurseButton || $this->response->showRaumdetailsButton;

    $this->response->oeffneReiter = MerkeReiterUseCase::oeffneReiter($this->getToolId(), $this->getToolSectionId());
    $this->response->raumNr = $this->request->raumNr;
    $this->response->hlRaumartDialog = $this->getRaumartDialog();

    $this->response->reiterButtons = $this->getButtons();

    return $this->response;
  }

  protected function getResponseStrategies()
  {
    return array(
        new \Kufer\KuferTools\Utils\ResponseStrategies\ResponseMitRahmen($this)
    );
  }

  private function setBenachbarteRaeumeToResponse($kursort, $user)
  {
    $alleIds = $this->getAlleIdsMitFilter();
    $index = \array_search($kursort->id, $alleIds);

    if ($index !== false) {
      if (isset($alleIds[$index - 1])) {
        $this->response->previousRaum = $alleIds[$index - 1];
      }
      if (isset($alleIds[$index + 1])) {
        $this->response->nextRaum = $alleIds[$index + 1];
      }
    }
  }

  public function isRechtAllowed($pRecht, $pToolId = null)
  {
    return \SecurityOperations::privilegAllowedCurrentUser($this->getToolId(), $pRecht) >= 1;
  }

  protected function getButtons()
  {
    $buttons = new \Kufer\KuferTools\Basis\Kursdetails\DetailsReiter\RaumDetailsButtons($this, $this->request, $this->response);

    return $buttons->getButtons();
  }

  private function getReiter()
  {
    $result = array();
    $reiterBeschriftung = $this->getReiterBeschriftung();

    $reiterSeite = new \ReiterSeite($this->getToolSectionId(), $reiterBeschriftung);
    $reiterAry = $reiterSeite->getReiterKeysAry();

    foreach ($reiterAry as $reiter) {
      $recht = $reiterSeite->getRecht($reiter);
      
      if ($this->isRechtAllowed($recht)) {
        $reiterDm = new \Datamodul();
        $reiterDm->label = $reiterSeite->getLabel($reiter);
        $reiterDm->action = $reiterSeite->getAction($reiter); 
        $reiterDm->rnr = $this->request->raumNr;
        $result[] = $reiterDm;
      }
    }

    return $result;
  }

  private function getReiterBeschriftung()
  {
    return isset($this->toolSettings->reiterBeschriftung_raum) ? $this->toolSettings->reiterBeschriftung_raum : '';
  }

  private function getRaumartDialog()
  {
    $hlRaumart = \HLDispatcher::GetHinterlegungDialog(\HLDispatcher::HL_RAUMART_DIALOG);
    $hlRaumart->fields = array('raumart');

    return $hlRaumart->CreateDialog('raumart_div', '.hlraumart', 'Hinterlegung', 250, 400);
  }
}

class RaumDetailsRequest
{
  public $raumNr;
  public $suchParameter;
}

class RaumDetailsResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  public $kursort;
  public $user;
  public $menuPunkte;
  public $reiter;
  public $previousRaum;
  public $nextRaum;

  public $reiterButtons;

  public $oeffneReiter;
  public $raumNr;
  public $hlRaumartDialog;
}