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

namespace Kufer\Web\KuferTools\Basis\Uebersicht;

abstract class UebersichtMitWssPresenter extends UebersichtPresenter
{
  protected function getContent()
  {
    if ($this->uebersichtAlsTabelle() && count($this->useCaseResponse->datamodule) > 0) {
      
      array_unshift($this->useCaseResponse->datamodule, $this->getDatamodulFuerUeberschriftszeile());
    }

    return $this->getAnderenContentAlsBody($this->useCaseResponse->datamodule);
  }

  protected function uebersichtAlsTabelle()
  {
    return isset($this->useCaseResponse->uebersichtAlsTabelle) && $this->useCaseResponse->uebersichtAlsTabelle;
  }

  protected function getDatamodulFuerUeberschriftszeile()
  {
    return new \Datamodul();
  }

  protected function getAnderenContent($datamodul, $key): string
  {
    if ($this->uebersichtAlsTabelle()) {
      return $this->contentAlsTabelle($datamodul, $key);
    } else {
      return $this->contentAlsPanels($datamodul, $key);
    }
  }

  protected function contentAlsPanels($datamodul, $key)
  {
    $panelViewModel = $this->getPanelViewModel($datamodul);
    $panelViewModel->toolSection = $this->useCaseResponse->toolSection;
    $panelViewModel->toolSettings = $this->useCaseResponse->toolSettings;

    $panelViewModel->index = $key;
    $panelViewModel->columns = $this->useCaseResponse->columns;
    $panelViewModel->datamodul = $datamodul;

    $panelViewModel->panel = $this->datamodulToPanel($datamodul);
    $bodyView = $this->getPanelView($panelViewModel);

    return $bodyView->show();
  }

  protected function contentAlsTabelle($datamodul, $key)
  {
    $panelViewModel = $this->getTableViewModel($datamodul);

    $panelViewModel->toolSection = $this->useCaseResponse->toolSection;
    $panelViewModel->toolSettings = $this->useCaseResponse->toolSettings;
    $panelViewModel->index = $key;
    $panelViewModel->columns = $this->useCaseResponse->columns;
    $panelViewModel->datamodul = $datamodul;
    $panelViewModel->isHeader = $this->isDatamodulHeader($datamodul);

    $panelViewModel->panel = $this->datamodulToPanel($datamodul);
    $bodyView = $this->getTableView($panelViewModel);

    return $bodyView->show();
  }

  protected function isDatamodulHeader($datamodul): bool
  {
    return is_null($datamodul->id) || $datamodul->id === '';
  }

  protected function getPanels()
  {
    return array();
  }

  protected function getPanelViewModel($datamodul)
  {
    return new UebersichtPanelMitWssViewModel();
  }

  protected function getPanelView($viewModel)
  {
    return new UebersichtPanelMitWssView($viewModel);
  }

  protected function getTableViewModel($datamodul)
  {
    return new UebersichtTabelleMitWssViewModel();
  }

  protected function getTableView($viewModel)
  {
    return new UebersichtTabelleMitWssView($viewModel);
  }

  protected function getViewModel()
  {
    $viewModel = parent::getViewModel();
    $viewModel->toolId = $this->useCaseResponse->toolId;
    $viewModel->showBackButton = $this->showBackButton();
    $viewModel->fehlermeldung = $this->getFehlermeldung();
    $viewModel->erfolgsmeldung = $this->getErfolgsmeldungen();
    $viewModel->uebersichtAlsTabelle = $this->uebersichtAlsTabelle();
    $viewModel->toolSection = $this->useCaseResponse->toolSection;
    $viewModel->toolSettings = $this->useCaseResponse->toolSettings;

    return $viewModel;
  }

  protected function showBackButton()
  {
    return false;
  }

  protected function getFehlermeldung()
  {
    return '';
  }

  protected function getErfolgsmeldungen()
  {
    return '';
  }

  protected function getUebersichtViewModel()
  {
    return new UebersichtViewModel();
  }

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