package cz.senslog.connector.tools.jwt;

import java.util.Map;

public class JWTPayload {

    /** sub (subject): Subject of the JWT (the user) */
    private final String sub;

    /** iss (issuer): Issuer of the JWT */
     private final String iss;

     /** aud (audience): Recipient for which the JWT is intended */
    private final String aud;

    /** exp (expiration time): Time after which the JWT expires */
    private final long exp;

    /** iat (issued at time): Time at which the JWT was issued; can be used to determine age of the JWT */
    private final long iat;

    /** jti (JWT ID): Unique identifier; can be used to prevent the JWT from being replayed (allows a token to be used only once) */
    private final String jti;

    private final Map<String, JWClaim> claims;

    public JWTPayload(String sub, String iss, String aud, long exp, long iat, String jti, Map<String, JWClaim> claims) {
        this.sub = sub;
        this.iss = iss;
        this.aud = aud;
        this.exp = exp;
        this.iat = iat;
        this.jti = jti;
        this.claims = claims;
    }

    public String getSub() {
        return sub;
    }

    public String getIss() {
        return iss;
    }

    public String getAud() {
        return aud;
    }

    public long getExp() {
        return exp;
    }

    public long getIat() {
        return iat;
    }

    public String getJti() {
        return jti;
    }

    public JWClaim getClaim(String claimName, Object defValue) {
        return claims.getOrDefault(claimName, new JWClaim(defValue));
    }

    public JWClaim getClaim(String claimName) {
        return claims.get(claimName);
    }

    public JWClaim remove(String claimName) {
        return claims.remove(claimName);
    }

    public JWClaim remove(String claimName, Object defValue) {
        if (claims.containsKey(claimName)) {
            return claims.remove(claimName);
        }
        return new JWClaim(defValue);
    }
}
