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

namespace Kufer\Web\KuferTools\Basis\Raumbelegung\Raumdetails\Detailsreiter\Termine;

use Kufer\Web\KuferTools\Basis\View\DetailsTabelleRowView;
use Kufer\Web\KuferTools\Basis\View\DetailsTabelleRowViewModel;
use Kufer\Web\KuferTools\Basis\View\ViewSight;

abstract class ReiterTerminePresenter extends \Kufer\Web\KuferTools\Basis\Presenter\Presenter
{
  abstract protected function getLinkZuKursdetails($pDatamodul);

  
  abstract protected function getView4AnderenContent($pViewModel);

  abstract protected function setColumnHyperLink($pDatamodul, $pColumn);

  abstract protected function isKursLink($pDataModul, $pColumn);

  abstract protected function isDozentenLink($pColumn);

  abstract protected function isAdressLink($pColumn);

  public function present()
  {
    $emptyDataObj = $this->getEmptyDataObj();
    $data = \array_merge(array($emptyDataObj), $this->useCaseResponse->data);

    return $this->getAnderenContentAlsBody($data);
  }

  
  protected function getEmptyDataObj()
  {
    
    $dbwObj = new \DBWorkKurseTageWT();

    return $dbwObj->getNewObject();
  }

  protected function isDatamodulAryEmpty(array $datamodulArray): bool
  {
    return count($datamodulArray) <= 1;
  }

  
  protected function getAnderenContent($datamodul, $key): string
  {
    $isHeader = empty($datamodul->id);
    $viewDetailsModel = new TerminRowViewModel();
    $viewDetailsModel->termin = $datamodul;
    
    $viewDetailsModel->termin->linkZuKursdetails = $this->getLinkZuKursdetails($datamodul);
    $viewDetailsModel->isHeader = $isHeader;

    $viewDetails = $this->getView4AnderenContent($viewDetailsModel);

    $viewModel = new DetailsTabelleRowViewModel(
      $this->useCaseResponse->toolSection,
      $this->useCaseResponse->toolSettings,
      true,
      $isHeader,
      trim($viewDetails->getHeader()),
      $this->getHyperlink4Columns($datamodul, $this->useCaseResponse->columns),
      $datamodul->id,
      $datamodul
    );

    $viewModel->useReplacementTableContentHeader = true;
    $viewModel->replacementTableContentHeader = trim($viewDetails->getHeader());
    $viewModel->useReplacmentTableContent = true;
    $viewModel->replacementTableContent = trim($viewDetails->getFooter());

    $view = new DetailsTabelleRowView($viewModel);

    return $view->show();
  }

  
  protected function getHyperlink4Columns($datamodul, $columns)
  {
    $columnsCopy = array();
    $colorEvent = new \ColorEvent('#FF0000', 'A', 'ausfall', 'kurstag');
    $color = $colorEvent->getTextColor($datamodul);

    $columnsCount = count($columns);

    
    for ($i = 0; $i < $columnsCount; $i++) {
      $columnsCopy[$i] = clone $columns[$i];

      
      if ($color !== true) {
        $columnsCopy[$i]->textfarbe = $color;
      }
      
      $this->setColumnHyperLink($datamodul, $columnsCopy[$i]);
    } 

    return $columnsCopy;
  }

  protected function getViewModel()
  {
    $viewModel = new ReiterTermineViewModel();
    $viewModel->kursort = $this->useCaseResponse->kursort;
    $viewModel->kursort->reiterButtons = $this->getButtonHtml();

    return $viewModel;
  }

  protected function getButtonHtml()
  {
    $buttonleiste = new \Kufer\Web\KuferTools\Basis\Button\Buttonleiste();

    return $buttonleiste->getHtmlButtonleiste($this->useCaseResponse->reiterButtons, 'raumreiter_buttons');
  }
}