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

namespace Kufer\KuferTools\Basis\Anmeldung\KursAuswahl;

use Kufer\KuferTools\Utils\KursFilter\HandleShowKursFilter;


abstract class KursAuswahlUseCase extends \Kufer\KuferTools\Basis\UseCase\ToolUseCase
{
  public function __construct($request)
  {
    parent::__construct($request, new KursAuswahlResponse());
  }

  public function action()
  {
    $knr = $this->getFilterKursnummer();
    $columns = $this->getColumns();

    return $this->getResponse($knr, $columns);
  }

  
  protected function getFilterKursnummer(): string
  {
    if ($this->toolSettings->kursFilterVorbelegen === 'W') {
      return $this->toolSettings->knrVon;
    }

    return '';
  }

  protected function getResponse($knr, $columns)
  {
    $kursFilterObj = $this->getKursFilterObj();
    $kursFilter = HandleShowKursFilter::getKursFilterVonBis($this->toolSettings, $kursFilterObj->getToolStrFuerFilter());
    
    $this->response->kursFilterVon = $kursFilter['von'];
    $this->response->kursFilterBis = $kursFilter['bis'];

    $this->setHinterlegungenToResponse();

    $this->response->knr = $knr;
    $this->response->columns = $columns;
    $this->response->showAdvancedFilter = $this->toolSettings->showAdvancedKursFilter;

    return $this->response;
  }

  protected function getKursFilterObj()
  {
    return new \Kufer\KuferTools\Utils\KursFilter\KursFilter($this->getToolId(), $this->toolSettings);
  }

  abstract protected function getKursHelper(): \Kufer\KuferTools\Utils\DatamodulHelper\KursHelper\KursHelper;

  protected function getKursIds()
  {
    return $this->getKursHelper()->getIdsOhneFilter();
  }

  protected function setHinterlegungenToResponse()
  {
    $this->response = HandleShowKursFilter::setHinterlegungenToResponse(
      $this->response,
      $this->getKursIds(),
      \explode(',', $this->toolSettings->semlist),
      isset($this->toolSettings->showAllHinterlegungenFilter) && $this->toolSettings->showAllHinterlegungenFilter
    );
  }

  protected function getExtraColumns()
  {
    $columns = $this->getColumns();

    $extraColumn = new \Column();
    $extraColumn->fieldname = 'id';
    $extraColumn->datamodul = 'kurse';
    $extraColumn->title = '';
    $extraColumn->width = '30';
    array_unshift($columns, $extraColumn);

    return $columns;
  }
}

class KursAuswahlRequest
{
}

class KursAuswahlResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  
  public $knr;

  
  public $columns;

  public $kategorien = array();
  public $sonderKategorien = array();
  public $aussenstellen = array();
  public $kursOrte = array();
  public $fachBereiche = array();
  public $bezirke = array();
  public $gemeinden = array();
  public $zielgruppen = array();
  public $semester = array();
  public $benutzer = array();

  public $kursFilterVon;
  public $kursFilterBis;

  public $showAdvancedFilter;
}