/*
 * @(#)BWHistory.java
 */
package ereinionbw;

import java.awt.Button;
import java.awt.Panel;
import java.awt.Frame;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Color;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Properties;
import java.util.Vector;
import java.util.Enumeration;

import ereinion.awt.AWTSetter;


/*
 * classe di package : BWHistory
 *
 * Finestra che mostra la storia delle ricerche effettuate.
 */
class BWHistory extends Frame implements WindowListener, ActionListener
{

	public static final String[] P_STATS = { "Used Time", "Max Space", "Max Depth", "Exp Nodes", "Sol Depth", "Sol Cost" };

	public static final String P_PROBLEM = "Problem";

	public static final String P_SEARCH = "Search Type";

	public static final String P_RESULT = "Search Result";

	public static final String P_EXP = "Expansion";

	public BWHistory(AWTSetter setter, int xDim, Vector history, Color indColor)
	{
		super("Blue Wizard History Frame");

		mainSetter = setter;

		mainSetter.setComponent(this);

		searchHistory = history;

		stackPointer = 0;

		// layout
		Panel controlPanel = (Panel)mainSetter.setComponent(new Panel(new GridLayout(1,2)));
		Panel controlPanel1 = (Panel)mainSetter.setComponent(new Panel(new GridLayout(1,2)));
		prevButton = (Button)mainSetter.setComponent(new Button("<<"),controlPanel1);
		prevButton.addActionListener(this);
		nextButton = (Button)mainSetter.setComponent(new Button(">>"),controlPanel1);
		nextButton.addActionListener(this);
		controlPanel.add(controlPanel1);
		clearButton = (Button)mainSetter.setComponent(new Button("Clear History"),controlPanel);
		clearButton.addActionListener(this);
		historyDataLabel = new Label[BUFFER][6];
		historyIndicatorLabel = new IndicatorLabel[BUFFER][6];
		Panel historyPanel = buildHistoryPanel(historyDataLabel,historyIndicatorLabel,indColor);
		Panel mainPanel = (Panel)mainSetter.setComponent(new Panel(new BorderLayout()));
		mainPanel.add(historyPanel,BorderLayout.CENTER);
		mainPanel.add(controlPanel,BorderLayout.NORTH);
		setLayout(new GridLayout(1,1));
		add(mainPanel);
		handleHistory();
		addWindowListener(this);
		setSize(xDim,xDim*5/8);
		setResizable(false);
		show();
	}


