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

import java.awt.Checkbox;
import java.awt.CheckboxGroup;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Button;
import java.awt.Panel;
import java.awt.Color;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.WindowListener;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import ereinion.awt.AWTSetter;
import ereinion.awt.AWTBaseSetter;
import ereinion.io.PrinterLoader;

/**
 * Classe che si limita a caricare l'applicazione grafica.
 *
 * @author  Ereinion
 * @version 1.0, 7/10/2002
 * @since EPR 1.0
 */
public class BW
{

	/**
	 * campo associato al colore di sfondo dell' applicazione.
	 */
	public final static Color BACKCOLOR = new Color(0,0,40);

	/**
	 * campo associato al colore delle scritte dell' applicazione.
	 */
	public final static Color FORECOLOR = new Color(230,230,255);

	/**
	 * campo associato con una risoluzione orizzantale di 640 pixel.
	 */
	public final static int X640RES = 0;

	/**
	 * campo associato con una risoluzione orizzantale di 900 pixel.
	 */
	public final static int X800RES = 1;

	/**
	 * campo associato con una risoluzione orizzantale di 1024 pixel.
	 */
	public final static int X1024RES = 2;

	/**
	 * campo associato con una risoluzione orizzantale di 1280 pixel.
	 */
	public final static int X1280RES = 3;

	/**
	 * campo che apre una finestra di configurazione per la scenta della risoluzione.
	 */
	public final static int RES_FRAME = -1;

	/**
	 * campo che associa direttamente la risolzione ritenuta migliore per le dimensioni dello schermo.
	 */
	public final static int RES_DETECT = -2;

	/**
	 * Funzione che effettua il caricamento.
	 *
	 * @param dim il valore relativa alle dimensioni desiderate della finestra.
	 * @see ereinionbw.BW#X640RES
	 * @see ereinionbw.BW#X800RES
	 * @see ereinionbw.BW#X1024RES
	 * @see ereinionbw.BW#X1280RES
	 * @see ereinionbw.BW#RES_FRAME
	 * @see ereinionbw.BW#RES_DETECT
	 */
	public static void loadWizard(int dim)
	{
		if (dim == RES_FRAME)
			new BWLoadFrame();
		else {
			BWData dataLoader = BWData.loadData(new PromptLoader());
			int xDimension = 600;
			int normalFontSize = 10;
			if (dim == RES_DETECT) {
				System.out.println("Probing screen resolution...");
				int screenX = Toolkit.getDefaultToolkit().getScreenSize().width;
				int screenY = Toolkit.getDefaultToolkit().getScreenSize().height;
				System.out.println("X size :"+screenX);
				System.out.println("Y size :"+screenY);
				if (screenX>1200)
					dim = X1280RES;
				else if (screenX>1000)
					dim = X1024RES;
				else if (screenX>=800)
					dim = X800RES;
				else
					dim = X640RES;
			}
			if (dim == X800RES) {
				xDimension = 800;
				normalFontSize = 11;
			} else if (dim == X1024RES) {
				xDimension = 1000;
				normalFontSize = 12;
			} else if (dim == X1280RES) {
				xDimension = 1200;
				normalFontSize = 14;
			} else  {
				xDimension = 600;
				normalFontSize = 10;
			}
			AWTSetter one = new AWTBaseSetter(BACKCOLOR,FORECOLOR,new Font("Courier",Font.PLAIN,normalFontSize));
			AWTSetter two = new AWTBaseSetter(BACKCOLOR,FORECOLOR,new Font("Courier",Font.BOLD,normalFontSize));
			Frame f = new BWFrame(dataLoader,one,two,xDimension);
		}
	}

}


/*
 * classe di package : PromptLoader
 *
 * PrinterLoader per messaggi da prompt
 */
class PromptLoader implements PrinterLoader
{

	public PromptLoader() {}

	public void printMessage(String message)
	{
		while (message.length()<50)
			message+=" ";
		System.out.print(message);
	}

	public void printFail() { System.out.println("[FAIL]"); }

	public void printDone() { System.out.println("[DONE]"); }

}


/*
 * classe di package : BWLoadFrame
 *
 * Finestra di caricamento del programma.
 */
