/**
 * @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.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import com.TwentyCodes.java.OrderProcessor.ProgressListener;

/**
 * This panel will be used to display all Vehicles in the VechicleDB
 */
public class ShowAllDialog extends JFrame implements ActionListener, ProgressListener {

	private static final long serialVersionUID = -8416144493079733535L;
	private JProgressBar mProgressBar;
	private OrderPane mOutput;
	
	/**
	 * Creates a new ShowAllDialog
	 */
	public ShowAllDialog(){
		super();
		setTitle("Show All Orders");
		setIconImage(new ImageIcon(getClass().getResource("/database.png")).getImage());
		
		JButton ok = new JButton("Ok", new ImageIcon(getClass().getResource("/accept.png")));
		ok.addActionListener(this);
		mOutput = new OrderPane();
		mProgressBar = new JProgressBar();
		getContentPane().add(mProgressBar, BorderLayout.SOUTH);
		this.getContentPane().add(mOutput, BorderLayout.CENTER);
		this.getContentPane().add(ok, BorderLayout.NORTH);
		mProgressBar.setString("Loading from Database");
		mProgressBar.setStringPainted(true);
		MainWindow.db.setOnProgressListerner(this);
		pack();
		
		java.util.concurrent.Executors.newSingleThreadExecutor().submit(new Runnable() { 
			public void run() { 
				try {
					mOutput.displayOrders(MainWindow.db.getAllOrders());
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				pack();
			}
		});
		
		setVisible(true);
	}

	/**
	 * Creates a new ShowAllDialog
	 */
	public ShowAllDialog(String title, String body){
		super();
		setTitle(title);
		setIconImage(new ImageIcon(getClass().getResource("/info.png")).getImage());
		
		JButton ok = new JButton("Ok", new ImageIcon(getClass().getResource("/accept.png")));
		ok.addActionListener(this);
		
		JScrollPane scrollPane = new JScrollPane();
		
		final JTextArea results = new JTextArea();
		scrollPane.setViewportView(results);
		
		results.setEditable(false);
		results.setText(body);
		
		this.getContentPane().add(scrollPane, BorderLayout.CENTER);
		this.getContentPane().add(ok, BorderLayout.SOUTH);
		pack();
		setVisible(true);
	}
	

	@Override
	public void actionPerformed(ActionEvent arg0) {
		this.dispose();	
	}

	@Override
	public void onProgressUpdate(int progress, int max) {
		mProgressBar.setMaximum(max);
		mProgressBar.setValue(progress);
		if(progress >= mProgressBar.getMaximum()){
			this.pack();
			mProgressBar.setString("Done");
		}
	}
}
