/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.cnr.iasi.giant.action;

import it.cnr.iasi.giant.Main;
import it.cnr.iasi.giant.io.IOWriter;
import it.cnr.iasi.giant.thread.NetworkLoaderThread;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.JOptionPane;
import org.cytoscape.model.CyEdge;
import org.cytoscape.model.CyNetwork;
import org.cytoscape.model.CyNode;

/**
 *
 * @author Fabio Cumbo
 * @info GIANT
 * @version v1.0
 * @organization IASI-CNR : Istituto di Analisi dei Sistemi ed Informatica 
 *                  "Antonio Ruberti" - Consiglio Nazionale delle Ricerche
 * 
 */
public class SelectNetworkAction extends Action {

    
    @Override
    public ArrayList<Object> execute(String command) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public ArrayList<Object> execute() {
        /*try {
            Main.getGiantAdapter().getCySwingApplication().getJMenu("File.Import.Network").doClick();
            Robot rbt = new Robot();
            rbt.keyPress(KeyEvent.VK_CONTROL);
            rbt.keyPress(KeyEvent.VK_L);
            rbt.keyRelease(KeyEvent.VK_CONTROL);
            rbt.keyRelease(KeyEvent.VK_L);
        } catch (Exception e) {e.printStackTrace();}*/
        
        /***********************************************************************/
        CyNetwork net = Main.getGiantAdapter().getCyApplicationManager().getCurrentNetwork();
        if (net.getNodeList().size()>1) {
            NetworkLoaderThread.setNetworkTitle(net.getRow(net).get(CyNetwork.NAME, String.class));
            HashMap<String, ArrayList<String>> degMap = fillDegreeMap(net);
            NetworkLoaderThread.setDegreeMap(degMap);
            IOWriter io = new IOWriter();
            NetworkLoaderThread.setNodesSetPath(io.writeTmpNodesSetFile(degMap.keySet().toArray()));
            JOptionPane.showMessageDialog(Main.getApp().getFrameOverride(), "Network Loaded Successfully!", "INFO", JOptionPane.INFORMATION_MESSAGE);
            if (Main.getGiantAdapter().getCyApplicationManager().getCurrentNetwork()!=null || !Main.getGiantAdapter().getCyApplicationManager().getCurrentNetwork().getNodeList().isEmpty()) {
                NetworkLoaderThread.setNodesSetPath(io.writeTmpNodesSetFile(degMap.keySet().toArray()));
                Main.getApp().getAttrib_button().setEnabled(true);
            }
        }
        /***********************************************************************/
        
        System.out.println("SelectNetworkAction");
        return null;
    }
    
    private HashMap<String, ArrayList<String>> fillDegreeMap(CyNetwork net) {
        HashMap<String, ArrayList<String>> result = new HashMap<String, ArrayList<String>>();
        ArrayList<CyEdge> list = new ArrayList<CyEdge>(net.getEdgeList());
        for (CyEdge edge: list) {
            if (result.containsKey(net.getRow((CyNode)edge.getSource()).get(CyNetwork.NAME, String.class))) {
                if (!result.get(net.getRow((CyNode)edge.getSource()).get(CyNetwork.NAME, String.class)).contains(net.getRow((CyNode)edge.getTarget()).get(CyNetwork.NAME, String.class)))
                    result.get(net.getRow((CyNode)edge.getSource()).get(CyNetwork.NAME, String.class)).add(net.getRow((CyNode)edge.getTarget()).get(CyNetwork.NAME, String.class));
            }
            else {
                ArrayList<String> neighbors = new ArrayList<String>();
                neighbors.add(net.getRow((CyNode)edge.getTarget()).get(CyNetwork.NAME, String.class));
                result.put(net.getRow((CyNode)edge.getSource()).get(CyNetwork.NAME, String.class), neighbors);
            }
            
            if (result.containsKey(net.getRow((CyNode)edge.getTarget()).get(CyNetwork.NAME, String.class))) {
                if (!result.get(net.getRow((CyNode)edge.getTarget()).get(CyNetwork.NAME, String.class)).contains(net.getRow((CyNode)edge.getSource()).get(CyNetwork.NAME, String.class)))
                    result.get(net.getRow((CyNode)edge.getTarget()).get(CyNetwork.NAME, String.class)).add(net.getRow((CyNode)edge.getSource()).get(CyNetwork.NAME, String.class));
            }
            else {
                ArrayList<String> neighbors = new ArrayList<String>();
                neighbors.add(net.getRow((CyNode)edge.getSource()).get(CyNetwork.NAME, String.class));
                result.put(net.getRow((CyNode)edge.getTarget()).get(CyNetwork.NAME, String.class), neighbors);
            }
        }
        return result;
    }
    
}
