/**
 * @author Twenty Codes, LLC
 * @author ricky barrette
 * @date Dec 18, 2010
 */

package com.TwentyCodes.java.OrderProcessor.UI;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import com.TwentyCodes.java.OrderProcessor.FileFilter;
import com.TwentyCodes.java.OrderProcessor.ProgressListener;
import com.TwentyCodes.java.OrderProcessor.UncaughtExceptionHandler;
import com.TwentyCodes.java.OrderProcessor.DB.OrderDB;

/**
 * this is the main window and class of this application
 */
public class MainWindow extends JFrame implements ActionListener, ProgressListener{

	private JButton mSearchButton;
	private JButton mShowAllButton;
	private JButton mLoadFileButton;
	private JFileChooser fc;
	public static OrderDB db;
	private static final long serialVersionUID = 1841715561053331517L;
	private JProgressBar mProgressBar;
	private String mCurrentFile;
	private UncaughtExceptionHandler mExceptionReport = new UncaughtExceptionHandler(this.getClass());

	/**
	 * Creates a new MainWindow
	 * @author ricky barrette
	 */
	public MainWindow() {
		
		Thread.setDefaultUncaughtExceptionHandler(mExceptionReport);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		
		setTitle("Twenty Codes, LLC Order Database");
		setIconImage(new ImageIcon(getClass().getResource("/database.png")).getImage());
		
		JPanel panel = new JPanel();
		
		getContentPane().add(panel);
		
		//Search buttom
		mSearchButton = new JButton("Search", new ImageIcon(getClass().getResource("/database_search.png")));
		mSearchButton.addActionListener(this);
		panel.add(mSearchButton);
		
		//show all button
		mShowAllButton = new JButton("Show All Orders", new ImageIcon(getClass().getResource("/database.png")));
		mShowAllButton.addActionListener(this);
		panel.add(mShowAllButton);
		
		//load button
		mLoadFileButton = new JButton("Load File", new ImageIcon(getClass().getResource("/database_add.png")));
		mLoadFileButton.addActionListener(this);
		panel.add(mLoadFileButton);
		
		//progress bar
		mProgressBar = new JProgressBar();
		mProgressBar.setStringPainted(true);
		getContentPane().add(mProgressBar, BorderLayout.SOUTH);
		
		setVisible(true);
		pack();
        
		try {
			db = new OrderDB();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

        
        fc = new JFileChooser();
        fc.setFileFilter(new FileFilter());
	}
	
	/**
	 * called when a button is clicked
	 * (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == mShowAllButton){
			new ShowAllDialog();
		}
		
		if (e.getSource() == mSearchButton) {
			new SearchDialog();
		}

		/**
		 * Prepares the progress bar, and loads the file using an executer to prevent UI hangups
		 */
		if(e.getSource() == mLoadFileButton){
			if(fc.showOpenDialog(MainWindow.this) == JFileChooser.APPROVE_OPTION){
				mLoadFileButton.setEnabled(false);
				db.setOnProgressListerner(this);
				mProgressBar.setIndeterminate(true);
				mCurrentFile = fc.getSelectedFile().toString();
				mProgressBar.setString(mCurrentFile);
				
				java.util.concurrent.Executors.newSingleThreadExecutor().submit(new Runnable() { 
					public void run() { 
						try {
							db.load(fc.getSelectedFile());
						} catch (FileNotFoundException e) {
							e.printStackTrace();
						} catch (SQLException e) {
							e.printStackTrace();
						} catch (IOException e) {
							e.printStackTrace();
						} catch (ClassNotFoundException e) {
							e.printStackTrace();
						} 
					}
				});

				pack();
			} else {
				mProgressBar.setIndeterminate(false);
			}
		}
	}
	
	

	
	/**
	 * called then the progress of the order db is updated
	 * (non-Javadoc)
	 * @see com.TwentyCodes.java.OrderProcessor.ProgressListener#onProgressUpdate(int)
	 * @author ricky barrette
	 */
	@Override
	public void onProgressUpdate(int progress, int max) {
		progress++;
		mProgressBar.setMaximum(max);
		mProgressBar.setValue(progress);
		mProgressBar.setString("     "+mCurrentFile + " ~ Parsing Order: "+ progress +" of "+mProgressBar.getMaximum()+"     ");
		if(mProgressBar.isIndeterminate()) {
			mProgressBar.setIndeterminate(false);
			pack();
		}
		if(progress >= mProgressBar.getMaximum())
			mLoadFileButton.setEnabled(true);
	}			

}