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

import it.cnr.iasi.giant.thread.AttributesLoaderThread;
import it.cnr.iasi.giant.thread.ClusterLoaderThread;
import it.cnr.iasi.giant.thread.NetworkLoaderThread;
import it.cnr.iasi.giant.util.ColorNodeUtil;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @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 IOWriter {
        
    public IOWriter() { }

    public void writeRegionsResult(Object[][] percRegionsData, Object[][] regionsData, HashMap<Integer, ArrayList<String>> clusters, File csv) {
        try {
            File file = csv;
            if (file.exists())
                file.delete();
            file.createNewFile();
            FileOutputStream fos = new FileOutputStream(file.getAbsoluteFile()); 
            PrintStream out = new PrintStream(fos);
                
                out.println("Role-specific regions in the zP parameter space");
                out.println();
                out.println("Network File;" + NetworkLoaderThread.getNetworkTitle());
                out.println("Cluster File;" + ClusterLoaderThread.getFileName());
                out.println();
                out.println("1. Percentage of nodes in R1, R2, R3, R4, R5, R6 and R7:");
                out.println("R1;R2;R3;R4;R5;R6;R7");
                out.println(String.valueOf((Double)(percRegionsData[0][1])).replaceAll("\\.", ",") + ";" + String.valueOf((Double)(percRegionsData[0][2])).replaceAll("\\.", ",") + ";" + 
                            String.valueOf((Double)(percRegionsData[0][3])).replaceAll("\\.", ",") + ";" + String.valueOf((Double)(percRegionsData[0][4])).replaceAll("\\.", ",") + ";" + 
                            String.valueOf((Double)(percRegionsData[0][5])).replaceAll("\\.", ",") + ";" + String.valueOf((Double)(percRegionsData[0][6])).replaceAll("\\.", ",") + ";" + 
                            String.valueOf((Double)(percRegionsData[0][7])).replaceAll("\\.", ",") + ";");
                out.println();
                
                out.println("2. Region2Color:");
                out.println("R1;R2;R3;R4;R5;R6;R7");
                ColorNodeUtil util = new ColorNodeUtil();
                out.println(util.getRegionColor("R1") + ";" + util.getRegionColor("R2") + ";" + util.getRegionColor("R3") + ";" + 
                            util.getRegionColor("R4") + ";" + util.getRegionColor("R5") + ";" + util.getRegionColor("R6") + ";" + 
                            util.getRegionColor("R7") + ";");
                out.println();
                out.println();
                
                out.println("3. CLUSTERS");
                for (Integer clus: clusters.keySet()) {
                    out.println("CLUSTER: " + (clus+1) + ";" + clusters.get(clus).size() + " NODES");
                }
                out.println();
                out.println();
                
                out.println("4. DATA");
                out.println();
                
                String head = "Node;Region;Degree;In Cluster Degree;Participation Coefficient;Z Score;Cluster;Hub;Type;;";
                for (int i=1; i<AttributesLoaderThread.getAttrib_header().size(); i++)
                    head = head + AttributesLoaderThread.getAttrib_header().get(i).replaceAll(";", "#") + ";";
                out.println(head);
                
                for (Object[] arr: regionsData) {
                    try {
                                                
                        String id = ((String)arr[0]).replaceAll(" ", "_");
                        id = id.replaceAll(";", "-");
                        String region = (String)arr[3];
                        Double degree = (Double)arr[1];
                        Double inClusterDegree = (Double)arr[2];
                        String p_ = String.valueOf((Double)arr[arr.length-2]).replaceAll("\\.", ",");
                        String z_ = String.valueOf((Double)arr[arr.length-1]).replaceAll("\\.", ",");
                        int cluster = ClusterLoaderThread.getClusterForNode((String)arr[0]);
                        String hub = (String)arr[4];
                        String type = (String)arr[5];
                        
                        String row = id + ";" + region + ";" + degree + ";" + inClusterDegree + 
                                    ";" + p_ + ";" + z_ + ";" +  cluster + 
                                    ";" + hub + ";" + type + ";;"; 
                        if (AttributesLoaderThread.getAttributes().size()>0)
                            if (AttributesLoaderThread.getAttributes().get((String)arr[0])!=null)
                                for (String att: AttributesLoaderThread.getAttributes().get((String)arr[0]))
                                    row = row + att.replaceAll(";", "#") + ";";
                        out.println(row);
                    } catch (NullPointerException e) {
                    }
                }
                
                out.close();
                fos.close();
                        
        } catch (IOException ex) {
            Logger.getLogger(IOWriter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public String writeTmpClusterFile(HashMap<Integer, ArrayList<String>> clusters) {
        try {
            File tmp = File.createTempFile(String.valueOf(Math.random()), ".cluster");
            System.out.println("TMP CLUSTER FILE: " + tmp.getAbsolutePath());
            FileOutputStream fos = new FileOutputStream(tmp.getAbsoluteFile()); 
            PrintStream out = new PrintStream(fos);
            
            for (ArrayList<String> arr: clusters.values()) {
                //String list = arr.toString();
                
                for (String str: arr) {
                    out.print((str.trim()).replaceAll(" ", "") + "\t");
                }
                out.println();
                
                //list = list.replaceAll(",", "");
                //list = list.replaceAll("\\[", "");
                //list = list.replaceAll("\\]", "");
                //list = list.replaceAll(" ", "\t");
                //out.println(list);
            }
            
            return tmp.getAbsolutePath();
            
        } catch (IOException e) {
            return "NULL";
        }
                
    }

    public String writeTmpDistanceMatrixFile(int[][] distanceMatrix) {
        try {
            File tmp = File.createTempFile(String.valueOf(Math.random()), ".distance");
            System.out.println("TMP DISTANCE MATRIX FILE: " + tmp.getAbsolutePath());
            FileOutputStream fos = new FileOutputStream(tmp.getAbsoluteFile()); 
            PrintStream out = new PrintStream(fos);
            
            for (int i=0; i<distanceMatrix.length; i++) {
                String line = Arrays.toString(distanceMatrix[i]);
                line = line.replaceAll(",", "");
                line = line.replaceAll("\\[", "");
                line = line.replaceAll("\\]", "");
                out.println(line);
            }
            
            return tmp.getAbsolutePath();
            
        } catch (IOException e) {
            return "NULL";
        }
    }
    
    public String writeTmpAdjMatrixFile(double[][] adjMatrix) {
        try {
            File tmp = File.createTempFile(String.valueOf(Math.random()), ".adjacency");
            System.out.println("TMP ADJACENCY MATRIX FILE: " + tmp.getAbsolutePath());
            FileOutputStream fos = new FileOutputStream(tmp.getAbsoluteFile()); 
            PrintStream out = new PrintStream(fos);
            
            for (int i=0; i<adjMatrix.length; i++) {
                String line = Arrays.toString(adjMatrix[i]);
                line = line.replaceAll(",", "");
                line = line.replaceAll("\\[", "");
                line = line.replaceAll("\\]", "");
                out.println(line);
            }
            
            return tmp.getAbsolutePath();
            
        } catch (IOException e) {
            return "NULL";
        }
    }
    
    public String writeTmpNodesSetFile(Object[] nodes) {
        try {
            File tmp = File.createTempFile(String.valueOf(Math.random()), ".nodes");
            System.out.println("TMP NODES SET FILE: " + tmp.getAbsolutePath());
            FileOutputStream fos = new FileOutputStream(tmp.getAbsoluteFile()); 
            PrintStream out = new PrintStream(fos);
            
            for (int i=0; i<nodes.length; i++)
                out.println((String)nodes[i]);
            
            return tmp.getAbsolutePath();
            
        } catch (IOException e) {
            return "NULL";
        }
    }
    
}
