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

namespace Kufer\Web\KuferTools\Teilnehmer\KursUebersicht\Terminplan;

abstract class KurstageUebersichtPdfUseCase extends \Kufer\Web\KuferTools\Basis\UseCase\ToolUseCase
{
  public function __construct($request, $response = null)
  {
    parent::__construct($request, $response ?? new KurstageUebersichtPdfResponse());
  }

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

  public static function getToolSectionId()
  {
    return 304;
  }

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

    if (\trim($this->request->belegIds)) {
      $belegIds = \explode(':', $this->request->belegIds);
      $belegungsHelper = $this->getTeilnehmerBelegungsHelper();
      if (!empty(\array_diff($belegIds, $belegungsHelper->getIdsOhneFilter()))) {
        return false;
      }
    }

    return true;
  }

  public function action()
  {
    $kurstage = $this->getKurstage();
    $pdf = $this->getPdf($kurstage);

    return $this->getResponse($pdf);
  }

  protected function getTeilnehmerBelegungsHelper(): \DatamodulHelper
  {
    return new \Kufer\Web\KuferTools\Utils\DatamodulHelper\Belegungen\TeilnehmerBelegungen($this->toolSettings, $this->getUser()->tnr);
  }

  protected function getKurstage()
  {
    $user = $this->getUser();

    $this->request->vondat = $this->request->datumVon;
    $this->request->bisdat = $this->request->datumBis;
    $this->request->terminbereich = $this->request->terminBereich;
    $icsKalenderKurstage = new \Kufer\Web\KuferTools\Utils\DatamodulHelper\KursTage\TnIcsKalenderKurstage($this->toolSettings, $user, $this->request, $this->getBelegungen());
    $kurstage = $icsKalenderKurstage->getDatamoduleMitFilter(null);

    $kurstageAssoc = array();
    foreach ($kurstage as $kurstag) {
      if (!isset($kurstageAssoc[$kurstag->datum])) {
        $kurstageAssoc[$kurstag->datum] = array();
      }
      $kurstageAssoc[$kurstag->datum][] = $kurstag;
    }

    return $kurstageAssoc;
  }

  protected function getBelegungen(): array
  {
    $belegungshelper = $this->getTeilnehmerBelegungsHelper();
    if (\trim($this->request->belegIds) !== '') {
      $belegIdsAry = explode(':', $this->request->belegIds);

      return $belegungshelper->getDatamoduleByIds($belegIdsAry);
    } else {
      return $belegungshelper->getDatamoduleMitFilter($this->getToolId());
    }
  }

  protected function getResponse($pPdf)
  {
    $this->response->pdf = $pPdf;

    return $this->response;
  }

  abstract protected function getPdf(array $pKurstage): \ExtPDF;

  
  protected function kuerzeText($pPdf, $pText, $pMaxLaenge)
  {
    if ($pMaxLaenge <= 0) {
      return '';
    }

    $laengeText = $pPdf->GetStringWidth($pText);
    if ($laengeText > $pMaxLaenge) {
      $maxLaengeNeuerText = $pMaxLaenge - $pPdf->GetStringWidth('...');
      while ($laengeText > $maxLaengeNeuerText) {
        $pText = \StringOperations::substr($pText, 0, -1);
        $laengeText = $pPdf->GetStringWidth($pText);
      }
      $pText .= '...';
    }

    return $pText;
  }

  
  protected function getKtStatusErlaubt()
  {
    $ktStatus = $this->toolSettings->ktstatus;
    if (\trim($ktStatus) === '') {
      $alleStatus = \ConstantOperations::getKurstagStatus4EinschraenkungAufKTStatus();
      $kuerzel = \array_keys($alleStatus);
      $ktStatus = \implode('', $kuerzel);
    }

    return $ktStatus;
  }
}

class KurstageUebersichtPdfRequest
{
  public $belegIds;
  public $terminBereich;
  public $datumVon;
  public $datumBis;
}

class KurstageUebersichtPdfResponse extends \Kufer\Web\KuferTools\Basis\UseCase\ToolResponse
{
  
  public $pdf;
}