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

namespace Kufer\Web\KuferTools\Dozent\Urlaube\Bearbeiten;

use Kufer\KuferTools\Basis\UseCase\BasisResponse;
use Kufer\KuferTools\Utils\ResponseStrategies\ResponseMitRahmen;
use Kufer\KuferTools\Utils\ResponseStrategies\ResponseWss;
use Kufer\Web\Classes\Db\DBWorkDozUrlaube;
use Kufer\Web\Classes\Dbfilter\DozUrlaubeSQLFilter;

class UrlaubBearbeitenUseCase extends \Kufer\KuferTools\Basis\UseCase\ToolUseCase
{
  
  protected $dbwUrlaube;

  public function __construct($request, $response = null)
  {
    parent::__construct($request, $response ?? new UrlaubBearbeitenResponse());
    $this->dbwUrlaube = DBWorkDozUrlaube::getDefaultInstance();
  }

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

  public static function getToolSectionId()
  {
    return 800;
  }

  public function hasAccess($pToolSectionId = null)
  {
    if (!parent::hasAccess($pToolSectionId)) {
      return false;
    }

    

    if (empty($this->request->id)) {
      return false;
    }

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

    if (!$this->dbwUrlaube->existsByFilter($filter)) {
      return false;
    }

    return true;
  }

  public function action()
  {
    $this->handleResponseStrategies();
    $this->response->urlaub = $this->dbwUrlaube->findOneByFilter(
      DozUrlaubeSQLFilter::create()->equalsId($this->request->id)
    ) ?? $this->dbwUrlaube->getNewObject(); 

    return $this->response;
  }

  protected function getResponseStrategies()
  {
    return array(
      new ResponseMitRahmen($this),
      new ResponseWss($this)
    );
  }
}

class UrlaubBearbeitenRequest
{
  public $id;

  public $columnValues;
  public $wssHandler;
}

class UrlaubBearbeitenResponse extends BasisResponse
{
  public $urlaub;
  public $columns;
}