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

import it.cnr.iasi.giant.Main;
import it.cnr.iasi.giant.io.IOWriter;
import it.cnr.iasi.giant.util.Helper;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.ProgressMonitorInputStream;
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 NetworkLoaderThread extends Thread {
    
    private File file;
    private static String networkTitle;
    private static String nodesSet = "NULL";
    private static HashMap<String, ArrayList<String>> degreeMap = new HashMap<String, ArrayList<String>>();
    private static ArrayList<String> nodesMap = new ArrayList<String>();
    
    public NetworkLoaderThread(File file) {
        super(file.getName());
        this.file = file;
    }
    
    @Override
    public void run() {
        try {
            JFrame frame = new JFrame();
            frame.setVisible(true);
            frame.setAlwaysOnTop(true);
            try {
                InputStream fstream = new FileInputStream(file.getAbsolutePath());
                    InputStream fstreamMonitor = new ProgressMonitorInputStream(frame, "Loading...", fstream);
                    DataInputStream in = new DataInputStream(fstreamMonitor); 
                    BufferedReader br = new BufferedReader(new InputStreamReader(in));
                frame.setVisible(false);
                
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(NetworkLoaderThread.class.getName()).log(Level.SEVERE, null, ex);
                }
                
                String strLine;
                
                CyNetwork cyNetwork = Main.getGiantAdapter().getCyNetworkFactory().createNetwork();
                cyNetwork.getRow(cyNetwork).set(CyNetwork.NAME, file.getName());
                try {
                    int indexSource = 0;
                    //int indexInteractionType = 1;
                    //int indexTarget = 2;
                    int indexTarget = 1;
                    while ((strLine = br.readLine()) != null) {
                        Helper helper = new Helper();
                        ArrayList<String> line = helper.spacePattern(strLine);
                        
                        CyNode node0 = cyNetwork.addNode();
                        cyNetwork.getRow(node0).set(CyNetwork.NAME, line.get(indexSource));
                        //String interactionType = line.get(indexInteractionType);
			CyNode node1 = cyNetwork.addNode();
                        cyNetwork.getRow(node1).set(CyNetwork.NAME, line.get(indexTarget));
                        cyNetwork.addEdge(node0, node1, true);
                        //CyEdge edge = Cytoscape.getCyEdge(node0, node1, Semantics.INTERACTION, interactionType, true, false);
                        if (degreeMap.containsKey(line.get(indexSource))) {
                            if (!degreeMap.get(line.get(indexSource)).contains(line.get(indexTarget)))
                                    degreeMap.get(line.get(indexSource)).add(line.get(indexTarget));
                        }
                        else {
                            ArrayList<String> deg = new ArrayList<String>();
                            deg.add(line.get(indexTarget));
                            degreeMap.put(line.get(indexSource), deg);
                        }
                        
                        if (degreeMap.containsKey(line.get(indexTarget))) {
                            if (!degreeMap.get(line.get(indexTarget)).contains(line.get(indexSource)))
                                    degreeMap.get(line.get(indexTarget)).add(line.get(indexSource));
                        }
                        else {
                            ArrayList<String> deg = new ArrayList<String>();
                            deg.add(line.get(indexSource));
                            degreeMap.put(line.get(indexTarget), deg);
                        }
                    }
                    
                    for (String arr: degreeMap.keySet())
                        System.out.println(arr + " : " + degreeMap.get(arr));
                    
                    Main.getGiantAdapter().getCyNetworkViewFactory().createNetworkView(cyNetwork);
                    Main.getGiantAdapter().getCyApplicationManager().setCurrentNetwork(cyNetwork);
                    
                    br.close();
                    in.close();
                    fstreamMonitor.close();
                    fstream.close();
                    
                    if (Main.getGiantAdapter().getCyApplicationManager().getCurrentNetwork()!=null || !Main.getGiantAdapter().getCyApplicationManager().getCurrentNetwork().getNodeList().isEmpty()) {
                        IOWriter io = new IOWriter();
                        nodesSet = io.writeTmpNodesSetFile(degreeMap.keySet().toArray());
                        Main.getApp().getAttrib_button().setEnabled(true);
                    }
                    
                    networkTitle = file.getName();
                    ClusterLoaderThread.setClusters(new HashMap<Integer, ArrayList<String>>());
                    ClusterLoaderThread.setClustersKRange(new HashMap<Integer, HashMap<Integer, ArrayList<String>>>());
                    
                }
                catch (NullPointerException e) {                                
                    br.close();
                    in.close();
                    fstreamMonitor.close();
                    fstream.close();
                }
            }
            catch (FileNotFoundException e) {}
        } catch (IOException e) { }
    }
    
    public static String getNetworkTitle() {
        return networkTitle;
    }
    
    public static void setNetworkTitle(String title) {
        networkTitle = title;
    }
    
    public static HashMap<String, ArrayList<String>> getDegreeMap() {
        return degreeMap;
    }
    
    public static void setDegreeMap(HashMap<String, ArrayList<String>> map) {
        degreeMap = map;
    }
    
    public static ArrayList<String> getNodesMap() {
        return nodesMap;
    }
    
    public static void setNodesMap(ArrayList<String> map) {
        nodesMap = map;
    }
    
    public static String getNodesSetPath() {
        return nodesSet;
    }
    
    public static void setNodesSetPath(String setPath) {
        nodesSet = setPath;
    }
    
    public static double[][] getAdjMatrix() {
        String netID = "NULL";
        if (Main.getGiantAdapter().getCyApplicationManager().getCurrentNetwork().getRow(Main.getGiantAdapter().getCyApplicationManager().getCurrentNetwork()).get(CyNetwork.NAME, String.class).equals(getNetworkTitle())) {
            netID = Main.getGiantAdapter().getCyApplicationManager().getCurrentNetwork().getRow(Main.getGiantAdapter().getCyApplicationManager().getCurrentNetwork()).get(CyNetwork.NAME, String.class);
        }
        if (!netID.equals("NULL")) {
            ArrayList<CyNode> vert = new ArrayList<CyNode>(Main.getGiantAdapter().getCyApplicationManager().getCurrentNetwork().getNodeList());
            double[][] adj = new double[vert.size()][vert.size()];
            for (int i=0; i<vert.size(); i++) {
                nodesMap.add(Main.getGiantAdapter().getCyApplicationManager().getCurrentNetwork().getRow(vert.get(i)).get(CyNetwork.NAME, String.class));
                for (int k=i; k<vert.size(); k++) {
                    if (Main.getGiantAdapter().getCyApplicationManager().getCurrentNetwork().getRow(vert.get(i)).get(CyNetwork.NAME, String.class).equals(Main.getGiantAdapter().getCyApplicationManager().getCurrentNetwork().getRow(vert.get(k)).get(CyNetwork.NAME, String.class))) {
			adj[i][k] = 0;
                        adj[k][i] = 0;
                    }
                    else if ((Main.getGiantAdapter().getCyApplicationManager().getCurrentNetwork().containsEdge((vert.get(i)), (vert.get(k))) || (Main.getGiantAdapter().getCyApplicationManager().getCurrentNetwork().containsEdge((vert.get(k)), (vert.get(i)))))) {
			adj[i][k] = 1;
                        adj[k][i] = 1;
                    }
                    else {
                        adj[i][k] = 0;
                        adj[k][i] = 0;
                    }
		}
            }
            return adj;
        }
        return null;
    }
    
}
