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

namespace Kufer\KuferTools\Mitarbeiter\AdressInfo\AdressUebersicht\AdressDetails;

use Kufer\KuferTools\Mitarbeiter\AdressInfo\NewAdress\HandleAdressInsert\HandleAdressInsertUseCase as HandleAdressInsertUseCase;

class AdressDetailsPresenter extends \Kufer\KuferTools\Basis\Presenter\MitRahmenPresenter
{
  protected function getView()
  {
    return new AdressDetailsView($this->getViewModel());
  }

  protected function getViewModel()
  {
    $viewModel = new AdressDetailsViewModel();
    $viewModel->adress = $this->useCaseResponse->adress;
    $viewModel->adress->nextAnr = $this->useCaseResponse->nextAnr;
    $viewModel->adress->previousAnr = $this->useCaseResponse->previousAnr;
    $viewModel->adress->backLink = $this->useCaseResponse->backLink;
    $viewModel->adress->backButtonLabel = $this->useCaseResponse->backButtonLabel;
    $viewModel->adress->ueberschrift = $this->useCaseResponse->ueberschrift;
    

    $allgSettings = \AllgSettingsAPI::getAllgSettings();
    $viewModel->newAdrMsgSuccess = $this->getNewAdressMsgSuccess($allgSettings);
    $viewModel->newAdrMsgInfo = $this->getNewAdressMsgInfo($allgSettings);
    $viewModel->newAdrMsgFehler = $this->getNewAdressMsgFehler($allgSettings);

    $viewModel->adress->reiterButtons = $this->getButtonHtml();
    $viewModel->adress->toolId = $this->useCaseResponse->toolId;
    
    $viewModel->reiterHtml = $this->getReiter();

    return $viewModel;
  }

  private function getReiter()
  {
    $viewModel = new ReiterViewModel();
    $viewModel->adress = $this->useCaseResponse->adress;
    $reiterAry = $this->useCaseResponse->reiter;
    $viewModel->reiter = $reiterAry;
    $view = new ReiterView($viewModel);

    return $view->show();
  }

  private function getNewAdressMsgSuccess($allgSettings)
  {
    if ($this->useCaseResponse->newAdrStatusMsg == HandleAdressInsertUseCase::SUCCESS_ADRESS_ERFOLGREICH_UEBERMITTELT) {
      return $allgSettings->successMsgAdresseNeu;
    }

    return '';
  }

  private function getNewAdressMsgInfo($allgSettings)
  {
    if ($this->useCaseResponse->newAdrStatusMsg == HandleAdressInsertUseCase::INFO_ADRESSDATEN_NICHT_VERAENDERT) {
      return $allgSettings->failMsgChangeAdresse;
    }

    return '';
  }

  
  private function getNewAdressMsgFehler($allgSettings)
  {
    if ($this->useCaseResponse->newAdrStatusMsg == HandleAdressInsertUseCase::ERROR_UEBERTRAGUNG_FEHLGESCHLAGEN) {
      return 'Fehler: ' . $allgSettings->failSubmitMsgAdresse;
    }
    if ($this->useCaseResponse->newAdrStatusMsg == HandleAdressInsertUseCase::ERROR_ADRESSDATEN_NICHT_AKTUELL) {
      return 'Fehler: ' . $allgSettings->failMsgAdresseNeu;
    }
    if ($this->useCaseResponse->newAdrStatusMsg == HandleAdressInsertUseCase::ERROR_ADRESSDATEN_NICHT_AKTUELL) {
      return 'Fehler: ' . $allgSettings->failSubmitMsgConflict;
    }
    if ($this->useCaseResponse->newAdrStatusMsg == HandleAdressInsertUseCase::ERROR_DOPPELT_AUFGERUFEN) {
      return $allgSettings->failMsgAlreadySubmitted;
    }
    if ($this->useCaseResponse->newAdrStatusMsg == HandleAdressInsertUseCase::ERROR_ADRESSDATEN_FEHLERHAFT) {
      return \Kufer\KuferTools\Utils\WssHandler::getErrorMessage($this->useCaseResponse->newAdrWrongFields);
    }

    return '';
  }

  
  protected function getJSDeclarations()
  {
    $result = parent::getJSDeclarations();
    $result .= \JsOperations::getDeclarationString('KuferTools.Anr', $this->useCaseResponse->adress->id);
    $result .= \JsOperations::getDeclarationString('KuferTools.Aguid', !empty($this->useCaseResponse->adress->guid) ? (string)$this->useCaseResponse->adress->guid : '');
    
    $result .= \JsOperations::getDeclarationString('KuferTools.OeffneReiter', $this->useCaseResponse->oeffneReiter);
    $result .= \JsOperations::getDeclarationString('KuferTools.ActionMerkeReiter', 'merkeAdressDetailsReiter');
    $result .= 'registerNamespace("KuferTools.KursDetails");' . "\n";
    $result .= 'registerNamespace("KuferTools.PersonDetails"); ' . "\n";
    $result .= \JsOperations::getDeclarationString('KuferTools.PersonDetails.Name', htmlentities($this->useCaseResponse->adress->namevorname, ENT_QUOTES));

    return $result;
  }

  
  protected function getJsFilesSrcAryWeitere()
  {
    return array($this->getJsFileFromSys('kursdetails.js'),
        $this->getJsFileFromJQuery('galleria/galleria.min.js', \CodingOperations::getEncoding4ExternalKuferToolsJsFiles()),
        $this->getJsFileFromOldTools('pdfdruck.js'),
        $this->getJsFileFromSys('pdfdruck.js'),
        $this->getJsFileFromSys('filter.js'),
        $this->getJsFileFromSys('datatables.js'),
        $this->getJsFileFromSys('kursdozenten.js'),
        $this->getJsFileFromSys('personendetails.js')
        );
  }

  protected function getButtonHtml()
  {
    $buttonleiste = new \Kufer\KuferTools\Basis\Button\Buttonleiste();

    return $buttonleiste->getHtmlButtonleiste($this->useCaseResponse->reiterButtons, 'kursdetails_buttons');
  }
}