<?php
/**
 * @copyright (c) Kufer Software GmbH, Oderstraße 7, D-84453 Mühldorf
 */

namespace Kufer\KuferTools\Basis\Uebersicht;

use Kufer\KuferTools\Basis\Kursdetails\DetailsReiter\MerkeReiter\MerkeReiterUseCase as MerkeReiterUseCase;
use Kufer\KuferTools\Utils\Referrer;

abstract class UebersichtUseCase extends \Kufer\KuferTools\Basis\UseCase\ToolUseCase
{
  abstract protected function getAlleIds();

  abstract protected function getUeberschrift();

  abstract protected function getDatamodulOnCurrentPage();

  protected function getAlleIdsOhneFilter()
  {
    return $this->getAlleIds();
  }

  protected function getFreitext()
  {
    return '';
  }

  protected function getDisplayButtonsTop()
  {
    return true;
  }

  protected function getDisplayButtonsBottom()
  {
    return false;
  }

  
  private $cache_alle_ids;

  protected function getAlleIdsCached()
  {
    if (is_null($this->cache_alle_ids)) {
      $this->cache_alle_ids = $this->getAlleIds();
    }

    return $this->cache_alle_ids;
  }

  
  private $cache_alle_ids_ohne_filter;

  protected function getAlleIdsOhneFilterCached()
  {
    if (is_null($this->cache_alle_ids_ohne_filter)) {
      $this->cache_alle_ids_ohne_filter = $this->getAlleIdsOhneFilter();
    }

    return $this->cache_alle_ids_ohne_filter;
  }

  protected function getDatamoduleProSeite()
  {
    return 4;
  }

  
  protected function checkboxNeeded()
  {
    return false;
  }

  protected function verlinkePanelHeaderAllowed()
  {
    return false;
  }

  protected function getButtons()
  {
    return array();
  }

  
  protected function getPanelButtonCallBacks()
  {
    return array();
  }

  
  public function action()
  {
    $this->response->einstieg = $this->request->einstieg;

    $this->vorAction();
    Referrer::resetReferrerInSession($this->getToolId());
    MerkeReiterUseCase::setzeReiterZurueck($this->getToolId(), $this->getToolSectionId());
    $this->aktualisiereSeite();
    $this->aktualisiereSelectedIds();
    $response = $this->getResponse();
    $this->nachAction();

    return $response;
  }

  protected function vorAction()
  {
  }

  protected function nachAction()
  {
    \SessionOperations::setErsterAufrufInKuferTools(static::getToolId());
  }

  private function aktualisiereSeite()
  {
    \Kufer\KuferTools\Utils\Blaettern::aktualisiereSeiteInSession($this->getToolId(), $this->request->seite, $this->request->einstieg, $this->request->aendereFilter, $this->getSeiteSessionPrefix());
    $this->request->seite = \Kufer\KuferTools\Utils\Blaettern::getAktuelleSeiteFromSession($this->getToolId(), $this->getSeiteSessionPrefix());
  }

  
  protected function getSeiteSessionPrefix()
  {
    return 'seite_tool';
  }

  protected function aktualisiereSelectedIds()
  {
    $ids = $this->getAlleIdsOhneFilterCached();
    if (!is_null($this->request->markieren)) {
      $this->request->markieren->setTotalDataArray($ids);
      if ($this->request->einstieg || $this->request->aendereFilter) {
        $this->request->markieren->clearMarkedIdsInSession();
      } else {
        $this->request->markieren->setMarkedIdsToSession();
      }
    } else {
      
      \SessionOperations::setVarValueWTIntern('totaldatastr_' . $this->getToolId(), implode('', $ids));
    }
  }

  protected function datensaetzeVorhanden()
  {
    return count($this->getAlleIdsCached()) > 0;
  }

  protected function isFilterGesetzt()
  {
    return false;
  }

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

    $this->response->buttons = $this->getButtons();
    $this->response->panelButtonCallBacks = $this->getPanelButtonCallBacks();
    $this->response->showCheckbox = $this->checkboxNeeded();

    if (isset($this->request->markieren) && !is_null($this->request->markieren)) {
      $this->response->selectedIds = $this->request->markieren->getMarkedIdsFromSession();
    }
    $this->response->verlinkePanelTitle = $this->verlinkePanelHeaderAllowed();
    $this->response->ueberschrift = $this->getUeberschrift();
    $this->response->freitext = $this->getFreitext();
    $this->response->display_buttons_top = $this->getDisplayButtonsTop();
    $this->response->display_buttons_bottom = $this->getDisplayButtonsBottom();

    return $this->response;
  }

  protected function getResponseStrategies()
  {
    return array(
        new \Kufer\KuferTools\Utils\ResponseStrategies\ResponseMitRahmen($this),
        new \Kufer\KuferTools\Utils\ResponseStrategies\ResponseBlaettern($this, $this->getAlleIdsCached(), $this->getDatamoduleProSeite(), $this->request->seite),
        $this->getDatamodulOnCurrentPage()
    );
  }

  
  protected function addFilterButton(&$pButtons, $pButtonFactory, $pAnzahlEintraege, $pText = '', $pFilterAction = 'suchen_button', $pFilterLeerenAction = 'reset_filter_button')
  {
    if ($this->isFilterGesetzt() || $pAnzahlEintraege > 0) {
      $buttonGruppeFilter = array();
      $buttonGruppeFilter[] = $pButtonFactory->getFilterButton($pText, $pFilterAction);
      if ($this->isFilterGesetzt()) {
        $buttonGruppeFilter[] = $pButtonFactory->getFilterLeerenButton('leeren', $pFilterLeerenAction);
      }
      $pButtons[] = $buttonGruppeFilter;
    }
  }

  
}

class UebersichtRequest
{
  public $seite = 1;
  public $suchParameter;
  public $einstieg;
  public $aendereFilter;

  public $markieren;
}

class UebersichtResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  public $datamodule = array();
  public $user;
  public $ueberschrift;
  public $freitext;
  public $display_buttons_top;
  public $display_buttons_bottom;
  public $blaettern;
  public $buttons;
  public $panelButtonCallBacks;
  public $menuPunkte;
  public $selectedIds = '';
  public $showCheckbox;
  public $verlinkePanelTitle;
  public $einstieg;
}