<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */

namespace Kufer\KuferTools\Basis\Kursdetails\DetailsReiter\Termine;

class TerminePresenter extends \Kufer\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($datamodulArray)
  {
    
    return count($datamodulArray) <= 1;
  }

  
  protected function getAnderenContent($datamodul, $key)
  {
    $viewModel = new TerminViewModel();
    $viewModel->columns = $this->getTerminStatusColor4Columns($datamodul, $this->useCaseResponse->columns);
    $viewModel->termin = $datamodul;
    $viewModel->index = $key;
    
    $viewModel->showCheckbox = $this->useCaseResponse->markierenButtons;
    $terminView = new TerminView($viewModel);

    return $terminView->show();
  }

  protected function getButtonHtml()
  {
    $buttonleiste = new \Kufer\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;
  }
}