package ru.micord.ervu.security.esia.config;

import java.util.Arrays;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author Eduard Tihomirov
 */
@Component
public class EsiaConfig {

  @Value("${esia.scopes}")
  private String esiaScopes;

  @Value("${esia.org.scopes}")
  private String esiaOrgScopes;

  @Value("${esia.org.scope.url}")
  private String orgScopeUrl;

  @Value("${esia.base.uri}")
  private String esiaBaseUri;

  @Value("${esia.client.id}")
  private String clientId;

  @Value("${esia.redirect.url}")
  private String redirectUrl;

  @Value("${esia.logout.redirect.url}")
  private String logoutRedirectUrl;

  @Value("${sign.url}")
  private String signUrl;

  @Value("${esia.client.cert.hash}")
  private String clientCertHash;

  @Value("${request.timeout:20}")
  private long requestTimeout;

  @Value("${connection.timeout:10}")
  private long connectionTimeout;

  @Value("${esia.logout.url:idp/ext/Logout}")
  private String esiaLogoutUrl;

  @Value("${esia.code.url:aas/oauth2/v2/ac}")
  private String esiaCodeUrl;

  @Value("${esia.token.url:aas/oauth2/v3/te}")
  private String esiaTokenUrl;

  @Value("${esia.upload.data.role}")
  private String esiaUploadDataRole;

  @Value("${sign.verify.url}")
  private String signVerifyUrl;

  @Value("${esia.issuer.url}")
  private String esiaIssuerUrl;

  @Value("${esia.marker.ver}")
  private String esiaMarkerVer;

  @Value("${esia.state.cookie.life.time:300}")
  private long esiaStateCookieLifeTime;

  @Value("${esia.login.attempts.count:5}")
  private long esiaLoginAttemptsCount;

  public String getEsiaOrgScopes() {
    String[] scopeItems = esiaOrgScopes.split(",");
    return String.join(" ", Arrays.stream(scopeItems).map(item -> orgScopeUrl + item.trim()).toArray(String[]::new));
  }

  public String getEsiaScopes() {
    String[] scopeItems = esiaScopes.split(",");
    return String.join(" ", Arrays.stream(scopeItems).map(String::trim).toArray(String[]::new));
  }

  public String getClientId() {
    return clientId;
  }

  public String getRedirectUrl() {
    return redirectUrl;
  }

  public String getEsiaBaseUri() {
    return esiaBaseUri;
  }

  public String getSignUrl() {
    return signUrl;
  }

  public String getClientCertHash() {return clientCertHash;}

  public long getRequestTimeout() {
    return requestTimeout;
  }

  public long getConnectionTimeout() {
    return connectionTimeout;
  }

  public String getEsiaLogoutUrl() {
    return esiaLogoutUrl;
  }

  public String getEsiaCodeUrl() {
    return esiaCodeUrl;
  }

  public String getEsiaTokenUrl() {
    return esiaTokenUrl;
  }

  public String getLogoutRedirectUrl() {
    return logoutRedirectUrl;
  }

  public String getSignVerifyUrl() {
    return signVerifyUrl;
  }

  public String getEsiaUploadDataRole() {
    return esiaUploadDataRole;
  }

  public String getEsiaIssuerUrl() {
    return esiaIssuerUrl;
  }

  public String getEsiaMarkerVer() {
    return esiaMarkerVer;
  }

  public long getEsiaStateCookieLifeTime() {
    return esiaStateCookieLifeTime;
  }

  public long getEsiaLoginAttemptsCount() {
    return esiaLoginAttemptsCount;
  }
}
