/*
 * 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.Main;
import it.cnr.iasi.giant.frame.GIANT_NT;
import it.cnr.iasi.giant.io.IOWriter;
import it.cnr.iasi.giant.thread.ClusterLoaderThread;
import it.cnr.iasi.giant.thread.ComputePZThread;
import it.cnr.iasi.giant.util.CSVFilter;
import java.io.File;
import java.util.ArrayList;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;

/**
 *
 * @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 SaveRegionsResultsAction extends Action {

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

    @Override
    public ArrayList<Object> execute() {
        JFileChooser chooser = new JFileChooser();
        
	FileFilter csvFilter = new CSVFilter();
        chooser.addChoosableFileFilter(csvFilter);
	chooser.setFileFilter(csvFilter);
        
        int code = chooser.showSaveDialog(null);
        File file;
        if (code == JFileChooser.APPROVE_OPTION) {
            file = chooser.getSelectedFile();
            
                IOWriter io = new IOWriter();
                if (file.exists()) {
                    int result = JOptionPane.showConfirmDialog(Main.getApp().getFrameOverride(), "File already exists, overwrite it?", "WORNING!", JOptionPane.YES_NO_OPTION);
                        switch(result){
                            case JOptionPane.YES_OPTION:
                                file.delete();
                                io.writeRegionsResult(ComputePZThread.getPercRegionsData(), ComputePZThread.getNode2RegionData(), ClusterLoaderThread.getClustersData(), file);
                            case JOptionPane.NO_OPTION:
                                chooser.cancelSelection();
                                return null;
                        }
                }
                else {
                    io.writeRegionsResult(ComputePZThread.getPercRegionsData(), ComputePZThread.getNode2RegionData(), ClusterLoaderThread.getClustersData(), file);
                }
        }
        return null;
    }
    
}
