/*
 * 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.event.*;
import javax.swing.*;

import WheelOfFortune.game.wheel.*;
import WheelOfFortune.game.player.*;
import WheelOfFortune.game.userInput.GameInput;
import WheelOfFortune.game.userOutput.*;
import java.awt.*;
/**
 * @author warren
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class OptionFrame extends JFrame { 
	private OptionFrame of;
	
	private MainMenu mm;
	private StartGamePanel sgp; 
	private final String PATH = ".\\WheelOfFortune\\game\\userOutput\\characters\\";
	
	private OptionModel om;
	private WheelSetupPanel wsp;
	private JTabbedPane tab;
	private JButton ok,cancel;
	private boolean startGame;
	MusicThread music;
	public OptionFrame(MainMenu mm, OptionModel om, MusicThread music,boolean startGame)
	{
		this.music = music;
		this.om = om;
		this.startGame = startGame;
		this.mm = mm;
		this.of = this;
		this.setTitle("Options");
		this.setSize(600,400);
		this.addWindowListener(new OptionWindowListener());
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.setResizable(false);
		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);

		sgp = new StartGamePanel(om);
		tab = new JTabbedPane();
		tab.addTab("Start New Game",sgp);
		
		Wheel fullWheel = null;
		try {
		fullWheel = new Wheel( "WheelOfFortune\\game\\wheel.txt");
		}
		catch(Exception e) { }
		wsp = new WheelSetupPanel(fullWheel, om.getCurWheel());
		tab.addTab("Wheel Setup", wsp);
		
		getContentPane().add(tab, BorderLayout.CENTER);
		
		ok = new JButton("Okay");
		cancel = new JButton("Cancel");

		JPanel bottem = new JPanel();
		bottem.setAlignmentX(JPanel.RIGHT_ALIGNMENT);
		bottem.add(ok);
		bottem.add(cancel);
		getContentPane().add(bottem,BorderLayout.SOUTH);
		OptionListener ol = new OptionListener();
		ok.addActionListener(ol);
		cancel.addActionListener(ol);
		this.setResizable(false);
		

		
	}
	
	public class OptionWindowListener extends WindowAdapter
	{
		public void windowClosing(WindowEvent e)
		{
			setVisible(false);
			mm.setVisible(true);
		}
	}
	public class OptionListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e) 
		{
			if(e.getSource() == ok)
			{
				
				
				for(int i = 0; i < 3; i++)
				{
					om.getP()[i] = new Player(sgp.getP(i).getName(), 
							sgp.getP(i).getPlayerDiff(),
							sgp.getP(i).getPlayerChar(),
							sgp.getP(i).getCharPath());
					om.setCurWheel(wsp.getCurrentWheel());
				}
				om.setNumRounds(sgp.getRounds());
				if(startGame)
				{
					try{
						GameOutput out = new GameOutput(mm,om.getP());
						GameModel gm = new GameModel(out,om,
								"WheelOfFortune\\game\\wheel.txt",
								mm);
						GameInput in = new GameInput(gm,music);
						gm.getOut().setInput(in);
						gm.startGame();
						setVisible(false);
					}
					catch(NoPlayerException npe)
					{
						JOptionPane.showMessageDialog(of,"There are no players");
					}
					catch(Exception ex)
					{
						ex.printStackTrace();
						JOptionPane.showMessageDialog(mm,
								"Fatal Error Occured in Game",
								"Error",
								JOptionPane.ERROR_MESSAGE);
						mm.setVisible(true);
					}
				}
				else
				{
					setVisible(false);
					mm.setVisible(true);
				}
			}
			else
			{
				setVisible(false);
				mm.setVisible(true);
			}
		}
	}
}
