/**
 * package contains the gui classes
 */
package simogl.gui;

import java.awt.BorderLayout;
import java.awt.DisplayMode;
import java.awt.GraphicsEnvironment;

import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.swing.JFrame;
import javax.swing.JPanel;

import simogl.gamecore.Renderer;

import com.sun.opengl.util.FPSAnimator;

public class MainFrame extends JFrame
{
	private static GLCanvas canvas = null;
	private static FPSAnimator animator = null;
	
	private JPanel rightMenuContainerPanel = null;
	
	/**
	 * Creates the mainframe of the game with the specified title.
	 * Use this constructor to create a frame in windowed mode.
	 * @param title title, that will be shown in the window's title bar
	 */
	public MainFrame(String title)
	{
		this.createGuiComponents(title);
	}
	
	/**
	 * Creates the mainframe of the game with the specified parameters.
	 * Use this constructor to create a frame in fullscreen mode.
	 * @param title title, that will be shown in the window's title bar
	 * @param width width in pixels
	 * @param height height in pixels
	 * @param bitDepth bit depth of the picture, normally 16 or 32
	 */
	public MainFrame(String title, int width, int height, int bitDepth)
	{
		this.createGuiComponents(title);

		
		// things to enable fullscreen mode
		this.setUndecorated(true);
		GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);
		GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setDisplayMode(
			(new DisplayMode(width, height, bitDepth,
					DisplayMode.REFRESH_RATE_UNKNOWN)));
	}
	
	/**
	 * Helper method to create all the GUI components
	 *
	 */
	private void createGuiComponents(String title)
	{
		this.setLayout(new BorderLayout());		
		this.setTitle(title);
		this.setSize(700,550);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// JOGL stuff
		GLCapabilities glCaps = new GLCapabilities();
		glCaps.setStencilBits(8); // neccessary to use shadow matrix approach 
		canvas = new GLCanvas(glCaps); 			
		canvas.addGLEventListener(new Renderer(canvas, this));		
		this.add(canvas,BorderLayout.CENTER);				
		animator = new FPSAnimator(canvas,65);
		
		this.rightMenuContainerPanel = new RightMenuContainerPanel();
		this.add(rightMenuContainerPanel, BorderLayout.EAST);		
	}
	
	/**
	 * Returns the GLCanvas object of the mainframe.
	 * @return GLCanvas assigned to the mainframe
	 */
	public GLCanvas getGLCanvas()
	{
		return canvas;
	}
	
	/**
	 * Returns the FPSAnimator object of the mainframe.
	 * @return FPSAnimator object assigned to the mainframe
	 */
	public FPSAnimator getAnimator()
	{
		return animator;
	}
}