class BWLoadFrame extends Frame implements ActionListener, WindowListener
{

	private final static Color backColor = new Color(0,0,40);
	private final static Color foreColor = new Color(230,230,255);

	private Checkbox[] resBox;

	private Button openButton;

	TextPrinterLoader logText;

	public BWLoadFrame()
	{
		AWTSetter mainSetter = new AWTBaseSetter(backColor,foreColor,new Font("Courier",Font.PLAIN,14));

		mainSetter.setComponent(this);

		mainSetter.setComponent(this);

		setLayout(new GridLayout(1,1));

		Panel mainPanel = new Panel(new BorderLayout());

		Panel resPanel = new Panel(new GridLayout(4,1));
		resBox = new Checkbox[4];
		CheckboxGroup resGroup = new CheckboxGroup();
		resBox[0] = (Checkbox)mainSetter.setComponent(new Checkbox("640x480",true,resGroup),resPanel);
		resBox[1] = (Checkbox)mainSetter.setComponent(new Checkbox("800x600",false,resGroup),resPanel);
		resBox[2] = (Checkbox)mainSetter.setComponent(new Checkbox("1024x768",false,resGroup),resPanel);
		resBox[3] = (Checkbox)mainSetter.setComponent(new Checkbox("1280x1024",false,resGroup),resPanel);

		openButton = (Button)mainSetter.setComponent(new Button("Open Window"));
		openButton.addActionListener(this);

		logText = (TextPrinterLoader)mainSetter.setComponent(new TextPrinterLoader());

		logText.printMessage("Probing screen resolution");
		int screenX = Toolkit.getDefaultToolkit().getScreenSize().width;
		int screenY = Toolkit.getDefaultToolkit().getScreenSize().height;
		logText.printDone();
		logText.printLine("X size: "+screenX);
		logText.printLine("Y size: "+screenY);
		logText.printMessage("Setting to best resolution");
		if (screenX>1200)
			resBox[3].setState(true);
		else if (screenX>1000)
			resBox[2].setState(true);
		else if (screenX>=800)
			resBox[1].setState(true);
		else
			resBox[0].setState(true);
		logText.printDone();
		logText.printLine("");
		logText.printLine("Adjust resolution if you want,");
		logText.printLine("then hit the Open Window button.");
		logText.printLine("");
		logText.printLine("waiting...");
		logText.printLine("");

		mainPanel.add(logText,BorderLayout.CENTER);
		mainPanel.add(openButton,BorderLayout.SOUTH);
		mainPanel.add(resPanel,BorderLayout.EAST);

		add(mainPanel);

		addWindowListener(this);

		setSize(600,400);
		setResizable(false);
		show();
	}

	public void actionPerformed(ActionEvent e)
	{
		BWData dataLoader = BWData.loadData(logText);
		// dimension setting
		int xDimension = 600;
		int normalFontSize = 10;
		if (resBox[1].getState()) {
			xDimension = 800;
			normalFontSize = 11;
		} else if (resBox[2].getState()) {
			xDimension = 1000;
			normalFontSize = 12;
		} else if (resBox[3].getState()) {
			xDimension = 1200;
			normalFontSize = 14;
		} else  {
			xDimension = 600;
			normalFontSize = 10;
		}
		AWTSetter one = new AWTBaseSetter(BW.BACKCOLOR,BW.FORECOLOR,new Font("Courier",Font.PLAIN,normalFontSize));
		AWTSetter two = new AWTBaseSetter(BW.BACKCOLOR,BW.FORECOLOR,new Font("Courier",Font.BOLD,normalFontSize));
		Frame f = new BWFrame(dataLoader,one,two,xDimension);
		f.show();
		dispose();
	}

	public void windowActivated(WindowEvent e) {}

	public void windowClosed(WindowEvent e) {}

	public void windowClosing(WindowEvent e)
	{
		if (e.getSource() == this)
			dispose();
	}

	public void windowDeactivated(WindowEvent e) {}

	public void windowDeiconified(WindowEvent e)  {}

	public void windowIconified(WindowEvent e) {}

	public void windowOpened(WindowEvent e) {}

}
