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

namespace Kufer\Web\KuferTools\Basis\Presenter;

class WssPresenterFormular extends WssPresenter
{
  public const TYPE_TEXTAREA = 1;
  public const TYPE_SELECT = 2;
  public const TYPE_DATUM = 3;
  public const TYPE_CHECKBOX = 4;
  public const TYPE_CHECKBOX_JN = 5;
  public const TYPE_INPUT = -1;

  public function present()
  {
    $result = $this->getLabel();
    $result .= $this->getField();

    return $result;
  }

  public function getLabel()
  {
    $view = $this->getView();

    return $view->getHeader();
  }

  public function getField()
  {
    $view = $this->getView();

    if (!$this->useCaseResponse->column->canedit) {
      return $view->getBody();
    }
    if ($this->useCaseResponse->column->istext == self::TYPE_TEXTAREA) {
      return $view->getNoColumn();
    } elseif ($this->useCaseResponse->column->istext == self::TYPE_SELECT) {
      $view = $this->getViewFormular2();
      $optionen = $this->getSelectOptionen();
      $view->setSelectOptionen($optionen);

      return $view->show();
    } elseif ($this->useCaseResponse->column->istext == self::TYPE_DATUM) {
      return $view->getFooter();
    } elseif (in_array($this->useCaseResponse->column->istext, array(self::TYPE_CHECKBOX, self::TYPE_CHECKBOX_JN))) {
      $view = $this->getViewFormular2();

      return $view->getNoData();
    } else { 
      return $view->getFooter();
    }
  }

  
  protected function getView()
  {
    return new \Kufer\Web\KuferTools\Basis\View\WssFormular1View($this->getViewModel());
  }

  protected function getViewModel()
  {
    $viewModel = new \Kufer\Web\KuferTools\Basis\View\WssFormular1ViewModel();
    $viewModel->toolSection = $this->useCaseResponse->toolSection;
    $viewModel->toolSettings = $this->useCaseResponse->toolSettings;
    $viewModel->column = $this->useCaseResponse->column;
    $viewModel->datamodul = $this->useCaseResponse->datamodul;
    $viewModel->requestPrefix = $this->useCaseResponse->requestPrefix ?? '';

    return $viewModel;
  }

  
  private function getViewFormular2()
  {
    return new \Kufer\Web\KuferTools\Basis\View\WssFormular2View($this->getViewModelFormular2());
  }

  private function getViewModelFormular2()
  {
    $viewModel = new \Kufer\Web\KuferTools\Basis\View\WssFormular2ViewModel();
    $viewModel->toolSection = $this->useCaseResponse->toolSection;
    $viewModel->toolSettings = $this->useCaseResponse->toolSettings;
    $viewModel->column = $this->useCaseResponse->column;
    $viewModel->datamodul = $this->useCaseResponse->datamodul;
    $viewModel->requestPrefix = $this->useCaseResponse->requestPrefix ?? '';

    return $viewModel;
  }

  private function getSelectOptionen()
  {
    $selected = $this->useCaseResponse->datamodul->getVarValueEx($this->useCaseResponse->column->fieldname, $this->useCaseResponse->column->datamodul);

    $combobox = \ComboboxFactory::getComboboxByColumn($this->useCaseResponse->column, $selected, $this->useCaseResponse->datamodul);
    if (!is_null($combobox)) {
      return $combobox->getOptionenMitDefaultOption();
    } else {
      return array();
    }
  }
}