#include "OpenGLUtils.h"


namespace touchcontrols
{
 float GLScaleWidth = 400;
 float GLScaleHeight = -300;

void drawRect(GLuint texture, float x, float y, GLRect &rect)
{
	glBindTexture(GL_TEXTURE_2D, texture);

	glVertexPointer(3, GL_FLOAT, 0, rect.vertices);
	glTexCoordPointer(2, GL_FLOAT, 0, rect.texture);
	glTranslatef(x, -y, 0);
	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

}

void drawRect(GLfloat r,GLfloat g,GLfloat b,GLfloat a, float x, float y, GLRect &rect)
{
	glDisable(GL_TEXTURE_2D);
	glColor4f(r, g, b, a );

	glVertexPointer(3, GL_FLOAT, 0, rect.vertices);
	glTranslatef(x, -y, 0);
	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

	glColor4f(1, 1, 1, 1 );
	glEnable(GL_TEXTURE_2D);
}

void drawLines(float x, float y, GLLines &lines)
{

	glDisable(GL_TEXTURE_2D);

	glVertexPointer(3, GL_FLOAT, 0, lines.vertices);

	glTranslatef(x, -y, 0);

	glDrawArrays(GL_LINES, 0, lines.len);

	glEnable(GL_TEXTURE_2D);
}

FILE* file;

void png_read(png_structp png_ptr, png_bytep data, png_size_t length) {
	fread(data,1,length,file);
}
//This need to be set
std::string graphicsBasePath;
void setGraphicsBasePath(std::string path)
{
	graphicsBasePath = path;
}


GLuint loadTextureFromPNG(std::string filename, int &width, int &height)
{
	std::string full_file = graphicsBasePath + filename + ".png";
	file = fopen(full_file.c_str(), "r");
	if (!file) {
		LOGTOUCH_E("Error opening %s from APK", full_file.c_str());
		return TEXTURE_LOAD_ERROR;
	}

	//header for testing if it is a png
	png_byte header[8];

	//read the header
	fread( header, 1,8,file);

	//test if png
	int is_png = !png_sig_cmp(header, 0, 8);
	if (!is_png) {
		fclose(file);
		LOGTOUCH_E("Not a png file : %s", full_file.c_str());
		return TEXTURE_LOAD_ERROR;
	}

	//create png struct
	png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
			NULL, NULL);
	if (!png_ptr) {
		fclose(file);
		LOGTOUCH_E("Unable to create png struct : %s", full_file.c_str());
		return (TEXTURE_LOAD_ERROR);
	}

	//create png info struct
	png_infop info_ptr = png_create_info_struct(png_ptr);
	if (!info_ptr) {
		png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
		LOGTOUCH_E("Unable to create png info : %s", full_file.c_str());
		fclose(file);
		return (TEXTURE_LOAD_ERROR);
	}

	//create png info struct
	png_infop end_info = png_create_info_struct(png_ptr);
	if (!end_info) {
		png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
		LOGTOUCH_E("Unable to create png end info : %s", full_file.c_str());
		fclose(file);
		return (TEXTURE_LOAD_ERROR);
	}

	//png error stuff, not sure libpng man suggests this.
	if (setjmp(png_jmpbuf(png_ptr))) {
		fclose(file);
		LOGTOUCH_E("Error during setjmp : %s", full_file.c_str());
		png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
		return (TEXTURE_LOAD_ERROR);
	}

	//init png reading
	//png_init_io(png_ptr, fp);
	png_set_read_fn(png_ptr, NULL, png_read);

	//let libpng know you already read the first 8 bytes
	png_set_sig_bytes(png_ptr, 8);

	// read all the info up to the image data
	png_read_info(png_ptr, info_ptr);

	//variables to pass to get info
	int bit_depth, color_type;
	png_uint_32 twidth, theight;

	// get info about png
	png_get_IHDR(png_ptr, info_ptr, &twidth, &theight, &bit_depth, &color_type,
			NULL, NULL, NULL);

	//update width and height based on png info
	width = twidth;
	height = theight;

	// Update the png info struct.
	png_read_update_info(png_ptr, info_ptr);

	// Row size in bytes.
	int rowbytes = png_get_rowbytes(png_ptr, info_ptr);

	// Allocate the image_data as a big block, to be given to opengl
	png_byte *image_data = new png_byte[rowbytes * height];
	if (!image_data) {
		//clean up memory and close stuff
		png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
		LOGTOUCH_E("Unable to allocate image_data while loading %s ", full_file.c_str());
		fclose(file);
		return TEXTURE_LOAD_ERROR;
	}

	//row_pointers is for pointing to image_data for reading the png with libpng
	png_bytep *row_pointers = new png_bytep[height];
	if (!row_pointers) {
		//clean up memory and close stuff
		png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
		delete[] image_data;
		LOGTOUCH_E("Unable to allocate row_pointer while loading %s ", full_file.c_str());
		fclose(file);
		return TEXTURE_LOAD_ERROR;
	}
	// set the individual row_pointers to point at the correct offsets of image_data
	for (int i = 0; i < height; ++i)
		row_pointers[height - 1 - i] = image_data + i * rowbytes;

	//read the png into image_data through row_pointers
	png_read_image(png_ptr, row_pointers);

	//Now generate the OpenGL texture object
	GLuint texture;
	glGenTextures(1, &texture);
	texture += 2;
	glBindTexture(GL_TEXTURE_2D, texture);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
			GL_UNSIGNED_BYTE, (GLvoid*) image_data);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

	//clean up memory and close stuff
	png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
	delete[] image_data;
	delete[] row_pointers;
	fclose(file);

	return texture;

}



}
