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

namespace Kufer\Web\KuferTools\Utils\FilterSearch;

use Kufer\Web\Classes\Dbfilter\Dozenten2SQLFilter;
use Kufer\Web\Classes\Dbfilter\DozentenBezirkeSQLFilter;
use Kufer\Web\Classes\Dbfilter\DozentenSQLFilter;
use Kufer\Web\Classes\Dbfilter\KennzeichenSQLFilter;
use Kufer\Web\Classes\Dbfilter\KurseSQLFilter;
use Kufer\Web\KuferTools\Utils\FilterSearch\Method\ChildSearchFilter;
use Kufer\Web\KuferTools\Utils\FilterSearch\Method\EqualsIntMethod;
use Kufer\Web\KuferTools\Utils\FilterSearch\Method\HasSearchFilterMethod;
use Kufer\Web\KuferTools\Utils\FilterSearch\Method\SearchStringMethod;
use Kufer\Web\KuferTools\Utils\FilterSearch\Method\StartsWithStringMethod;

class DozentenSearchFilter extends ToolSearchFilter
{
  public function __construct(bool $recursive = false)
  {
    parent::__construct();

    $this->addSearch(
      'doznr',
      new EqualsIntMethod('Id')
    );

    $this->addSearch(
      'name',
      new StartsWithStringMethod('Nachname')
    );

    $this->addSearch(
      'vorname',
      new StartsWithStringMethod('Vorname')
    );

    $this->addSearch(
      'namevorname',
      new SearchStringMethod('Name')
    );

    $this->addSearch(
      'wohnort',
      new StartsWithStringMethod('Ort')
    );

    $this->addSearch(
      'strasse',
      new StartsWithStringMethod('Strasse')
    );

    $this->addSearch(
      'fachbereich',
      new HasSearchFilterMethod('Fachbereich')
    );

    $this->addSearch(
      'pnr',
      new StartsWithStringMethod('Personalnummer')
    );

    for ($i = 1; $i <= 16; $i++) {
      $this->addSearch(
        "krit$i",
        new StartsWithStringMethod("Freidef$i")
      );
    }

    $this->addSearch(
      'branchen_name',
      new HasSearchFilterMethod('BranchenText')
    );

    $this->addSearch(
      'fach',
      new HasSearchFilterMethod('FachId')
    );

    $this->addSearchFilter(
      new ChildSearchFilter(
        '',
        new Dozenten2SearchFilter(),
        Dozenten2SQLFilter::create(),
        static function(DozentenSQLFilter $thisFilter, Dozenten2SQLFilter $filter) {
          $thisFilter->applyDozenten2Filter($filter);

          return $thisFilter;
        }
      )
    );

    $this->addSearchFilter(
      new ChildSearchFilter(
        '',
        new DozentenBezirkeSearchFilter(),
        DozentenBezirkeSQLFilter::create(),
        static function(DozentenSQLFilter $thisFilter, DozentenBezirkeSQLFilter $filter) {
          $thisFilter->applyDozentenBezirkeFilter($filter);

          return $thisFilter;
        }
      )
    );

    if (!$recursive) {
      $this->addSearchFilter(
        new ChildSearchFilter(
          'veranstalter',
          new DozentenSearchFilter(true),
          DozentenSQLFilter::create(),
          static function(DozentenSQLFilter $thisFilter, DozentenSQLFilter $filter) {
            $thisFilter->applyVeranstalterFilter($filter);

            return $thisFilter;
          }
        )
      );
    }

    $this->addAliasFor('nicht_kennzeichen_nummer', 'kennzeichen_not_kennz');

    $this->addSearchFilter(
      new ChildSearchFilter(
        'kennzeichen',
        new KennzeichenSearchFilter(),
        KennzeichenSQLFilter::create(),
        static function(DozentenSQLFilter $thisFilter, KennzeichenSQLFilter $filter) {
          $thisFilter->applyKennzeichenFilter($filter);

          return $thisFilter;
        }
        
      )
    );

    $this->addSearchFilter(
      new ChildSearchFilter(
        'kurs',
        new KurseSearchFilter(),
        \Kufer\Web\Classes\Dbfilter\KurseSQLFilter::create(),
        static function(DozentenSQLFilter $thisFilter, KurseSQLFilter $filter) {
          return $thisFilter->applyKurseFilter($filter);
        }
      )
    );
  }
}