package org.yamcs.studio.core.model;

import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

import org.yamcs.api.ws.WebSocketRequest;
import org.yamcs.protobuf.Yamcs.Event;
import org.yamcs.studio.core.ConnectionManager;
import org.yamcs.studio.core.NotConnectedException;
import org.yamcs.studio.core.YamcsPlugin;
import org.yamcs.studio.core.web.ResponseHandler;
import org.yamcs.studio.core.web.RestClient;
import org.yamcs.studio.core.web.WebSocketRegistrar;
import org.yamcs.utils.TimeEncoding;

public class EventCatalogue implements Catalogue {

    private Set<EventListener> eventListeners = new CopyOnWriteArraySet<>();

    public static EventCatalogue getInstance() {
        return YamcsPlugin.getDefault().getCatalogue(EventCatalogue.class);
    }

    public void addEventListener(EventListener listener) {
        eventListeners.add(listener);
    }

    @Override
    public void onStudioConnect() {
        WebSocketRegistrar webSocketClient = ConnectionManager.getInstance().getWebSocketClient();
        webSocketClient.sendMessage(new WebSocketRequest("events", "subscribe"));
    }

    @Override
    public void instanceChanged(String oldInstance, String newInstance) {
        // Don't assume anything. Let listeners choose whether they want
        // to register as an instance listeners.
    }

    @Override
    public void onStudioDisconnect() {
    }

    public void processEvent(Event event) {
        eventListeners.forEach(l -> l.processEvent(event));
    }

    public void downloadEvents(long start, long stop, ResponseHandler responseHandler) {
        String instance = ManagementCatalogue.getCurrentYamcsInstance();
        String resource = "/archive/" + instance + "/downloads/events";
        if (start != TimeEncoding.INVALID_INSTANT) {
            resource += "?start=" + start;
            if (stop != TimeEncoding.INVALID_INSTANT) {
                resource += "&stop=" + stop;
            }
        } else if (stop != TimeEncoding.INVALID_INSTANT) {
            resource += "?stop=" + stop;
        }
        RestClient restClient = ConnectionManager.getInstance().getRestClient();
        if (restClient != null) {
            restClient.streamGet(resource, null, () -> Event.newBuilder(), responseHandler);
        } else {
            responseHandler.onException(new NotConnectedException());
        }
    }
}

@@ -145,16 +145,67 @@
 equest;%0A
+import org.yamcs.protobuf.Rest.ListEventsResponse;%0A
 import o
@@ -231,24 +231,24 @@
 amcs.Event;%0A
-
 import org.y
@@ -921,32 +921,142 @@
 stener);%0A    %7D%0A%0A
+    public void removeEventListener(EventListener listener) %7B%0A        eventListeners.remove(listener);%0A    %7D%0A%0A
     @Override%0A  
@@ -1594,24 +1594,24 @@
 nt event) %7B%0A
-
         even
@@ -1657,32 +1657,493 @@
 event));%0A    %7D%0A%0A
+    public void fetchLatestEvents(String instance, ResponseHandler responseHandler) %7B%0A        String resource = %22/archive/%22 + instance + %22/events%22;%0A        RestClient restClient = ConnectionManager.getInstance().getRestClient();%0A        if (restClient != null) %7B%0A            restClient.get(resource, null, ListEventsResponse.newBuilder(), responseHandler);%0A        %7D else %7B%0A            responseHandler.onException(new NotConnectedException());%0A        %7D%0A    %7D%0A%0A
     public void 

Add utility method for fetching latest events