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

namespace Kufer\Web\KuferTools\Veranstalter\KursUebersicht\Detailsreiter\Honorar;

class HonorarUseCase extends \Kufer\Web\KuferTools\Basis\UseCase\EinzelKursUseCase
{
  public function __construct($request)
  {
    parent::__construct($request, new HonorarResponse());
  }

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

  public static function getToolSectionId()
  {
    return 626;
  }

  public function hasAccess($pToolSectionId = null)
  {
    if (!parent::hasAccess()) {
      return false;
    }
    if (!$this->isRechtAllowed('632.1.5')) {
      return false;
    }

    return true;
  }

  public function action()
  {
    $kurs = $this->getKurs($this->request->knr);
    
    
    $dbwDozhon = new \DBWorkDozentenHonorar();
    $honorare = $dbwDozhon->getHonorarebyKNROhneVeranstalter($this->request->knr);

    
    
    $persErweiterungStrategy = new \Kufer\Web\KuferTools\Utils\PersonenErweiternStrategies\ErweitereDozKursStrategy();
    $honorare = $persErweiterungStrategy->fillAll($honorare, $this->getToolId(), $this->request->knr);

    $this->sortDozenten($honorare);

    return $this->getResponse($kurs, $honorare);
  }

  private function sortDozenten(&$honorare)
  {
    if (preg_match('/^(d\\.)?name$/', $this->toolSettings->sortdoz)) {
      $honorare = \SortFunctions::sortiereByDozentenName($honorare, $this->toolSettings->sortdozasc);
    }
  }

  protected function getResponse($kurs, $honorare)
  {
    $this->handleResponseStrategies();
    $this->response->kurs = $kurs;
    $this->response->honorare = $this->addDozInfoLinksToDozent($honorare);
    $datenSaetzeVorhanden = count($honorare) > 0;
    $this->response->reiterButtons = $this->getButtons($datenSaetzeVorhanden);
    
    
    
    $this->response->newDozentAllow = $this->isRechtAllowed('626.0.6') && \SessionOperations::isNichtReplikat();

    
    
    $this->response->dozentDeleteXsrf = $this->getXsrfKey('dozentDelete', $this->request->knr);

    return $this->response;
  }

  protected function getXsrfKey($pParam, $zusatzParam = '')
  {
    $xsrf = new \Xsrf();
    $key = $xsrf->generateKey4Action($pParam, $zusatzParam);

    return 'xsrf=' . \CleanOperations::cleanStringForHtml($key);
  }

  
  protected function getButtons($pDatenSaetzeVorhanden)
  {
    $buttonsObj = new \Kufer\Web\KuferTools\Basis\Kursdetails\Detailsreiter\VaHonorarReiterButtons($this, $this->request, $this->response);
    $buttonsArray = $buttonsObj->getButtons($pDatenSaetzeVorhanden);

    
    $selectionDependentButtons = array('Vertrag',  'Dozent entfernen', 'Dozent hinzufügen');
    $this->response->markierenButtons = $this->markierenButtonNotwendig($buttonsArray, $selectionDependentButtons);

    return $buttonsArray;
  }

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

  protected function addDozInfoLinksToDozent($honorare)
  {
    $helper = $this->getDozentenUebersichtHelper();
    if (\is_null($helper)) {
      return $honorare;
    }

    $allowedDnrs = $helper->getIdsOhneFilter();
    $currentUrl = urlencode($this->getRefererURL());
    $currentReferer = urlencode(\StringOperations::htmlentities('Reiter Dozenten'));
    foreach ($honorare as $honorar) {
      $dnr = $honorar->getVarValueEx('id', 'dozent');
      if (\in_array($dnr, $allowedDnrs)) {
        $doz = $honorar->getDatamodulByName('dozent');
        $link = 'index.php?tool_id=' . $this->getDozentenUebersichtToolId() . '&action=dozentdetails&dnr=' . $dnr . '&dguid=' . ((string)$doz->guid);
        $doz->dozinfoLink = $link;
        $doz->referer = $currentUrl;
        $doz->refererLabel = $currentReferer;
      }
    }

    return $honorare;
  }

  protected function getRefererURL()
  {
    $url = \URL::loadCurrent();
    $url->setParameterValue('action', 'kursdetails');

    return $url->getUrlRelative();
  }

  protected function getDozentenUebersichtHelper(): ?\Kufer\Web\KuferTools\Utils\DatamodulHelper\Dozenten\DozentenHelper
  {
    $dbwTools = new \DBWorkTools();
    $toolIdUebersicht = $this->getDozentenUebersichtToolId();
    $user = $this->getUser();
    if ($dbwTools->isToolAllowed($user->getIdentifier(), $toolIdUebersicht)) {
      return \Kufer\Web\KuferTools\Utils\DatamodulHelper\Dozenten\DozentenHelper::createVeranstalterDozentenUebersicht(
        \ToolSettingsAPI::getToolSettings($toolIdUebersicht),
        $user
      );
    }

    return null;
  }

  protected function getDozentenUebersichtToolId(): ?int
  {
    return \Kufer\Web\KuferTools\Veranstalter\DozentenInfo\DozentenUebersicht\DozentenUebersichtUseCase::getToolId();
  }
} 

class HonorarRequest
{
  public $knr;
}

class HonorarResponse extends \Kufer\Web\KuferTools\Basis\UseCase\ToolResponse
{
  public $kurs;
  public $honorare;
  public $columns;
  public $reiterButtons;
  public $markierenButtons;
  
  
  public $newDozentAllow;
  public $dozentDeleteXsrf;

  
}