package cz.senslog.connector.push.theros;

import java.time.LocalDateTime;
import java.util.Arrays;

public class AuthorizationToken {

    private final String accessToken;
    private final String tokenType;
    private final String[] scopes;
    private final LocalDateTime expirationTime;

    public static AuthorizationToken of(String accessToken, String tokenType, String scopes, int expiresInSeconds) {
        return new AuthorizationToken(accessToken, tokenType, scopes.split(" "), LocalDateTime.now().plusSeconds(expiresInSeconds));
    }

    public AuthorizationToken(String accessToken, String tokenType, String[] scopes, LocalDateTime expirationTime) {
        this.accessToken = accessToken;
        this.tokenType = tokenType;
        this.scopes = scopes;
        this.expirationTime = expirationTime;
    }

    public String getAccessToken() {
        return accessToken;
    }

    public String getTokenType() {
        return tokenType;
    }

    public String[] getScopes() {
        return scopes;
    }

    public LocalDateTime getExpirationTime() {
        return expirationTime;
    }

    public boolean isExpired() {
        return expirationTime.isBefore(LocalDateTime.now());
    }

    public boolean isNotExpired() {
        return !isExpired();
    }

    public boolean isValid() {
        return isNotExpired() && accessToken != null;
    }

    public static boolean isValid(AuthorizationToken token) {
        return token != null && token.isValid();
    }

    @Override
    public String toString() {
        return "{" +
                "accessToken:'" + accessToken + '\'' +
                ", tokenType:'" + tokenType + '\'' +
                ", scopes:" + Arrays.toString(scopes) +
                ", expirationTime:" + expirationTime +
                '}';
    }
}
