/*
 * 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.Controller;
import it.cnr.iasi.giant.Main;
import it.cnr.iasi.giant.algorithm.clustering.ClusteringAlgorithm;
import it.cnr.iasi.giant.frame.GIANT_NT;
import it.cnr.iasi.giant.thread.NetworkLoaderThread;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
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 RunClusteringAction extends Action {

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

    @Override
    public ArrayList<Object> execute() {
        initAlg();
        if (!NetworkLoaderThread.getDegreeMap().isEmpty()) {
            try {
                String selected = Main.getApp().getClusteringList().getSelectedItem().toString().toUpperCase();
                ClusteringAlgorithm alg = (ClusteringAlgorithm)Class.forName(supportedAlgorithms.get(selected)).newInstance();
                alg.execute();
                /*if (!alg.execute()) {
                    if (!selected.equals("KMEANS") && !selected.equals("SPECTRAL CLUSTERING"))
                        JOptionPane.showMessageDialog(GIANT.getWindows()[0], "INTERNAL ERROR", "ERROR!", JOptionPane.ERROR_MESSAGE);
                }
                else {
                    JOptionPane.showMessageDialog(GIANT.getWindows()[0], "Completed...", "INFO!", JOptionPane.INFORMATION_MESSAGE);
                    GIANT.getComputePZ_button().setEnabled(true);
                }*/
                return null;
            } catch (InstantiationException e1) {
                Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, e1);
            } catch (IllegalAccessException e2) {
                Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, e2);
            } catch (ClassNotFoundException e3) {
                Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, e3);
            }
        }
        else {
            JOptionPane.showMessageDialog(Main.getApp().getFrameOverride(), "Load network first...", "ERROR!", JOptionPane.ERROR_MESSAGE);
        }
        return null;
    }
    
    private void initAlg() {
        supportedAlgorithms =  new HashMap<String, String>();
        supportedAlgorithms.put("MCL", "it.cnr.iasi.giant.algorithm.clustering.MarkovClusterAlgorithm");
        supportedAlgorithms.put("KMEANS", "it.cnr.iasi.giant.algorithm.clustering.KMeansAlgorithm");
        supportedAlgorithms.put("SPECTRAL CLUSTERING", "it.cnr.iasi.giant.algorithm.clustering.SpectralClustering");
    }
    
    public static HashMap<Integer, Integer> getNewMap(int upperBound, int lowerBound) {
        System.err.println("[" + lowerBound + " , " + upperBound + "]");
        
        HashMap<Integer, Integer> result = new HashMap<Integer, Integer>();
        int count = 0;
        for (int i=lowerBound; i<=upperBound; i++) {
            result.put(i, count);
            count++;
        }
        
        return result;
    }
    
}
