package ru.micord.ervu.account_applications.security.context;

import java.util.Set;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import ru.micord.ervu.account_applications.security.model.UserSession;
import ru.micord.ervu.account_applications.security.model.jwt.authentication.JwtTokenAuthentication;
import ru.micord.ervu.account_applications.security.model.role.ErvuPermissionAuthority;


@Component
public class SecurityContextImpl
    implements SecurityContext {
  @Override
  public String getDomainId() {
    JwtTokenAuthentication auth = (JwtTokenAuthentication) SecurityContextHolder.getContext().getAuthentication();
    return auth != null ? auth.getUserSession().domainId() : null;
    }

  @Override
  public Set<ErvuPermissionAuthority> getPermissions() {
    JwtTokenAuthentication auth = (JwtTokenAuthentication) SecurityContextHolder.getContext().getAuthentication();
    return auth != null ? auth.getUserSession().permissions() : null;
  }

  @Override
  public String getUserId() {
    JwtTokenAuthentication auth = (JwtTokenAuthentication) SecurityContextHolder.getContext().getAuthentication();
    return auth != null ? auth.getUserSession().userId() : null;
  }

  @Override
  public String getToken() {
    JwtTokenAuthentication auth = (JwtTokenAuthentication) SecurityContextHolder.getContext().getAuthentication();
    return auth != null ? (String) auth.getCredentials() : null;
  }

  @Override
  public UserSession getUserSession() {
    JwtTokenAuthentication auth = (JwtTokenAuthentication) SecurityContextHolder.getContext().getAuthentication();
    return auth != null ? auth.getUserSession() : null;
  }
}
