package leigh.chain.pki;

import leigh.mecha.json.JSONObject;

/**
 * This message represents a naming claim.
 *
 * @author Alex Leigh
 */
public class NameClaim {
    public static String KEY_NAME = "n";
    public static String KEY_SERVICE_URL = "s";
    public static String KEY_VALIDITY_DAYS = "v";
    public static int MAX_VALIDITY = 31 * 6;

    private final String name;
    private final String serviceUrl;
    private final int validityDays;

    public NameClaim(String name, String serviceUrl, int validityDays) {
        this.name = name;
        this.serviceUrl = serviceUrl;
        this.validityDays = validityDays;

        if (validityDays < 0 || validityDays > MAX_VALIDITY) {
            throw new IllegalArgumentException("Invalid validity period.");
        }
    }

    /**
     * Formulate this name claim as a JSON request. This is the message that is
     * written to the blockchain. Note that the serialized version will not include
     * the origination address; this is contained within the transaction header stored
     * on the blockchain and does not need to be duplicated here.
     */
    public String toJSON() {
        JSONObject obj = new JSONObject();
        obj.put(KEY_NAME, name);
        obj.put(KEY_SERVICE_URL, serviceUrl);
        obj.put(KEY_VALIDITY_DAYS, validityDays);
        return obj.toString();
    }

    public String getName() {
        return name;
    }

    public String getServiceUrl() {
        return serviceUrl;
    }

    public int getValidityDays() {
        return validityDays;
    }
}
