/*
 * Created on Nov 28, 2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package WheelOfFortune.game;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import WheelOfFortune.game.player.Player;
import WheelOfFortune.game.userOutput.MusicThread;
import WheelOfFortune.localManager.*;
import WheelOfFortune.puzzleManager.PuzzleManager;
import WheelOfFortune.puzzleManager.PuzzleManagerInput;
import WheelOfFortune.puzzleManager.PuzzleManagerOutput;

/**
 * @author warren
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class MainMenu extends JFrame{
	private MainMenu mm;
	private MainPanel mp;
	private Player[] p;
	private MusicThread music;
	private int butleft = 550,buttop = 300,butwidth = 200,butheight=200;
	private OptionModel om;
	private OptionFrame of;
	public MainMenu()
	{
		mm = this;
		mp = new MainPanel();
		loadOption();

		p = new Player[3];
		music = new MusicThread();
		music.play();
		Container c = this.getContentPane();
		c.add(mp);
		pack();
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension d = tk.getScreenSize();
		int screenHeight = d.height;
		int screenWidth = d.width;
		setLocation((screenWidth - this.getWidth()) / 2, (screenHeight - this.getHeight()) / 2);
		this.setTitle("Future Designers' Wheel of Fortune");
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.addWindowListener(new MainWindowListener());

	}
	public class MainWindowListener extends WindowAdapter
	{
		public void windowClosing(WindowEvent e)
		{
			try{
				om.save();
				
			}
			catch(IOException ex){}
			
			System.exit(0);
		}

	}
	public void loadOption()
	{
		try
		{
			FileInputStream fis = new FileInputStream(OptionModel.OPTFILE);
			ObjectInputStream ois = new ObjectInputStream(fis);
		
			om = (OptionModel)ois.readObject();
			
		}
		catch(Exception e)
		{
			om = new OptionModel();
		
		}
	}
	public OptionFrame getOF()
	{
		return of;
	}
	public class MainPanel extends JPanel
	{
		private Image mainBG;
		private final String PATH = ".\\WheelOfFortune\\game\\";
		private JLabel newGame, organizePuzzles, puzzlesOnline, options, exit;
		private int width = 800, height = 600;
		private JPanel buttonPanel;

		public MainPanel()
		{
			this.setPreferredSize(new Dimension(width,height));
			try {
				mainBG = ImageIO.read(new File(PATH + "mainbg.jpg"));
				
			}
			catch (IOException e) {
				e.printStackTrace();
			}		
			

			this.setLayout(null);
			buttonPanel = new JPanel();
			buttonPanel.setPreferredSize(new Dimension(300,200));
			add(buttonPanel);
			buttonPanel.setBounds(butleft,buttop,butwidth,butheight);
			newGame = new JLabel("Start New Game");
			organizePuzzles = new JLabel("Organize your puzzles");
			puzzlesOnline = new JLabel("Puzzles Online");
			options = new JLabel("Options");
			exit = new JLabel("Exit");
			buttonPanel.setOpaque(false);
			buttonPanel.setLayout(new GridLayout(5,1));
			buttonPanel.add(newGame);
			buttonPanel.add(organizePuzzles);
			buttonPanel.add(puzzlesOnline);
			buttonPanel.add(options);
			buttonPanel.add(exit);
			newGame.setHorizontalAlignment(JLabel.CENTER);
			organizePuzzles.setHorizontalAlignment(JLabel.CENTER);
			puzzlesOnline.setHorizontalAlignment(JLabel.CENTER);
			options.setHorizontalAlignment(JLabel.CENTER);
			exit.setHorizontalAlignment(JLabel.CENTER);
			newGame.setOpaque(false);
			organizePuzzles.setOpaque(false);
			puzzlesOnline.setOpaque(false);
			options.setOpaque(false);
			exit.setOpaque(false);
			
			
			MainListener ml = new MainListener();
			newGame.addMouseListener(ml);
			organizePuzzles.addMouseListener(ml);
			puzzlesOnline.addMouseListener(ml);
			options.addMouseListener(ml);
			exit.addMouseListener(ml);
		}
		public void paintComponent(Graphics g)
		{
			super.paintComponent(g);
			int imageWidth;
			int imageHeight;
			if(mainBG == null)
				return;
			else
			{
				imageWidth = mainBG.getWidth(this);
				imageHeight = mainBG.getHeight(this); 
				g.drawImage(mainBG, (getWidth() - imageWidth) / 2, 
						(getHeight() - imageHeight) / 2, null);
			}
			buttonPanel.setBounds(butleft,buttop,butwidth,butheight);
		}
		public class MainListener extends MouseAdapter
		{
			public void mouseEntered(MouseEvent e)
			{
				((JLabel)e.getSource()).setForeground(Color.BLUE);
				repaint();
			}
			public void mouseExited(MouseEvent e)
			{
				((JLabel)e.getSource()).setForeground(Color.BLACK);
				repaint();
			}
			public void mouseClicked(MouseEvent e)
			{
				JLabel button = (JLabel)e.getSource();
				if(button == exit)
				{
					try{
						om.save();
					}
					catch(IOException ex){}
					System.exit(0);
				}
				if(button == newGame)
				{
					mm.setVisible(false);
					try{
						of = new OptionFrame(mm,om,music,true);
						of.show();
					}
					catch(Exception ex)
					{
			
						ex.printStackTrace();
						JOptionPane.showMessageDialog(mp,
								"Fatal Error Occured in Game",
								"Error",
								JOptionPane.ERROR_MESSAGE);
						mm.setVisible(true);
					}

				}
				if(button == organizePuzzles)
				{
					  LocalManagerOutput lmo = new LocalManagerOutput();
					  LocalManager lm = new LocalManager(lmo);
					  LocalManagerInput in = new LocalManagerInput(lm, lmo);
					  lmo.display(mm);
					  mm.setVisible(false);
				}
				if(button == puzzlesOnline)
				{
					mm.setVisible(false);
					try{
					PuzzleManagerOutput out = new PuzzleManagerOutput(mm);
					PuzzleManager pm = new PuzzleManager(out);
					PuzzleManagerInput in = new PuzzleManagerInput(pm,out);
					
					out.display();
					}
					catch(Exception ex)
					{
						JOptionPane.showMessageDialog(mp,
								"Puzzle Online was unable to load",
								"Error",
								JOptionPane.ERROR);
						mm.setVisible(true);
					}

				}
				if(button == options)
				{
					mm.setVisible(false);
					try{
						OptionFrame of = new OptionFrame(mm,om,music,false);
						of.show();
					}
					catch(Exception ex)
					{
					
						ex.printStackTrace();
						JOptionPane.showMessageDialog(mp,
								"Fatal Error Occured in Game",
								"Error",
								JOptionPane.ERROR_MESSAGE);
						mm.setVisible(true);
					}
				}
			}
		}
	}
}
