#include "Wumbo-RenderTarget.h"
#include "Wumbo-Renderer.h"
#if PLATFORM_WINDOWS
#include <GL/glew.h>
#include <GL/wglew.h>
#endif




namespace Wumbo
{
//
//		That function below, removeBind, is there because
//		Visual Studio throws a hissy fit for some reason if it's a class method.
//		Sorry!
//
#if PLATFORM_IOS
	void removeBind(GLint zero)
	{
		glBindFramebuffer(GL_FRAMEBUFFER,zero);
	}
#else
	void removeBind()
	{
		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
	}
#endif

	void RenderTarget::bind()
	{
		if (!GL2)
		{
			printf("RenderTarget::bind is not available in OpenGL 1.x\n");
			return;
		}
#if PLATFORM_IOS
		glBindFramebuffer(GL_FRAMEBUFFER, frameID);
#else
		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameID);
#endif
	}

	void RenderTarget::initialize(Renderer *rend, unsigned int ww, unsigned int hh)
	{
		//glGenFramebuffers = wglGetProcAddress("glGenFramebuffers");;
		if (rend->getGLVersion() == 2)
			GL2 = true;
		width = ww;
		height = hh;
		if (!GL2)
		{
			printf("RenderTarget::initialize is not available in OpenGL 1.x\n");
			return;
		}
		initialized = true;
		
		glGenTextures(1, &textureID);
		glBindTexture(GL_TEXTURE_2D, textureID);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glBindTexture(GL_TEXTURE_2D, 0);
		
#if PLATFORM_IOS
		glGenFramebuffers(1, &frameID); // Generate one frame buffer and store the ID in fbo
		glBindFramebuffer(GL_FRAMEBUFFER, frameID); // Bind our frame buffer
		
			/*glGenRenderbuffers(1, &renderID);
			glBindRenderbuffer(GL_RENDERBUFFER, renderID);
				glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, width, height);
				glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderID);
			glBindRenderbuffer(GL_RENDERBUFFER, 0);*/
		
		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureID, 0);
				
		GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
		if(status != GL_FRAMEBUFFER_COMPLETE) {
			NSLog(@"failed to make complete framebuffer object %x", status);
		}
		
		glBindFramebuffer(GL_FRAMEBUFFER, 0); // Unbind our frame buffer
#else
		glGenFramebuffersEXT(1, &frameID); // Generate one frame buffer and store the ID in fbo
		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, frameID); // Bind our frame buffer  
		glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, textureID,0);
		glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); // Unbind our frame buffer
#endif

		printf("renderbuff %i tex %i\n",frameID,textureID);

		texture = new Texture(textureID,width,height);
	}

	RenderTarget::RenderTarget(Renderer *rend, unsigned int ww, unsigned int hh)
	{
		frameID = 0;
		texture = 0;
		initialized = false;
		GL2 = false;
		initialize(rend, ww,hh);
	}

	RenderTarget::RenderTarget()
	{
		frameID = 0;
		texture = 0;
		initialized = false;
		width = 1;
		height = 1;
		GL2 = false;	
	}

	unsigned int RenderTarget::getWidth()
	{
		return width;
	}
	unsigned int RenderTarget::getHeight()
	{
		//printf("height = %u\n",height);
		return height;
	}

	/*GLuint RenderTarget::getID()
	{
		return frameID;
	}*/
	Texture* RenderTarget::getTexture()
	{
		if (!GL2)
		{
			printf("RenderTarget::getTexture is not available in OpenGL 1.x\n");
			return NULL;
		}
		return texture;
	}
}