package cz.senslog.connector.tools.jwt;

import java.util.Objects;

public class JWClaim {

    private final Object claim;

    public JWClaim(Object claim) {
        Objects.requireNonNull(claim);
        this.claim = claim;
    }

    public long getAsLong() {
        if (claim instanceof Long) {
            return (Long) claim;
        } else if (claim instanceof Double) {
            return ((Double) claim).longValue();
        } else if (claim instanceof Integer) {
            return ((Integer) claim).longValue();
        }
        throw new ClassCastException("No long cast for the claim: " + claim);
    }

    public double getAsDouble() {
        if (claim instanceof Long) {
            return ((Long) claim).doubleValue();
        } else if (claim instanceof Double) {
            return (Double) claim;
        } else if (claim instanceof Integer) {
            return ((Integer) claim).doubleValue();
        }
        throw new ClassCastException("No double cast for the claim: " + claim);
    }

    public int getAsInt() {
        if (claim instanceof Long) {
            return ((Long) claim).intValue();
        } else if (claim instanceof Double) {
            return ((Double) claim).intValue();
        } else if (claim instanceof Integer) {
            return (Integer) claim;
        }
        throw new ClassCastException("No int cast for the claim: " + claim);
    }

    public boolean getAsBoolean() {
        if (claim instanceof Boolean) {
            return (Boolean) claim;
        }
        throw new ClassCastException("No boolean cast for the claim: " + claim);
    }

    public String getAsString() {
        return claim.toString();
    }

    @Override
    public String toString() {
        return getAsString();
    }
}
