package leigh.chain.directory;

import java.io.Serial;
import java.io.Serializable;
import java.time.Instant;
import java.util.Objects;

/**
 * This class represents a recognized identity within the directory service. In an Etherium blockchain implementation
 * the identity should have a 1:1 relationship with a wallet.
 *
 * @author Alex Leigh
 */
public class Identity implements Serializable {
    @Serial
    private static final long serialVersionUID = 0L;
    private String address;
    private String name;
    private Instant nameClaimExpire;
    private String serviceUrl;

    public void setNameClaimExpire(Instant nameClaimExpire) {
        this.nameClaimExpire = nameClaimExpire;
    }

    /**
     * Returns <code>true</code> if the expiry time for this claim is after the current time.
     */
    public boolean isNameClaimValid(Instant when) {
        if(nameClaimExpire==null) return false;
        return !nameClaimExpire.isBefore(when);
    }

    public String getServiceUrl() {
        return serviceUrl;
    }

    public void setServiceUrl(String serviceUrl) {
        this.serviceUrl = serviceUrl;
    }

    public String getAddress() {
        return address;
    }

    public Instant getNameClaimExpire() {
        return nameClaimExpire;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Identity identity = (Identity) o;
        return Objects.equals(address, identity.address);
    }

    @Override
    public int hashCode() {
        return Objects.hash(address);
    }

    @Override
    public String toString() {
        return "Identity{" +
                "address='" + address + '\'' +
                ", name='" + name + '\'' +
                ", nameClaimExpire=" + nameClaimExpire +
                ", serviceUrl='" + serviceUrl + '\'' +
                '}';
    }
}
