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

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

use Kufer\Web\KuferTools\Basis\UseCase\ToolResponse;
use Kufer\Web\KuferTools\Utils\ResponseStrategies\ResponseMitRahmen;
use Kufer\Web\KuferTools\Utils\ResponseStrategies\ResponseWss;
use Kufer\Web\Classes\Db\DBWorkWunschtermine;
use Kufer\Web\Classes\Dbfilter\WunschtermineSQLFilter;

class WunschterminBearbeitenUseCase extends \Kufer\Web\KuferTools\Basis\UseCase\ToolUseCase
{
  
  protected $dbwWunschtermine;

  public function __construct($request, $response = null)
  {
    parent::__construct($request, $response ?? new WunschterminBearbeitenResponse());
    $this->dbwWunschtermine = DBWorkWunschtermine::getDefaultInstance();
  }

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

  public static function getToolSectionId()
  {
    return 798;
  }

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

    

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

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

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

    return true;
  }

  public function action()
  {
    $this->handleResponseStrategies();
    $this->response->wunschtermin = $this->dbwWunschtermine->findOneByFilter(
      WunschtermineSQLFilter::create()->equalsId($this->request->id)
    ) ?? $this->dbwWunschtermine->getNewObject(); 

    return $this->response;
  }

  protected function getResponseStrategies()
  {
    return array(
      new ResponseMitRahmen($this),
      new ResponseWss($this),
      new \Kufer\Web\KuferTools\Utils\ResponseStrategies\ResponseWithToolSettings($this)
    );
  }
}

class WunschterminBearbeitenRequest extends \Kufer\Web\KuferTools\Basis\UseCase\XsrfRequest
{
  public $id;

  public $columnValues;
  public $wssHandler;
}

class WunschterminBearbeitenResponse extends ToolResponse
{
  public $menuPunkte;
  public $user;
  public $wunschtermin;
  public $columns;
}