package be.iminds.ilabt.jfed.lowlevel.api_wrapper;

import be.iminds.ilabt.jfed.util.GeniUrn;

import java.util.Objects;

/**
 * User: twalcari
 * Date: 7/2/2014
 * Time: 17:23
 */
public class ErrorDetails {
    private final String error;
    private final GeniUrn sliverUrn;
    private final GeniUrn componentUrn;
    private final long timestamp;

    public ErrorDetails(String error, GeniUrn sliverUrn, GeniUrn componentUrn) {
        assert (error != null);
        assert (!error.isEmpty());
        this.error = error;
        this.sliverUrn = sliverUrn;
        this.componentUrn = componentUrn;
        this.timestamp = System.currentTimeMillis();
    }

    public ErrorDetails(String error, GeniUrn sliverUrn) {
        this(error, sliverUrn, null);
    }

    public ErrorDetails(String error, String sliverUrn, String componentUrn) {
        this(error, GeniUrn.parse(sliverUrn), GeniUrn.parse(componentUrn));
    }

    public ErrorDetails(String error, String sliverUrn) {
        this(error, sliverUrn, null);
    }

    public ErrorDetails(String error) {
        this.error = error;
        this.sliverUrn = null;
        this.componentUrn = null;
        this.timestamp = System.currentTimeMillis();
    }

    public ErrorDetails(String error, GeniUrn sliverUrn,GeniUrn componentUrn,  long timestamp) {
        this.componentUrn = componentUrn;
        this.error = error;
        this.sliverUrn = sliverUrn;
        this.timestamp = timestamp;
    }

    public GeniUrn getSliverUrn() {
        return sliverUrn;
    }

    public GeniUrn getComponentUrn() {
        return componentUrn;
    }

    public String getError() {
        return error;
    }

    public long getTimestamp() {
        return timestamp;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ErrorDetails that = (ErrorDetails) o;

        if (timestamp != that.timestamp) return false;
        if (componentUrn != null ? !Objects.equals(componentUrn, that.componentUrn) : that.componentUrn != null) return false;
        if (error != null ? !Objects.equals(error, that.error) : that.error != null) return false;
        if (sliverUrn != null ? !Objects.equals(sliverUrn, that.sliverUrn) : that.sliverUrn != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = (int) (timestamp ^ (timestamp >>> 32));
        result = 31 * result + (error != null ? error.hashCode() : 0);
        result = 31 * result + (sliverUrn != null ? sliverUrn.hashCode() : 0);
        result = 31 * result + (componentUrn != null ? componentUrn.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "ErrorDetails{" +
                "timestamp=" + timestamp +
                ", error='" + error + '\'' +
                ", sliverUrn=" + sliverUrn +
                ", componentUrn=" + componentUrn +
                '}';
    }
}
