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

namespace Kufer\KuferTools\Basis\Presenter;

class WssPresenterFormular extends WssPresenter
{
  public const TEXTAREA = 1;
  public const SELECT = 2;
  public const DATUM = 3;
  public const CHECKBOX = 4;
  public const CHECKBOX_JN = 5;
  public const 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::TEXTAREA) {
      return $view->getNoColumn();
    } elseif ($this->useCaseResponse->column->istext == self::SELECT) {
      $view = $this->getViewFormular2();
      $optionen = $this->getSelectOptionen();
      $view->setSelectOptionen($optionen);

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

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

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

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

    return $viewModel;
  }

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

  private function getViewModelFormular2()
  {
    $viewModel = new \Kufer\KuferTools\Basis\View\WssFormular2ViewModel();
    $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();
    }
  }
}