#include "Wumbo-Header.h"
#if PLATFORM_WINDOWS || PLATFORM_OSX

#if PLATFORM_WINDOWS
#include <GL/glew.h>
#include <windows.h>
#include <gl/gl.h>
#endif
#if PLATFORM_OSX
#include <OpenGL/gl.h>
#endif

#include "Wumbo-WindowsRoot.h"
#include "Wumbo-Program.h"
#include "Wumbo-InputManager.h"
#include "Wumbo-Scene.h"
#include "Wumbo-KeyboardInput.h"
#include "Wumbo-MouseInput.h"
#include "Wumbo-Renderer.h"
#include "Wumbo-VirtualRenderTarget.h"
#include "Wumbo-Functions.h"


extern std::string MULDERRESOURCE(std::string str);

void Wumbo::WindowsRoot::go(Wumbo::Program *prog)
{
	program = prog;
	program->setSFMLWindow(new sf::Window(sf::VideoMode(1,1,32),"Lemons",sf::Style::Close,sf::ContextSettings(24,0,0,1,1)));
	/*char *vers = (char*)glGetString(GL_VERSION);
	if (atof(vers) < 2.0f)
	{
		Wumbo::MsgBox("This game requires a graphics card with OpenGL 2.0 or later. Sadly, yours doesn't.\n\nBoohoo.","Wumbo Framework");
		exit(0);
	}*/
	//glClearColor(0.f,1.f,0.f,1.f);
	//glClear(GL_COLOR_BUFFER_BIT);
	//glFlush();
	/*sf::Texture tex;
	tex.loadFromFile(MULDERRESOURCE("load.png"));
	sf::Sprite spr(tex);
	spr.setScale(1,1);
	program->getSFMLWindow()->draw(spr);
	program->getSFMLWindow()->display();*/
	//program->getSFMLWindow()->ce
#if PLATFORM_WINDOWS
	char *vers = (char*)glGetString(GL_VERSION);
	if (atof(vers) >= 2.0f)
		glewInit();
#endif
	 
	glViewport(0,0,program->getSFMLWindow()->getSize().x,program->getSFMLWindow()->getSize().y);
	glEnable(GL_SCISSOR_TEST);
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
	glEnable(GL_TEXTURE_2D);

	//glEnable(GL_CULL_FACE);

	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);

	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	//glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);
	//glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA, GL_ZERO);

	program->begin();

	program->getInputManager()->update();
	program->update();
	program->getRenderer()->resetVirtualRenderTarget();
	program->render();
	program->getSFMLWindow()->display();
	program->getSFMLWindow()->setKeyRepeatEnabled(false);

	clock_t timeSinceLastFrame = clock();

	bool loop = true;
	//program->getSFMLWindow()->clear();
	while(loop)
	{
		if (program->getRenderer() == NULL)
		{
			FATALMSG("Program has no Renderer! ... Dying.")
			loop = false;
			break;
		}

		sf::Event event;
		while(program->getSFMLWindow()->pollEvent(event))
		{
			if (event.type == sf::Event::KeyPressed || event.type == sf::Event::KeyReleased)
			{
				if (program->getInputManager() != NULL)
					if (program->getInputManager()->getReceiveKeyboardUpdates())
						program->getInputManager()->getKeyboardState()->handleEvent(event);
			}
			if (event.type == sf::Event::MouseButtonPressed || event.type == sf::Event::MouseButtonReleased || event.type == sf::Event::MouseMoved)
			{
				if (program->getInputManager()->getReceiveMouseUpdates())
				{
					program->getInputManager()->getMouseState()->handleEvent(event);
				}
			}
			if (event.type == sf::Event::Closed)
				program->getSFMLWindow()->close();
		}
		if (!program->getSFMLWindow()->isOpen())
		{
			if (program->getScene() != NULL)
				program->getScene()->end();
			loop = false;
			break;
		}
		program->getInputManager()->update();
		program->update();

		program->getRenderer()->resetVirtualRenderTarget();
		program->render();
		program->getSFMLWindow()->display();
		if (program->getInputManager() != NULL)
		{
			if (program->getInputManager()->getReceiveKeyboardUpdates())
				program->getInputManager()->getKeyboardState()->onEndOfFrame();
			if (program->getInputManager()->getReceiveMouseUpdates())
				program->getInputManager()->getMouseState()->onEndOfFrame();
		}
	}
}
#endif