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

namespace Kufer\Web\KuferTools\Teilnehmer\Visitenkarten\VisitenkartenDetails;

use Kufer\Web\KuferTools\Teilnehmer\Visitenkarten\NeueVisitenkarteErstellen\NeueVisitenkarteErstellenSpeichern\InsertVisitenkarteUseCase;

class VisitenkartenDetailsPresenter extends \Kufer\Web\KuferTools\Basis\Presenter\MitRahmenPresenter
{
  public function getJsFilesSrcAryWeitere(): array
  {
    $js = parent::getJsFilesSrcAryWeitere();
    $js[] = $this->getJsFileFromSys('webvisitenkarten.js');

    return $js;
  }

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

  protected function getViewModel()
  {
    $viewModel = new VisitenkartenDetailsViewModel();
    $viewModel->toolSection = $this->useCaseResponse->toolSection;
    $viewModel->toolSettings = $this->useCaseResponse->toolSettings;
    $viewModel->ueberschrift = $this->useCaseResponse->ueberschrift;
    $viewModel->visitenkarte = $this->useCaseResponse->visitenkarte;
    $viewModel->columns = $this->useCaseResponse->columns;
    $viewModel->buttons = $this->useCaseResponse->buttons;
    $viewModel->successMeldung = $this->getSuccessMeldung();
    $viewModel->infoMeldung = $this->getInfoMeldung();

    return $viewModel;
  }

  private function getSuccessMeldung()
  {
    $allgSettings = $this->getAllgSettings();

    if ($this->useCaseResponse->statusCode === InsertVisitenkarteUseCase::VISITENKARTE_ERSTELLEN_INSERT_SUCCESS) {
      return $allgSettings->successMsgInsertVisitenkarte;
    }

    if ($this->useCaseResponse->statusCode === VisitenkartenDetailsSpeichern\VisitenkartenDetailsSpeichernUseCase::VISITENKARTE_BEARBEITEN_SUCCESS) {
      return $allgSettings->successMsgUpdateVisitenkarte;
    }

    return '';
  }

  private function getInfoMeldung()
  {
    $allgSettings = $this->getAllgSettings();
    if ($this->useCaseResponse->statusCode === VisitenkartenDetailsSpeichern\VisitenkartenDetailsSpeichernUseCase::VISITENKARTE_BEARBEITEN_INFO_NO_CHANGE) {
      return $allgSettings->failNoChangesMsgVisitenkarte;
    }

    if ($this->useCaseResponse->statusCode === VisitenkartenDetailsSpeichern\VisitenkartenDetailsSpeichernUseCase::VISITENKARTE_BEARBEITEN_REFRESH
            || $this->useCaseResponse->statusCode === InsertVisitenkarteUseCase::VISITENKARTE_ERSTELLEN_BROWSER_REFRESH) {
      return $allgSettings->browserRefreshMsgVisitenkarte;
    }

    return '';
  }

  private function getAllgSettings()
  {
    return \AllgSettingsAPI::getAllgSettings();
  }

  protected function getAktuelleURLRelative()
  {
    $url = \URL::loadCurrent();

    return $url->getUrlRelative();
  }
}