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

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

/**
 * Returns the Debugging SfaAuthority or adds it to a list.
 *
 * This authority is completely fake and only useful for debugging.
 */
public class DebuggingAuthorityList {
    private DebuggingAuthorityList() {    }

    private static Testbed debuggingTestbed = null;
    private static Server debuggingServer = null;

    public static String getDebuggingAuthUrn() {
        return "urn:publicid:IDN+debug.jfed.iminds.be+authority+cm";
    }

    public static boolean isDebugging(Service service) {
        getDebuggingServer();
        assert (service.getServer() == debuggingServer) == (service.getServer().getId().equals(debuggingServer.getId()));
        return service.getServer() == debuggingServer;
    }
    public static boolean isDebugging(Server server) {
        getDebuggingServer();
        assert (server == debuggingServer) == (server.getId().equals(debuggingServer.getId()));
        return server == debuggingServer;
    }
    public static boolean isDebugging(Testbed testbed) {
        getDebuggingServer();
        assert (testbed == debuggingTestbed) == (testbed.getId().equals(debuggingTestbed.getId()));
        return testbed == debuggingTestbed;
    }

    private static void addService(ServerBuilder serverBuilder, ApiInfo.ApiName api, int version, String url, String urn) {
        ServiceBuilder serviceBuilder = new ServiceBuilder();
        serviceBuilder.setApi(api.getId());
        serviceBuilder.setApiVersion(version + "");
        serviceBuilder.setUrl(url);
        serviceBuilder.setUrn(urn);
        serverBuilder.addServiceBuilder(serviceBuilder);
    }

    public static Server getDebuggingServer() {
        if (debuggingTestbed == null || debuggingServer == null) {
            String debugUrn = getDebuggingAuthUrn();
            TestbedBuilder testbedBuilder = new TestbedBuilder();
            ServerBuilder serverBuilder = new ServerBuilder();
            testbedBuilder.addServerBuilder(serverBuilder);
            testbedBuilder.setLongName("jFed Debug Authority");
            serverBuilder.setName("jFed Debug Authority");
            testbedBuilder.setDefaultComponentManagerUrn(debugUrn);
            serverBuilder.setDefaultComponentManagerUrn(debugUrn);
            serverBuilder.setServerType("debugging");
            
                //these URL are not supposed to be get used
                //  the debug system should be in place automatically and handle them
            addService(serverBuilder, ApiInfo.ApiName.GENI_AM, 2, "https://debug.jfed.iminds.be/example/AM/2", debugUrn);
            addService(serverBuilder, ApiInfo.ApiName.GENI_AM, 3, "https://debug.jfed.iminds.be/example/AM/3", debugUrn);
            addService(serverBuilder, ApiInfo.ApiName.GENI_CH, 1, "https://debug.jfed.iminds.be/example/GENI_CH/1", debugUrn);
            addService(serverBuilder, ApiInfo.ApiName.GENI_CH_MA, 1, "https://debug.jfed.iminds.be/example/GENI_CH_MA/1", debugUrn);
            addService(serverBuilder, ApiInfo.ApiName.GENI_CH_SA, 1, "https://debug.jfed.iminds.be/example/GENI_CH_SA/1", debugUrn);
            addService(serverBuilder, ApiInfo.ApiName.PLANETLAB_SLICE_REGISTRY, 1, "https://debug.jfed.iminds.be/example/PL/SR/1", debugUrn);
            addService(serverBuilder, ApiInfo.ApiName.PROTOGENI_CH, 1, "https://debug.jfed.iminds.be/example/PG/CH/1", debugUrn);
            addService(serverBuilder, ApiInfo.ApiName.PROTOGENI_SA, 1, "https://debug.jfed.iminds.be/example/PG/SA/1", debugUrn);
            addService(serverBuilder, ApiInfo.ApiName.GENI_SCS, 1, "https://debug.jfed.iminds.be/example/SCS/1", debugUrn);

            debuggingTestbed = testbedBuilder.create();
            debuggingServer = debuggingTestbed.getServers().get(0);
        }
        assert debuggingServer != null;
        assert debuggingTestbed != null;

        return debuggingServer;
    }
}
