/*
 * 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.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.JOptionPane;
import javax.swing.ProgressMonitorInputStream;
import org.cytoscape.model.CyRow;
import org.cytoscape.model.CyTable;
import org.cytoscape.model.CyTableFactory;
import org.cytoscape.model.CyTableManager;

/**
 *
 * @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 AttributesLoaderThread extends Thread {

    private File file;
    private static ArrayList<String> attrib_header = new ArrayList<String>();
    private static HashMap<String, ArrayList<String>> attributes = new HashMap<String, ArrayList<String>>();
    private static int attributesNum = 0;
    private CyTableFactory tableFactory;
    private CyTableManager tableManager;
    
    public AttributesLoaderThread(File file) {
        super(file.getName());
        this.file = file;
    }

    @Override
    public void run() {
        //LOAD ATTRIBUTES
        
        //FILE FORMAT:
        /*
         *  ID      Attribute1  Attribute2  AttributeN
         *  node1   value1.1    value1.2    value1.n
         *  node2   value2.1    value2.2    value2.n
         * 
         */
        
        attrib_header = new ArrayList<String>();
        attributes = new HashMap<String, ArrayList<String>>();
        attributesNum = 0;
        
        if (!NetworkLoaderThread.getNodesSetPath().equals("NULL")) {
            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;
                
                try {
                    strLine = br.readLine();
                    Helper helper = new Helper();
                    attrib_header = helper.tabPattern(strLine);
                    if (attrib_header.size()>0) {
                        if (attrib_header.get(0).toUpperCase().equals("ID") && attrib_header.get(1)!=null) {
                            CyTable table = tableFactory.createTable("GiantAttrTable", "name", String.class, true, true);
                            for (int k=1; k<attrib_header.size(); k++) {
                                String attributeName = attrib_header.get(k); 
                                table.createColumn(attributeName, String.class, false);
                            }
                            while ((strLine = br.readLine()) != null) {
                                ArrayList<String> data = helper.tabPattern(strLine);
                                if (data.size()>0 && data.size()==attrib_header.size()) {
                                    String node = data.get(0);
                                    ArrayList<String> tmp = new ArrayList<String>();
                                    CyRow row = table.getRow(node);
                                    for (int i=1; i<data.size(); i++) {
                                        tmp.add(data.get(i));
                                        row.set(attrib_header.get(i), new String(data.get(i)));
                                    }
                                    attributes.put(node, tmp);
                                    
                                }
                            }

                            attributesNum = attrib_header.size()-1;
                            Main.getGiantAdapter().getCyApplicationManager().getCurrentNetworkView().updateView();
                            tableManager.addTable(table);
                        }
                        else {
                            JOptionPane.showMessageDialog(Main.getApp().getFrameOverride(), "INTERNAL ATTRIBUTES FILE ERROR", "ERROR!", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                    else {
                        JOptionPane.showMessageDialog(Main.getApp().getFrameOverride(), "ERROR: EMPTY FILE...", "ERROR!", JOptionPane.ERROR_MESSAGE);
                    }
                } catch (IOException e) {
                } catch (NullPointerException ex) {}
                
            } catch (FileNotFoundException e) {}
        }
        else {
            JOptionPane.showMessageDialog(Main.getApp().getFrameOverride(), "INTERNAL ERROR", "ERROR!", JOptionPane.ERROR_MESSAGE);
        }
    }

    public static ArrayList<String> getAttrib_header() {
        return attrib_header;
    }

    public static HashMap<String, ArrayList<String>> getAttributes() {
        return attributes;
    }
    
    public static int getAttributesNumber() {
        return attributesNum;
    }
        
}
