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

namespace Kufer\Web\KuferTools\Allgemein\Ferien;

use Kufer\Web\KuferTools\Basis\UseCase\UseCase;
use Kufer\Web\Classes\Dbfilter\FerienSQLFilter;
use Kufer\Web\KuferTools\Basis\UseCaseBuilder\AccessDeniedAction;
use Kufer\Web\KuferTools\Basis\UseCaseBuilder\ActionBus;
use Kufer\Web\KuferTools\Basis\UseCaseBuilder\HandleWithUserAction;
use Kufer\Web\KuferTools\Basis\UseCaseBuilder\HasAccessWithUserAction;
use Kufer\Web\KuferTools\Basis\UseCaseBuilder\UseCaseBuilder;

class FerienUseCaseBuilder
{
  public function build(FerienRequest $request, FerienResponse $response): UseCase
  {
    $builder = new UseCaseBuilder();
    $builder->setUserBySession();

    $builder->addListenerByFunction(
      HasAccessWithUserAction::getStaticName(),
      static function(ActionBus $actionBus, HasAccessWithUserAction $action) use ($request) {
        if (is_null($request->kalenderId)) {
          return;
        }

        if (!\CleanOperations::isInt($request->kalenderId)) {
          $actionBus->trigger(new AccessDeniedAction());
        }

        $request->kalenderId = (int)$request->kalenderId;
      }
    );

    $builder->addListenerByFunction(
      HandleWithUserAction::getStaticName(),
      static function(ActionBus $actionBus, HandleWithUserAction $action) use ($request, $response) {
        $response->timestamp = \DateTimeOperations::getTimestampNow();
        $response->success = true;

        $filter = FerienSQLFilter::create();
        if (!is_null($request->kalenderId)) {
          $filter->equalsKalender((int)$request->kalenderId);
        }
        
        $filter->greaterEqualsBeginn(\DateTimeOperations::addDays(\DateTimeOperations::getDateNow(), 365 * -5));

        $toolAdminSettings = \ToolSettingsAPI::getToolSettings(0);
        $ferienKalender = array_map(static function($str) {
          return (int)$str;
        }, array_filter(explode(',', $toolAdminSettings->ferienKalender), \CleanOperations::class . '::isInt'));
        if (!empty($ferienKalender)) {
          $filter->inKalender($ferienKalender);
        }

        $filter->notAny(static function(FerienSQLFilter $filter): FerienSQLFilter {
          $filter->equalsBeginn(null);
          $filter->equalsEnde(null);

          return $filter;
        });
        $dbwFerien = \DBWorkFerien::getDefaultInstance();
        $response->ferien = array_map(static function(\Ferien $ferien) {
          $result = new class() extends \Datamodul {
            public $id;
            public $beginn;
            public $ende;
            public $text;
          };

          $result->id = $ferien->id;
          $result->beginn = \DateTime::createFromFormat('d.m.Y', $ferien->beginn)->format('Y-m-d');
          $result->ende = \DateTime::createFromFormat('d.m.Y', $ferien->ende)->format('Y-m-d');
          $result->text = $ferien->text;

          return $result;
        }, $dbwFerien->findByFilter($filter));
      }
    );

    return $builder->build($request, $response);
  }
}