/* ----------------------------------------
 * Origin  : GFI INFORMATIQUE 
 * Project : CDPP Propagation Tool 
 * ----------------------------------------
 * History 
 * Version : 01/05/2015 : SCA : V2.1
 * ----------------------------------------
 */
package fr.tlse.irap.pt.client.controller.tools.samp;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.IOException;

import javax.swing.JComponent;

import org.astrogrid.samp.Message;
import org.astrogrid.samp.Metadata;
import org.astrogrid.samp.Subscriptions;
import org.astrogrid.samp.client.ClientProfile;
import org.astrogrid.samp.client.DefaultClientProfile;
import org.astrogrid.samp.client.SampException;
import org.astrogrid.samp.gui.GuiHubConnector;
import org.astrogrid.samp.httpd.UtilServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JSampFrame extends javax.swing.JFrame {
    
	private static final long serialVersionUID = 1L;
	private static final Logger log = LoggerFactory.getLogger(JSampFrame.class);
    private javax.swing.JButton jbConn;
    private javax.swing.JButton jbOut;
	
    private GuiHubConnector ghConnector = null;
    
    /**
     * Creates new form JSAMPFrame
     */
    public JSampFrame() {
        initComponents();
        // Construct a connector
        ClientProfile profile = DefaultClientProfile.getProfile();
        ghConnector = new GuiHubConnector(profile);

        // Configure it with metadata about this application
        Metadata meta = new Metadata();
        meta.setName("PropTool");
        meta.setDescriptionText("Propagation Tool Client");
        try {
        	// set icon
        	//String logoUrl = UtilServer.getInstance().exportResource("/images/favicon64.ico").toString();
            String logoUrl = UtilServer.getInstance().exportResource("/images/favicon.png").toString();
	        meta.setIconUrl(logoUrl);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
                 
        ghConnector.declareMetadata(meta);
        
        Subscriptions lMap = ghConnector.computeSubscriptions();
        lMap.addMType("table.load.votable");
        
        // This step required even if no custom message handlers added.
        ghConnector.declareSubscriptions(lMap);

        jbConn.setAction(ghConnector.createRegisterAction());
        jbOut.setAction(ghConnector.createUnregisterAction());
        
        JComponent lJp = ghConnector.createMonitorPanel();
        add(lJp,BorderLayout.CENTER);
        
        setSize(500, 500);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation(screenSize.width/3, screenSize.height/4);
        revalidate();
    }

    /**
     * Send VOTable format url to SAMP Hub
     * @param url
     */
    public void sendVOTable(String url) {
		try {
	       	Message msg = new Message("table.load.votable").addParam( "url", url );
            log.info("Send " + msg.getMType() +" to hub for all subscriptors");
            ghConnector.getConnection().notifyAll(msg);
		} catch (SampException e) {
			e.printStackTrace();
		}
    }
    
    /**
     * Send FITS format url to SAMP Hub
     * @param url
     * @throws SampException 
     */
	public void sendFits(String url) throws SampException {
       	Message msg = new Message("image.load.fits").addParam( "url", url );
        log.info("Send " + msg.getMType() +" to hub for all subscriptors");
        ghConnector.getConnection().notifyAll(msg);
	}
	
	/**
     * This method is called from within the constructor to initialize the form.
     */
    private void initComponents() {

        jbConn = new javax.swing.JButton();
        jbOut = new javax.swing.JButton();

        //setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setTitle("PropTool Samp Client Monitor");

        jbConn.setText("GuiConnector");
        getContentPane().add(jbConn, java.awt.BorderLayout.NORTH);

        jbOut.setText("disconnect");
        getContentPane().add(jbOut, java.awt.BorderLayout.PAGE_END);

        pack();
    }

    /**
     * Disconnect from hub before window dispose.
     */
    @Override
    public void dispose() {
        try {
            if (ghConnector != null && ghConnector.getConnection() != null) {
            	log.debug("Disconnect from hub");
                ghConnector.getConnection().unregister();
            }
        } catch (SampException ex) {
            log.warn(ex.getMessage(), ex);
        }
                
        super.dispose();
    }

}
