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

namespace Kufer\Web\KuferTools\Basis\Kursdetails\Detailsreiter\Termine;

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

class TerminePresenter extends \Kufer\Web\KuferTools\Basis\Presenter\Presenter
{
  public function present()
  {
    
    $dbwKurseTage = new \DBWorkKurseTageWT();
    $emptyKurstag = $dbwKurseTage->getNewObject();
    $termine = \array_merge(array($emptyKurstag), $this->useCaseResponse->termine);

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

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

  
  protected function getAnderenContent($datamodul, $key): string
  {
    $isHeader = empty($datamodul->id);
    $viewModelDetails = new TerminRowViewModel();
    $viewModelDetails->termin = $datamodul;
    $viewModelDetails->isHeader = $isHeader;
    $viewModelDetails->showCheckbox = $this->useCaseResponse->markierenButtons;
    $viewDetails = new TerminRowView($viewModelDetails);

    $viewModel = new DetailsTabelleRowViewModel(
      $this->useCaseResponse->toolSection,
      $this->useCaseResponse->toolSettings,
      $this->useCaseResponse->markierenButtons,
      $isHeader,
      trim($viewDetails->getHeader()),
      $this->getTerminStatusColor4Columns($datamodul, $this->useCaseResponse->columns),
      $datamodul->id,
      $datamodul
    );
    $viewModel->additionalContent = $viewDetails->getBody();
    $viewModel->checkboxInputAttr = trim($viewDetails->getFooter());

    $view = new DetailsTabelleRowView($viewModel);

    return $view->show();
  }

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

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

  protected function getView()
  {
    return new TerminReiterView($this->getViewModel());
  }

  protected function getViewModel()
  {
    $viewModel = new TerminReiterViewModel();
    $viewModel->kurs = $this->useCaseResponse->kurs;
    $viewModel->kurs->toolId = $this->useCaseResponse->toolId;
    $viewModel->kurs->reiterButtons = $this->getButtonHtml();
    $viewModel->kurs->markierenButtons = $this->useCaseResponse->markierenButtons;

    return $viewModel;
  }

  
  protected function getTerminStatusColor4Columns($datamodul, $columns)
  {
    $columnsCopy = array();
    $toolSettingsAPI = new \ToolSettingsAPI();
    $toolSettings = $toolSettingsAPI->getToolSettings($this->useCaseResponse->toolId);
    $terminStatusHighlight = isset($toolSettings->ktstatus) && $toolSettings->ktstatus != '' ? $toolSettings->ktstatus : '';
    $ktStatusHighlightColor = isset($toolSettings->colorktstatus) && $toolSettings->colorktstatus != '' ? $toolSettings->colorktstatus : '';
    $colorEvent = new \ColorEvent($ktStatusHighlightColor, $terminStatusHighlight, 'ausfall', 'kurstag');
    $color = $colorEvent->getTextColor($datamodul);
    
    if ($color !== true) {
      foreach ($columns as $key => $column) {
        $columnsCopy[$key] = clone $column;
        $columnsCopy[$key]->textfarbe = $color;
      }

      return $columnsCopy;
    }

    return $columns;
  }
}