	private Panel buildHistoryPanel(Label[][] data, IndicatorLabel[][] ind, Color indColor)
	{
		Panel pan = (Panel)mainSetter.setComponent(new Panel(new GridLayout((BUFFER+3)*2,1)));

		mainSetter.setComponent(new Label(""),pan);
		mainSetter.setComponent(new Label("Search Stats",Label.CENTER),pan);

		Panel labelPanel1 = (Panel)mainSetter.setComponent(new Panel(new GridLayout(1,P_STATS.length+1)));
		for (int k=0; k<P_STATS.length; k++)
			mainSetter.setComponent(new Label(P_STATS[k],Label.CENTER),labelPanel1);
		mainSetter.setComponent(new Label("Search",Label.CENTER),labelPanel1);
		Panel labelPanel1Main = (Panel)mainSetter.setComponent(new Panel(new BorderLayout()));
		labelPanel1Main.add(labelPanel1,BorderLayout.CENTER);
		labelPanel1Main.add(mainSetter.setComponent(new Label(" # ",Label.CENTER)),BorderLayout.WEST);
		pan.add(labelPanel1Main);

		for (int i=0; i<BUFFER; i++) {
			Panel topPanel = (Panel)mainSetter.setComponent(new Panel(new GridLayout(1,7)));
			int seed = 10;
			for (int j=0; j<P_STATS.length; j++)
				ind[i][j] = (IndicatorLabel)mainSetter.setComponent(new IndicatorLabel(indColor,seed),topPanel);
			data[i][0] = (Label)mainSetter.setComponent(new Label("none",Label.CENTER),topPanel);
			data[i][4] = (Label)mainSetter.setComponent(new Label("0"+(i+1)+" ",Label.CENTER));
			Panel topPanelMain = (Panel)mainSetter.setComponent(new Panel(new BorderLayout()));
			topPanelMain.add(topPanel,BorderLayout.CENTER);
			topPanelMain.add(data[i][4],BorderLayout.WEST);
			pan.add(topPanelMain);
		}

		mainSetter.setComponent(new Label(""),pan);
		mainSetter.setComponent(new Label("Additional Info",Label.CENTER),pan);

		Panel labelPanel2 = (Panel)mainSetter.setComponent(new Panel(new GridLayout(1,2)));
		mainSetter.setComponent(new Label(P_PROBLEM,Label.CENTER),labelPanel2);
		mainSetter.setComponent(new Label(P_RESULT,Label.CENTER),labelPanel2);
		mainSetter.setComponent(new Label(P_EXP,Label.CENTER),labelPanel2);
		Panel labelPanel2Main = (Panel)mainSetter.setComponent(new Panel(new BorderLayout()));
		labelPanel2Main.add(labelPanel2,BorderLayout.CENTER);
		labelPanel2Main.add(mainSetter.setComponent(new Label(" # ",Label.CENTER)),BorderLayout.WEST);
		pan.add(labelPanel2Main);

		for (int i=0; i<BUFFER; i++) {
			Panel bottomPanel = (Panel)mainSetter.setComponent(new Panel(new GridLayout(1,3)));
			data[i][1] = (Label)mainSetter.setComponent(new Label("unset",Label.CENTER),bottomPanel);
			data[i][2] = (Label)mainSetter.setComponent(new Label("none",Label.CENTER),bottomPanel);
			data[i][3] = (Label)mainSetter.setComponent(new Label("none",Label.CENTER),bottomPanel);
			data[i][5] = (Label)mainSetter.setComponent(new Label("0"+(i+1)+" ",Label.CENTER));
			Panel bottomPanelMain = (Panel)mainSetter.setComponent(new Panel(new BorderLayout()));
			bottomPanelMain.add(bottomPanel,BorderLayout.CENTER);
			bottomPanelMain.add(data[i][5],BorderLayout.WEST);
			pan.add(bottomPanelMain);
		}

		return pan;
	}

	private void handleHistory()
	{
		for (int k=0; k<BUFFER && k<searchHistory.size(); k++) {
			Properties p = (Properties)searchHistory.elementAt(k+stackPointer);
			for (int i=0; i<P_STATS.length; i++)
				historyIndicatorLabel[k][i].setValue(Integer.parseInt(p.getProperty(P_STATS[i])));
			historyDataLabel[k][0].setText(p.getProperty(P_SEARCH));
			historyDataLabel[k][1].setText(p.getProperty(P_PROBLEM));
			historyDataLabel[k][2].setText(p.getProperty(P_RESULT));
			historyDataLabel[k][3].setText(p.getProperty(P_EXP));
			historyDataLabel[k][4].setText("0"+(k+stackPointer+1)+" ");
			historyDataLabel[k][5].setText("0"+(k+stackPointer+1)+" ");
		}
	}

	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() == nextButton) {
			if (stackPointer+BUFFER<searchHistory.size()) {
				stackPointer++;
				handleHistory();
			}
		} else if (e.getSource() == prevButton) {
			if (stackPointer>0) {
				stackPointer--;
				handleHistory();
			}
		} else if (e.getSource() == clearButton) {
			searchHistory.removeAllElements();
			dispose();
		}
	}

	public void windowActivated(WindowEvent e)  {}

	public void windowClosed(WindowEvent e) {}

	public void windowClosing(WindowEvent e) { dispose(); }

	public void windowDeactivated(WindowEvent e) {}

	public void windowDeiconified(WindowEvent e)  {}

	public void windowIconified(WindowEvent e) {}

	public void windowOpened(WindowEvent e) {}

	private AWTSetter mainSetter;

	private Vector searchHistory;

	private Label[][] historyDataLabel;

	private IndicatorLabel[][] historyIndicatorLabel;

	private final static int BUFFER = 5;

	private int stackPointer;

	private Button nextButton, prevButton, clearButton;

}
