/**
 * @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.IOException;
import java.sql.SQLException;

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

import com.TwentyCodes.java.OrderProcessor.ProgressListener;

/**
 * This panel will be used to display all orders in the order database
 */
public class ShowAllPanel extends JPanel implements ActionListener, ProgressListener {

	private static final long serialVersionUID = -8416144493079733535L;
	private JProgressBar mProgressBar;
	private OrderPane mOutput;
	private JFrame mFrame;
	private JButton ok;
	private JButton clear;
	
	/**
	 * Creates a new ShowAllDialog
	 */
	public ShowAllPanel(final JFrame frame){
		super(new BorderLayout());
		mFrame = frame;
		
		JPanel panel = new JPanel();
		
//		setTitle("Show All Orders");
//		setIconImage(new ImageIcon(getClass().getResource("/database.png")).getImage());
		
		//ok button
		ok = new JButton("Refresh", new ImageIcon(getClass().getResource("/page_process.png")));
		ok.addActionListener(this);
		panel.add(ok);
		
		//clear button
		clear = new JButton("Clear", new ImageIcon(getClass().getResource("/page.png")));
		clear.addActionListener(this);
		panel.add(clear);
		
		mOutput = new OrderPane();
		mProgressBar = new JProgressBar();
		add(mProgressBar, BorderLayout.SOUTH);
		add(mOutput, BorderLayout.CENTER);
		add(panel, BorderLayout.NORTH);
		mProgressBar.setStringPainted(true);
		frame.pack();
		setVisible(true);
	}	

	/**
	 * called when the buttons are clicked
	 * (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 * @author ricky barrette
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		
		//display all the orders
		if(e.getSource() == ok){
			MainWindow.db.setOnProgressListerner(this);
			java.util.concurrent.Executors.newSingleThreadExecutor().submit(new Runnable() { 
				public void run() { 
					try {
						mOutput.displayOrders(MainWindow.db.getAllOrders());
					} catch (SQLException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					} catch (ClassNotFoundException e) {
						e.printStackTrace();
					}
					mFrame.pack();
				}
			});
		}
		
		//clear the output
		if(e.getSource() == clear){
			mOutput.setText(null);
			onProgressUpdate(null, false, 0, 1);
			mFrame.pack();
		}
	}

	/**
	 * called when there is a progress update
	 * (non-Javadoc)
	 * @see com.TwentyCodes.java.OrderProcessor.ProgressListener#onProgressUpdate(java.lang.String, boolean, int, int)
	 * @author ricky barrette
	 */
	@Override
	public void onProgressUpdate(String string, boolean isIndeterminate, int progress, int max) {
		mProgressBar.setMaximum(max);
		mProgressBar.setValue(progress);
		mProgressBar.setIndeterminate(isIndeterminate);
		mProgressBar.setString(string);
		if(progress >= mProgressBar.getMaximum()){
			mFrame.pack();
			mProgressBar.setString("Done");
		}
	}
}
