package sim;

import sim.credit.*;

import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.io.File;
import java.util.concurrent.CountDownLatch;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;


import sim.util.BubbleListener;
import sim.util.BubbleWaitThread;
import sim.util.SimBubble;

public class MainWindow extends Frame {

	public static final String PROGRAM_NAME = "ReTrO";
	public static final String VERSION = "V4.2";
	public static String WORKSPACE;
	public static final String DEFAULT_FILENAME = "Untitle.toy";
	public static Image ICON;
	public static MainWindow MAIN_WINDOW;
	public static WestPanel WEST_PANEL;
	public static CentralPanel CENTRAL_PANEL;
	public static SouthPanel SOUTH_PANEL;
	public static NorthPanel NORTH_PANEL;
	public static SimMenuBar MENU;
	public static Applet MASTER;
	public static OkWindow OK_WINDOW;
	public static Color BACKGROUND;
	private static final Dimension INITIAL_SIZE = new Dimension(1000, 700);
	private static final int LOADING_TIME = 1000; //// wait for x msec for splash screen
	public static CountDownLatch WAIT_BLOCK = new CountDownLatch(1);

	public static Point getMiddleOfScreen(Component c) {
		Dimension screen = MainWindow.MAIN_WINDOW.getToolkit().getScreenSize();
		Dimension size = c.getSize();

		return (new Point((screen.width - size.width) / 2, (screen.height - size.height) / 2));
	}

	public MainWindow(Applet master) {
		super();
		MainWindow.MASTER = master;
		MainWindow.MAIN_WINDOW = this;

		// show splash screen
		SplashWindow splash = new SplashWindow();
		long startSplash = System.currentTimeMillis();
		while (System.currentTimeMillis() - startSplash < LOADING_TIME) {
			splash.progress((int) (100 * (System.currentTimeMillis() - startSplash) / LOADING_TIME));
		}
		splash.dispose();

		MainWindow.ICON = GuiFileLink.getImage("sim/images/Icon.gif");
		this.setIconImage(MainWindow.ICON);

		BubbleListener.WAIT_THREAD = new BubbleWaitThread();
		BubbleListener.WAIT_THREAD.start();
		BubbleWaitThread.BUBBLE_WINDOW = new SimBubble(this);

		MainWindow.OK_WINDOW = new OkWindow(this);
		MainWindow.OK_WINDOW.setTitle(MainWindow.PROGRAM_NAME);

		// initialise frames components
		this.setLayout(new BorderLayout(2, 2));

		MainWindow.BACKGROUND = SystemColor.control;
		this.setBackground(MainWindow.BACKGROUND);

		MainWindow.SOUTH_PANEL = new SouthPanel();
		MainWindow.WEST_PANEL = new WestPanel();
		MainWindow.CENTRAL_PANEL = new CentralPanel();
		MainWindow.NORTH_PANEL = new NorthPanel();

		this.add(MainWindow.SOUTH_PANEL, BorderLayout.SOUTH);
		this.add(MainWindow.WEST_PANEL, BorderLayout.WEST);
		this.add(MainWindow.CENTRAL_PANEL, BorderLayout.CENTER);
		this.add(MainWindow.NORTH_PANEL, BorderLayout.NORTH);

		// initialise frame
		//this.setSize(MainWindow.MAIN_WINDOW.getToolkit().getScreenSize());
		this.setSize(this.INITIAL_SIZE);
		this.setLocation(MainWindow.getMiddleOfScreen(this));

		MainWindow.MENU = new SimMenuBar();
		this.setMenuBar(MENU);


		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent event) {
				exit();
			}

			public void windowActivated(WindowEvent e) {
				if (!SimMenuBar.STOP.isEnabled()) {
					MainWindow.WEST_PANEL.realSetEnabled(true);
				}

				MainWindow.NORTH_PANEL.setEnabled(true);
			}

			public void windowDeactivated(WindowEvent e) {
				MainWindow.WEST_PANEL.realSetEnabled(false);
				MainWindow.NORTH_PANEL.setEnabled(false);
			}
		});

		try {
			MainWindow.WEST_PANEL.initialize();
		} catch (SimException e) {
			MainWindow.OK_WINDOW.setDescription("Warning");
			MainWindow.OK_WINDOW.setMessage(e.getMessage());
			MainWindow.OK_WINDOW.setVisible(true);
		}

		//workspace chooser
		//Workspace ws = new Workspace();
		//ws.setVisible(true);
		//WAIT_BLOCK.await();
		//if (MainWindow.WORKSPACE == null) {
		//	exit();
		//}
		setWorkspace(System.getProperty("user.dir"),null); //set workspace to current directory
		this.setVisible(true);
	}

	public static void setWorkspace(String path, String fileName) {
		// null workspace --> exit
		if (path == null) {
			return;
		}
		//set workspace, attach / at the end if needed
		MainWindow.WORKSPACE = path;
		if (path.charAt(path.length() - 1) != File.separatorChar) {
			MainWindow.WORKSPACE += File.separator;
		}
		//set title bar
		if (fileName == null) {
			MainWindow.MAIN_WINDOW.setTitle(MainWindow.PROGRAM_NAME + " " + MainWindow.VERSION
					+ " - " + MainWindow.WORKSPACE + MainWindow.DEFAULT_FILENAME);
		} else {
			MainWindow.MAIN_WINDOW.setTitle(MainWindow.PROGRAM_NAME + " " + MainWindow.VERSION
					+ " - " + MainWindow.WORKSPACE + fileName);
		}
		//set Lastpath
		SaveLoadShortcut.LASTPATH = MainWindow.WORKSPACE;
	}

	public void exit() {
		if (MainWindow.MASTER != null) {
			MainWindow.MASTER.destroy();
		} else {
			if (CentralPanel.ACTIVE_GRID.isModified()) {
				String title = "Confirm Exit";
				String msg = "Do you want to save the changes you made?";

				Object[] options = {"Save", "Don't Save", "Cancel"};
				int confirm = JOptionPane.showOptionDialog(null,
						msg, title, JOptionPane.DEFAULT_OPTION,
						JOptionPane.WARNING_MESSAGE,
						null,
						options,
						options[2]);
				switch (confirm) {
					case 0:
						NorthPanel.STORE_SHORTCUT.save();
						if (!CentralPanel.ACTIVE_GRID.isModified()) {
							System.exit(0);
						}
						break;
					case 1:
						System.exit(0);
						break;
					case 2:
						break;
				}
			} else {
				System.exit(0);
			}
		}
	}
}
