/// 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 Gpu Quadratic Curve Render 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;

IMouseListener* Window::m_mouseListener = 0;

MouseState Window::m_mouseState;

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;

	m_mouseListener = mouseListener;

	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 (m_mouseListener)
	{
		glutMouseFunc(ProcessMouse);	
		glutMotionFunc(ProcessMouseMotion);
		glutPassiveMotionFunc(ProcessMousePassiveMotion);
	}

	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)
{
	assert(m_keyboardListener);

	m_keyboardListener->ProcessNormalKeys(key);
}

void Window::ProcessMouse(int button, int state, int x, int y)
{
	assert(m_mouseListener);

	m_mouseState.m_oldX = m_mouseState.m_x;
	m_mouseState.m_oldY = m_mouseState.m_y;

	m_mouseState.m_x = x;
	m_mouseState.m_y = y;

	m_mouseState.m_button = static_cast<MouseButton>(button);
	m_mouseState.m_buttonState = static_cast<MouseButtonState>(state);

	m_mouseListener->ProcessMouse(m_mouseState);
}	

void Window::ProcessMouseMotion(int x, int y)
{
	assert(m_mouseListener);

	m_mouseState.m_oldX = m_mouseState.m_x;
	m_mouseState.m_oldY = m_mouseState.m_y;

	m_mouseState.m_x = x;
	m_mouseState.m_y = y;

	m_mouseListener->ProcessMouse(m_mouseState);
}

void Window::ProcessMousePassiveMotion(int x, int y)
{		
	assert(m_mouseListener);
	
	m_mouseState.m_buttonState = Up;	

	m_mouseState.m_oldX = m_mouseState.m_x;
	m_mouseState.m_oldY = m_mouseState.m_y;

	m_mouseState.m_x = x;
	m_mouseState.m_y = y;

	m_mouseListener->ProcessMouse(m_mouseState);
}