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

namespace Kufer\Web\KuferTools\Dozent\Wunschtermine\Uebersicht;

use Kufer\Web\KuferTools\Basis\Uebersicht\UebersichtMitWssRequest;
use Kufer\Web\KuferTools\Basis\Uebersicht\UebersichtMitWssResponse;
use Kufer\Web\KuferTools\Utils\StatusMessages\StatusMessageContainer;
use Kufer\Web\Classes\Db\DBWorkWunschtermine;
use Kufer\Web\Classes\Dbfilter\WunschtermineSQLFilter;
use Kufer\Web\KuferTools\Utils\FilterSearch\WunschtermineSearchFilter;
use Kufer\Web\KuferTools\Utils\ResponseStrategies\ResponseWunschtermineOnCurrentPage;

class WunschtermineUseCase extends \Kufer\Web\KuferTools\Basis\Uebersicht\UebersichtMitWssUseCase
{
  public function __construct($request)
  {
    parent::__construct($request, new WunschtermineResponse());
  }

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

  public static function getToolSectionId()
  {
    return 796;
  }

  public function action()
  {
    $response = parent::action();
    $response->uebersichtAlsTabelle = $this->toolSettings->wunschtermineUebersichtAlsTabelle;

    return $response;
  }

  protected function vorAction()
  {
    parent::vorAction(); 

    if ($this->request->aendereFilter) {
      $searchApi = $this->getSearchAPI();
      $searchApi->setSearchMapToSession($this->request->suchParameter, $this->getToolId());
    }
  }

  protected function getSearchAPI()
  {
    return new WunschtermineSearchFilter();
  }

  protected function isFilterGesetzt()
  {
    $suchApi = $this->getSearchAPI();

    return (!isset($this->request->suchParameter) || ($this->request->aendereFilter ?? false)) && !$suchApi->isSearchMapFromSessionEmpty($this->getToolId());
  }

  protected function getAlleIds()
  {
    $searchApi = $this->getSearchAPI();

    $user = $this->getUser();
    $filter = WunschtermineSQLFilter::create();
    if (!$user->isAdmin()) {
      $filter->equalsDozent($user->getDnrIdentifier());
    }

    $filter = $searchApi->applySearchFromSession($filter, $this->getToolId());

    return DBWorkWunschtermine::getDefaultInstance()->findIdsByFilter($filter);
  }

  protected function countAlleOhneFilter()
  {
    $user = $this->getUser();
    $filter = WunschtermineSQLFilter::create();
    if (!$user->isAdmin()) {
      $filter->equalsDozent($user->getDnrIdentifier());
    }

    return DBWorkWunschtermine::getDefaultInstance()->countByFilter($filter);
  }

  protected function getUeberschrift()
  {
    return 'Wunschtermine';
  }

  protected function getDatamodulOnCurrentPage()
  {
    return new ResponseWunschtermineOnCurrentPage($this);
  }

  protected function getPanelButtonCallBacks()
  {
    $buttons = array();
    $buttonFactory = new \Kufer\Web\KuferTools\Basis\Button\ButtonFactory();

    if ($this->isRechtAllowed('796.0.1')) {
      $buttons[] = function($wunschtermin) use ($buttonFactory) {
        return $buttonFactory->getWunschterminBearbeitenButton($this->getToolId(), $wunschtermin->id);
      };
    }

    if ($this->isRechtAllowed('796.0.2')) {
      $buttons[] = function($wunschtermin) use ($buttonFactory) {
        return $buttonFactory->getWunschterminLoeschenButton($this->getToolId(), $wunschtermin->id);
      };
    }

    return $buttons;
  }

  protected function getButtons()
  {
    $buttons = array();
    $buttonFactory = new \Kufer\Web\KuferTools\Basis\Button\ButtonFactory();

    if ($this->isRechtAllowed('796.0.3')) {
      $buttons[] = array($buttonFactory->getWunschterminNeuButton($this->getToolId()));
    }

    $anzahlEintraege = $this->countAlleOhneFilter();
    $this->addFilterButton($buttons, $buttonFactory, $anzahlEintraege, 'Filter', 'suchen_button_wunschtermine', 'reset_filter_button_wunschtermine');

    return $buttons;
  }

  protected function getDatamoduleProSeite()
  {
    return $this->toolSettings->wunschtermineProSeite;
  }
}

class WunschtermineRequest extends UebersichtMitWssRequest
{
}

class WunschtermineResponse extends UebersichtMitWssResponse
{
  public $datamodule;
  public $uebersichtAlsTabelle;
  public $info = '';
  public $success = '';
  public $warning = '';
  public $error = '';
}