import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.WindowConstants;

import org.apache.log4j.Logger;
/**
 * window to manage jobs and launching stamping
 * @author 413
 *
 */
public class Run extends JFrame implements ActionListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	static Logger logger = Logger.getLogger(Run.class.getName());
	static Boolean stopped = false;
	/**
	 * Starting
	 * @param args
	 */
	public  Run()
	{
		logger.info("program start");
		JButton stop = new JButton("stop");
		GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);
		layout.setHorizontalGroup(layout
				.createSequentialGroup()
				.addComponent(stop));
		layout.setVerticalGroup(layout
				.createSequentialGroup()
				.addComponent(stop));
		stop.addActionListener(this);
		
		setTitle("Result");
		pack();
		setSize(100,100);
		setLocationRelativeTo(null);
		setVisible(true);
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		while(!stopped)
		{
			if(ServiceController.run())
			{
				logger.info("stamping start");
				//====================================GET XML FILES====================================================
				
				/*
				 * a filter to get only XML files
				 */
				FilenameFilter filterXML = new FilenameFilter()
				{
					public boolean accept(File arg0, String arg1)
					{
						return arg1.endsWith(".xml");
					}
				};
				//Loading XML files
				File folder = ServiceConfig.job;
				File[] children = folder.listFiles(filterXML);
				ArrayList<Job> l = new ArrayList<Job>(children.length);
				for(int i = 0; i < children.length; i++)
				{
					l.add(Job.load(children[i]));
				}
				if(l.isEmpty())
				{
					logger.fatal("No job to execute");
				}
				else
				{
					logger.info("loading job files");
					//======================================start stamping ================================================================
					StampleProcess sp = new StampleProcess();
					//delete folder Image Files
					folder = ServiceConfig.image;
					children = folder.listFiles();
					logger.info("deleting images files");
					for(int i = 0; i < children.length; i++)
					{
						children[i].delete();
					}/*
					folder = ServiceConfig.logs;
					children = folder.listFiles();
					logger.info("deleting logs files");
					for(int i = 0; i < children.length; i++)
					{
						children[i].delete();
					}*/
					for(int i = 0; i < l.size();i++)
					{
						//execute the stamping
						sp.processJob(l.get(i));
					}
					if(sp.isStop())
					{
						ServiceController.stop();
						logger.info("stamping has been stop");
					}
					else
					{
						ServiceController.finish();
						logger.info("stamping is finished");
					}
				}
			}
			else
			{
				try
				{
					Thread.sleep(1000);
				}
				catch (InterruptedException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		logger.info("programme stop");
		this.dispose();
		
	}
	public static void main(String args[])
	{
        
		ServiceConfig.load();
		ServiceController.init();
		new Run();
	}

	public void actionPerformed(ActionEvent arg0)
	{
		stopped=true;
	}
}
