package org.helioviewer.jhv.plugins.samp;

import java.io.IOException;

import org.astrogrid.samp.Message;
import org.astrogrid.samp.Metadata;
import org.astrogrid.samp.client.ClientProfile;
import org.astrogrid.samp.client.HubConnector;
import org.astrogrid.samp.hub.Hub;
import org.astrogrid.samp.hub.HubServiceMode;
import org.helioviewer.jhv.JHVGlobals;
import org.helioviewer.jhv.layers.Layers;

class SampClient extends HubConnector {

    private final static String MTYPE_VIEW_DATA = "jhv.vso.load";

    SampClient(ClientProfile _profile) {
        super(_profile);

        Hub[] runningHubs = Hub.getRunningHubs();
        if (runningHubs.length == 0) {
            try {
                Hub.checkExternalHubAvailability();
                Hub.runExternalHub(HubServiceMode.CLIENT_GUI);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        Metadata meta = new Metadata();
        // TODO: name / description
        meta.setName("JHelioviewer");
        meta.setDescriptionText(JHVGlobals.userAgent);
        declareMetadata(meta);
        declareSubscriptions(computeSubscriptions());

        setAutoconnect(10);
    }

    void notifyRequestData() {
        Message msg = new Message(MTYPE_VIEW_DATA);
        // Layers.getSAMPMessage(msg);
        try {
            getConnection().notifyAll(msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

@@ -63,16 +63,59 @@
 ception;
+%0Aimport java.net.URI;%0Aimport java.util.Map;
 %0A%0Aimport
@@ -212,29 +212,134 @@
 .client.
-ClientProfile
+AbstractMessageHandler;%0Aimport org.astrogrid.samp.client.ClientProfile;%0Aimport org.astrogrid.samp.client.HubConnection
 ;%0Aimport
@@ -499,16 +499,105 @@
 lobals;%0A
+import org.helioviewer.jhv.io.LoadURITask;%0Aimport org.helioviewer.jhv.layers.ImageLayer;%0A
 import o
@@ -1327,16 +1327,545 @@
 a(meta);
+%0A%0A        addMessageHandler(new AbstractMessageHandler(%22image.load.fits%22) %7B%0A            public Map%3C?,?%3E processCall(HubConnection c, String senderId, Message msg) %7B%0A                try %7B%0A                    URI uri = new URI(msg.getParam(%22url%22).toString());%0A                    JHVGlobals.getExecutorService().execute(new LoadURITask(ImageLayer.createImageLayer(null), uri));%0A                %7D catch (Exception e) %7B%0A                    e.printStackTrace();%0A                %7D%0A                return null;%0A            %7D%0A        %7D);
 %0A       

Allow loading of FITS via SAMP