/// Copyright (c) 2009 Ruben Penalva. All rights reserved.
/// Redistribution and use in source and binary forms, with or without modification, 
/// are permitted provided that the following conditions are met:
///    1. Redistributions of source code must retain the above copyright notice, this 
///    list of conditions and the following disclaimer.
///    2. Redistributions in binary form must reproduce the above copyright notice,
///    this list of conditions and the following disclaimer in the documentation and/or
///    other materials provided with the distribution.
///    3. The name of the author may not be used to endorse or promote products derived
///    from this software without specific prior written permission.
///
/// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
/// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
/// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 
/// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
/// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
/// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
/// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 
/// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

/// @file Window.cpp
/// This source file is part of 2dRender LetterCDT Test.
/// The file holds the Window class implementation.
/// @author Ruben Penalva (ruben.penalva@rpenalva.com)

#include "Window.h"

#include "IRunner.h"
#include "IMouseListener.h"
#include "IKeyboardListener.h"
#include "IMenuListener.h"

#include <GL/glut.h>

#include <assert.h>

using namespace GlutWrapper;

IRunner* Window::m_runner = 0;

IMenuListener* Window::m_menuListener = 0;

IKeyboardListener* Window::m_keyboardListener = 0;

Window::Window(int argc, char** argv, int positionX, int positionY, 
			   int width, int height, const std::string& windowName,
			   IRunner* runner, const std::vector<MenuEntry>& menuEntries,
			   IMenuListener* menuListener,
			   IMouseListener* mouseListener, 
			   IKeyboardListener* keyboardListener, bool idleRunning)
{
	InitializeGlut(argc, argv, positionX, positionY, width, height, 
				   windowName, runner, menuEntries, menuListener, 
				   mouseListener, keyboardListener, idleRunning);
}

Window::~Window(void)
{
}


void Window::InitializeGlut(int argc, char** argv, int positionX, int positionY, 
							int width, int height, const std::string& windowName,
							IRunner* runner, const std::vector<MenuEntry>& menuEntries,
							IMenuListener* menuListener,
							IMouseListener* mouseListener, 
							IKeyboardListener* keyboardListener, bool idleRunning)
{
	assert(runner);
	
	m_runner = runner;

	m_menuListener = menuListener;

	m_keyboardListener = keyboardListener;

	glutInit(&argc, argv);
	
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowPosition(positionX, positionY);
	glutInitWindowSize(width, height);
	glutCreateWindow(windowName.c_str());
	glutDisplayFunc(DisplayFunc);
	glutReshapeFunc(ReshapeFunc);

	if (idleRunning)
	{
		glutIdleFunc(DisplayFunc);
	}
	
	if (m_keyboardListener)
	{
		glutKeyboardFunc(ProcessNormalKeys);
	}


	if (menuListener)
	{	
		glutCreateMenu(ProcessMenuEvents);
		int menuEntriesCount = menuEntries.size();
		for (int i = 0; i < menuEntriesCount; ++i)
		{
			const MenuEntry& entry = menuEntries[i];
			glutAddMenuEntry(entry.m_name.c_str(), entry.m_option);
		}

		// attach the menu to the right button
		glutAttachMenu(GLUT_RIGHT_BUTTON);
	}

	glutIgnoreKeyRepeat(GLUT_KEY_REPEAT_ON);

	runner->InitializeOpengl(width, height);

	glutMainLoop();

}

void Window::DisplayFunc(void)
{
	assert(m_runner);

	m_runner->Run();
}

void Window::ReshapeFunc(int width, int height)
{
	assert(m_runner);

	m_runner->Reshape(width, height);
}

void Window::ProcessMenuEvents(int option)
{
	assert(m_menuListener);

	m_menuListener->ProcessMenuEvents(option);
}

void Window::ProcessNormalKeys(unsigned char key, int x, int y)
{
	if (m_keyboardListener)
	{
		m_keyboardListener->ProcessNormalKeys(key);
	}
}