import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * Contain macro to access to the PDF
 * @author JPR
 *
 */
public class MacroMethods 
{

	public static void openPdf(File file)
    {
        
        try
        {            
            String[] cmd = new String[3];
            cmd[0] = "cmd.exe" ;
            cmd[1] = "/C" ;
            cmd[2] = file.getAbsolutePath();
            
            Runtime rt = Runtime.getRuntime();
            //System.out.println("Execing " + cmd[0] + " " + cmd[1]  + " " + cmd[2]);
            Process proc = rt.exec(cmd);
            // any error message?
            Thread errorGobbler = new Thread()
            {
        	    InputStream is= proc.getErrorStream();
        	    String type= "ERROR";
        	    
        	    public void run()
        	    {
        	        try
        	        {
        	            InputStreamReader isr = new InputStreamReader(is);
        	            BufferedReader br = new BufferedReader(isr);
        	            String line=null;
        	            while ( (line = br.readLine()) != null)
        	            {
        	                //System.out.println(type + ">" + line);   
        	            	
        	            } 
        	            } catch (IOException ioe)
        	              {
        	                ioe.printStackTrace();  
        	              }
        	    }
            	
            };            
            
            // any output?
            Thread outputGobbler  = new Thread()
            {
        	    InputStream is= proc.getInputStream();
        	    String type= "OUTPUT";
        	    
        	    public void run()
        	    {
        	        try
        	        {
        	            InputStreamReader isr = new InputStreamReader(is);
        	            BufferedReader br = new BufferedReader(isr);
        	            String line=null;
        	            while ( (line = br.readLine()) != null)
        	            {
        	                //System.out.println(type + ">" + line);    
        	            	
        	            }
        	            } catch (IOException ioe)
        	              {
        	                ioe.printStackTrace();  
        	              }
        	    }
            	
            }; 
                
            // kick them off
            errorGobbler.start();
            outputGobbler.start();
                                    
            // any error???
            int exitVal = proc.waitFor();
            //System.out.println("ExitValue: " + exitVal);        
        }
        catch (Throwable t)
        {
        	t.printStackTrace();
        }
    }
	
	/**
	 * open the PDF corresponding to the File in parameter
	 * @param job
	 *//*
	public static Process openPdf(File file)
	{
		Process p = null;
		
		try
		{
			String fileName = file.getAbsolutePath();
			if ((new File(fileName)).exists()) {
				p=Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + fileName);
			} else {
				System.out.println("File is not exists");
			}
			waitLowProcessor();
			waitLowProcessor();		
			waitLowProcessor();		
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
		return p;
	}*/

	/**
	 * wait while the page is not loaded
	 *//*
	public static void waitLowProcessor() throws ErrorException
	{
		float cpu[] = new float[10];
		do {
			int cpttry = 0;
			try 
			{
				CPUController cc = new CPUController();
				for(int i = 0; i < 10 ; i++)
				{
					Thread.sleep(500L);
					cpu[i]=cc.cpuController(1);
				}
				cpttry++;
			}
			catch(InterruptedException e)
			{
				throw new ErrorException("05 : "+e.getMessage(), 1);
			}
			if(cpttry == 10)throw new ErrorException("04 : page chargement is too long", 1);
			//TODO si on attend plus de 10 minutes, alors killer le process accrobat et passer à la page suivante (destroyProcess)
			//cf process qui a ouvert le stamp et destroyProcess
		} while ((cpu[0] > 0.12f) || (cpu[1] > 0.12f) || (cpu[2] > 0.12f) || (cpu[3] > 0.12f) || (cpu[4] > 0.12f) || (cpu[5] > 0.12f) || (cpu[6] > 0.12f) || (cpu[7] > 0.12f) || (cpu[8] > 0.12f) || (cpu[9] > 0.12f) );
	}*/


}