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

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

class FeedbackBogenPresenter extends \Kufer\Web\KuferTools\Basis\Presenter\MitRahmenPresenter
{
  public function present()
  {
    $rahmenView = $this->getRahmenView();

    return $rahmenView->getHeader()
      . $this->getMenuLeiste()
      . '<main id="main-content">' . $this->getContent() . '</main>'
      . $rahmenView->getFooter();
  }

  protected function getContent()
  {
    if (!$this->useCaseResponse->feedbackBogenError) {
      $result = $this->getFeedbackBogenContent();
    } else {
      $feedbackView = $this->getView();
      $result = $feedbackView->getNoData();
    }

    return $result;
  }

  private function getFeedbackBogenContent(): string
  {
    $result = '';
    
    
    
    switch ($this->useCaseResponse->advanceState) {
      case 'fragenHandle':
        $feedbackView = $this->getView();
        $result .= $feedbackView->getHeader();
        $result .= $this->getAnderenContentAlsBody($this->useCaseResponse->anzeigeTextAry, $this->useCaseResponse->isInline);
        $result .= $feedbackView->getFooter();
        break;
      case 'fragenEnde':
      case 'fragenStart':
        $viewModel = new VorspannNachspannViewModel();
        
        $this->setBlaetternLinksInViewModel($viewModel);
        $viewModel->nachspann = $this->useCaseResponse->nachspann;
        $viewModel->vorspann = $this->useCaseResponse->vorspann;
        $view = new VorspannNachspannView($viewModel);
        if ($this->useCaseResponse->advanceState === 'fragenStart') {
          
          $result .= $view->getHeader();
        } else { 
          $result .= $view->getFooter();
        }
        break;
      case 'keineFragen':
      case 'fragenErfasst':
        $viewModel = new VorspannNachspannViewModel();
        $this->setBlaetternLinksInViewModel($viewModel);
        $viewModel->nachspann = $this->useCaseResponse->nachspann; 
        $viewModel->vorspann = $this->useCaseResponse->vorspann;

        $view = new VorspannNachspannView($viewModel);
        if ($this->useCaseResponse->advanceState == 'fragenErfasst') {
          $result .= $view->getNoData();
        } else {
          $result .= $view->getNoColumn();
        }
        break;
    }

    return $result;
  }

  
  protected function getAnderenContentAlsBody(array $datamodulArray, bool $pIsInline = false): string
  {
    switch ($this->useCaseResponse->ausgabeTyp) {
      case 'radio':
        $viewModel = new RadioAnzeigeViewModel();
        $viewModel->itemsInline = $pIsInline;
        $viewModel->anzeigeTexteAry = $datamodulArray;
        $view = new RadioAnzeigeView($viewModel);
        break;
      case 'checkbox':
        $viewModel = new CheckBoxAnzeigeViewModel();
        $viewModel->itemsInline = $pIsInline;
        $viewModel->anzeigeTexteAry = $datamodulArray;
        $view = new CheckBoxAnzeigeView($viewModel);
        break;
      default:
      case 'textinput':
        $viewModel = new InputAnzeigeViewModel();
        $view = new InputAnzeigeView($viewModel);
        break;
    }

    return $view->show();
  }

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

  protected function getViewModel()
  {
    $viewModel = new FeedbackBogenViewModel();
    $viewModel->toolId = $this->useCaseResponse->toolId;

    
    
    if (!$this->useCaseResponse->feedbackBogenError) {
      
      
      $viewModel->advanceState = $this->useCaseResponse->advanceState;

      $viewModel->frageBezeichnung = $this->useCaseResponse->aktfrage->bezeichnung;
      
      $viewModel->fragenAnzeigeText = $this->useCaseResponse->aktfrage->anzeigetext;
      $viewModel->bogenAnzeigeText = $this->useCaseResponse->bogenAnzeigeText;

      
      $viewModel->bemerkungsTextAnzeigen = $this->useCaseResponse->aktfrage->warumanzeigen == 'W';
      $viewModel->bemerkungsZeilen = $this->useCaseResponse->aktfrage->warumzeilen;
      
      $viewModel->bemerkungsLabelText = $this->useCaseResponse->aktfrage->warumtext;
      
      $viewModel->bemerkungsAntwort = $this->useCaseResponse->bemerkungsAntwort;


      
      
      $viewModel->inputAntwortWert = $this->useCaseResponse->inputAntwortWert;

      
      $this->setBlaetternLinksInViewModel($viewModel);
    }
    
    else {
      
      $viewModel->backToStartLink = $this->useCaseResponse->backToStartLink;
    }

    return $viewModel;
  }

  protected function setBlaetternLinksInViewModel($pViewModel)
  {
    $pViewModel->backwardLink = $this->useCaseResponse->backwardLink;
    $pViewModel->forwardLink = $this->useCaseResponse->forwardLink;

    $pViewModel->frageNrForward = $this->useCaseResponse->frageNrForward;
    $pViewModel->frageNrBackward = $this->useCaseResponse->frageNrBackward;

    $pViewModel->aktuelleSeite = $this->useCaseResponse->aktuelleSeite;
    $pViewModel->anzahlSeiten = $this->useCaseResponse->anzahlSeiten;

    
    $pViewModel->backToStartLink = $this->useCaseResponse->backToStartLink;
  }

  protected function getSeitenTitel(): string
  {
    return 'Feedback zum Kurs ' . $this->useCaseResponse->kurs->id;
  }
}