/*
 * 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 java.io.*;
import java.util.ArrayList;
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 GiantHelpAction extends Action {
    
    private static String tmpPath = "";

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

    @Override
    public ArrayList<Object> execute() {
        try {
            String currentDir = System.getProperty("user.dir");
            File chm = new File(currentDir + "\\plugins\\giant-data\\giant.chm");
            if (tmpPath==null || tmpPath.equals("")) {
                File tmp = File.createTempFile("giant", ".chm");
                copy(chm.getAbsolutePath(), tmp.getAbsolutePath());
                tmpPath = tmp.getAbsolutePath();
            }
            if (chm.exists())
                Runtime.getRuntime().exec("hh.exe " + tmpPath);
            else
                JOptionPane.showMessageDialog(Main.getApp().getFrameOverride(), "See the official website for help\non using GIANT v1.0\nhttp://www.iasi.cnr.it/~dsantoni/GIANT/giant.html", "INFO!", JOptionPane.INFORMATION_MESSAGE);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(Main.getApp().getFrameOverride(), "See the official website for help\non using GIANT v1.0\nhttp://www.iasi.cnr.it/~dsantoni/GIANT/giant.html", "INFO!", JOptionPane.INFORMATION_MESSAGE);
        }
        return null;
    }
    
    public static String getTmpPath() {
        return tmpPath;
    }
    
    private void copy(String source, String target) {
        InputStream inStream;
	OutputStream outStream;
 
    	try{
 
    	    File afile =new File(source);
    	    File bfile =new File(target);
 
    	    inStream = new FileInputStream(afile);
    	    outStream = new FileOutputStream(bfile);
 
    	    byte[] buffer = new byte[1024];
 
    	    int length;
    	    //copy the file content in bytes 
    	    while ((length = inStream.read(buffer)) > 0){
 
    	    	outStream.write(buffer, 0, length);
 
    	    }
 
    	    inStream.close();
    	    outStream.close();
 
    	    //System.out.println("File is copied successful!");
 
    	}catch(IOException e){
    		e.printStackTrace();
    	}
    }
    
}
