package be.iminds.ilabt.jfed.util;

import be.iminds.ilabt.jfed.fedmon.webapi.service.json.*;

import java.util.Objects;

/**
 * TargetAuthority: An authority to test or do something else with.
 *
 * This is simple 3 parts:
 *   - The original provided URN
 *   - The Server derived from it that is used for connections
 *   - The Server derived from it that is used as component_manager_id when generating RSpec's for it
 */
public class TargetAuthority {
    protected final GeniUrn origUrn;
    protected final GeniUrn rspecUrn;
    protected final Server serverForRspecComponentManager;
    protected final Server serverToConnect;

    public TargetAuthority(GeniUrn origUrn, Server serverForRspecComponentManager, Server serverToConnect) {
        this.origUrn = origUrn;
        this.serverForRspecComponentManager = serverForRspecComponentManager;
        try {
            this.rspecUrn = new GeniUrn(serverForRspecComponentManager.getDefaultComponentManagerUrn());
        } catch (GeniUrn.GeniUrnParseException e) {
            throw new IllegalArgumentException("Server urn did not parse", e);
        }
        this.serverToConnect = serverToConnect;
    }
//    public TargetAuthority(GeniUrn origUrn, GeniUrn rspecUrn, SfaAuthority serverToConnect) {
//        this.origUrn = origUrn;
//        this.rspecUrn = rspecUrn;
//        this.serverToConnect = serverToConnect;
//    }

    public GeniUrn getOrigUrn() {
        return origUrn;
    }

    public GeniUrn getRspecUrn() {
        return rspecUrn;
    }

    public Server getServerForRspecComponentManager() {
        return serverForRspecComponentManager;
    }

    public Server getServerToConnect() {
        return serverToConnect;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof TargetAuthority)) return false;

        TargetAuthority that = (TargetAuthority) o;

        if (!Objects.equals(origUrn, that.origUrn)) return false;
        if (!Objects.equals(rspecUrn, that.rspecUrn)) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = origUrn.hashCode();
        result = 31 * result + rspecUrn.hashCode();
        return result;
    }
}
