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

namespace Kufer\KuferTools\Oeffentlich\PasswortAendern\SendePasswortAendernAnfordern;

use Kufer\KuferTools\Oeffentlich\PasswortAnfordern\Utils\HandlePasswortAnfordernEingaben as HandlePasswortAnfordern;

class SendePasswortAendernAnfordernPresenter extends \Kufer\KuferTools\Basis\Presenter\MitRahmenPresenter
{
  protected function getContent()
  {
    if ($this->useCaseResponse->ktPwVergessenVorRateAngriffSchuetzen) {
      $this->useCaseResponse->statusCodes = $this->filterUnsecureStatusCodes($this->useCaseResponse->statusCodes);
    }

    $view = $this->getView();

    return $view->getBody();
  }

  
  private function filterUnsecureStatusCodes($statusCodes)
  {
    $dontShowStatus = $this->unsecureStatusCodes();
    $showableStatus = array_filter($statusCodes, function($statusCode) use ($dontShowStatus) {return !in_array($statusCode, $dontShowStatus);});

    if (empty($showableStatus)) {
      $showableStatus[] = HandlePasswortAnfordern::SUCCESS_DBLOPTIN_EMAIL_VERSANDT;
    }

    return $showableStatus;
  }

  
  private function unsecureStatusCodes()
  {
    return array(
      HandlePasswortAnfordern::ERROR_LOGIN_NICHT_GEFUNDEN,
      HandlePasswortAnfordern::ERROR_LOGIN_OHNE_EMAIL,
      HandlePasswortAnfordern::ERROR_FEHLER_BEIM_DBLOPTIN_EMAIL_VERSAND
    );
  }

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

  protected function getViewModel()
  {
    $viewModel = new SendePasswortAendernAnfordernViewModel();

    $viewModel->login = $this->useCaseResponse->login;
    $viewModel->https = $this->useCaseResponse->https;
    $viewModel->captcha_link_erweiterung = \time();
    $viewModel->show_logininfo = !$this->useCaseResponse->ktPwVergessenVorRateAngriffSchuetzen;
    $viewModel->ktPwVergessenVorRateAngriffSchuetzen = $this->useCaseResponse->ktPwVergessenVorRateAngriffSchuetzen;
    $viewModel->email_censored = $this->useCaseResponse->email_censored;
    $viewModel->email_betreff = $this->useCaseResponse->email_betreff;
    $viewModel->expiration_hours = $this->useCaseResponse->expiration_hours;

    if ($viewModel->ktPwVergessenVorRateAngriffSchuetzen) {
      $viewModel->userdata = new \Datamodul();
    } else {
      $viewModel->userdata = $this->useCaseResponse->userdata;
    }

    return $viewModel;
  }
}