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

import cytoscape.CyEdge;
import cytoscape.CyNetwork;
import cytoscape.CyNode;
import cytoscape.Cytoscape;
import edu.ucsd.bioeng.coreplugin.tableImport.actions.ImportNetworkTableAction;
import it.cnr.iasi.giant.Main;
import it.cnr.iasi.giant.frame.GIANT_NT;
import it.cnr.iasi.giant.io.IOWriter;
import it.cnr.iasi.giant.thread.NetworkLoaderThread;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

/**
 *
 * @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() {
        /*JFileChooser chooser = new JFileChooser();
        
        int code = chooser.showOpenDialog(GIANT.getFrames()[0]);
        File file;
        if (code == JFileChooser.APPROVE_OPTION) {
            file = chooser.getSelectedFile();
            if (file.exists()) {
                GIANT.getNetworkField().setText(file.getAbsolutePath());
                NetworkLoaderThread loader = new NetworkLoaderThread(file);
                loader.start();
            }
            else {
                JOptionPane.showMessageDialog(GIANT.getFrames()[0], "Specified file does not exist!", "ERROR", JOptionPane.ERROR_MESSAGE);
            }
        }*/
        
        /***********************************************************************/
        ImportNetworkTableAction act = new ImportNetworkTableAction();
        act.actionPerformed(null);

        CyNetwork net = Cytoscape.getCurrentNetworkView().getNetwork();
        Main.getApp().getNetworkField().setText(net.getTitle());
        NetworkLoaderThread.setNetworkTitle(net.getTitle());
        HashMap<String, ArrayList<String>> degMap = fillDegreeMap(net);
        NetworkLoaderThread.setDegreeMap(degMap);
        IOWriter io = new IOWriter();
        NetworkLoaderThread.setNodesSetPath(io.writeTmpNodesSetFile(degMap.keySet().toArray()));
        if (Cytoscape.getNetworkSet()!=null || !Cytoscape.getNetworkSet().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.edgesList());
        for (CyEdge edge: list) {
            if (result.containsKey(((CyNode)edge.getSource()).getIdentifier())) {
                if (!result.get(((CyNode)edge.getSource()).getIdentifier()).contains(((CyNode)edge.getTarget()).getIdentifier()))
                    result.get(((CyNode)edge.getSource()).getIdentifier()).add(((CyNode)edge.getTarget()).getIdentifier());
            }
            else {
                ArrayList<String> neighbors = new ArrayList<String>();
                neighbors.add(((CyNode)edge.getTarget()).getIdentifier());
                result.put(((CyNode)edge.getSource()).getIdentifier(), neighbors);
            }
            
            if (result.containsKey(((CyNode)edge.getTarget()).getIdentifier())) {
                if (!result.get(((CyNode)edge.getTarget()).getIdentifier()).contains(((CyNode)edge.getSource()).getIdentifier()))
                    result.get(((CyNode)edge.getTarget()).getIdentifier()).add(((CyNode)edge.getSource()).getIdentifier());
            }
            else {
                ArrayList<String> neighbors = new ArrayList<String>();
                neighbors.add(((CyNode)edge.getSource()).getIdentifier());
                result.put(((CyNode)edge.getTarget()).getIdentifier(), neighbors);
            }
        }
        return result;
    }
    
}
