package ru.micord.ervu.security.webbpm.jwt;

import java.util.Collection;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.SpringSecurityCoreVersion;

/**
 * @author zamaliev
 */
public class JwtAuthentication implements Authentication {

  private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;

  private final String userAccountId;
  private final Authentication authentication;
  private final String token;

  private final UserIdsPair userIdsPair;

  public JwtAuthentication(Authentication authentication, String userAccountId, String token) {
    this.userAccountId = userAccountId;
    this.userIdsPair = new UserIdsPair(userAccountId);
    this.authentication = authentication;
    this.token = token;
  }

  public JwtAuthentication(Authentication authentication, String userAccountId) {
    this(authentication, userAccountId, null);
  }

  public String getUserAccountId() {
    return userAccountId;
  }

  public UserIdsPair getUserIdsPair() {
    return userIdsPair;
  }

  @Override
  public Collection<? extends GrantedAuthority> getAuthorities() {
    return authentication.getAuthorities();
  }

  @Override
  public Object getCredentials() {
    return authentication.getCredentials();
  }

  @Override
  public Object getDetails() {
    return authentication.getDetails();
  }

  @Override
  public Object getPrincipal() {
    return authentication.getPrincipal();
  }

  @Override
  public boolean isAuthenticated() {
    if (authentication == null) {
      return false;
    }
    else {
      return authentication.isAuthenticated();
    }
  }

  @Override
  public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
    authentication.setAuthenticated(isAuthenticated);
  }

  @Override
  public String getName() {
    return authentication.getName();
  }

  public String getToken() {
    return this.token;
  }
}
