
#include <jni.h>
#include <errno.h>

#include "osmroute.h"
#include "system.h"
#include "utf8.h"

#include <ft2build.h>
#include FT_FREETYPE_H

struct point {
	GLfloat x;
	GLfloat y;
	GLfloat s;
	GLfloat t;
};

int ft_init = 0;
FT_Library ft;


int font_init () {
	/* Initialize the FreeType2 library */
	if (FT_Init_FreeType(&ft)) {
		d_printf ("Could not init freetype library\n");
		return 0;
	}
	d_printf ("font init");
	return 1;
};


struct font* font_load(char *fname) {
	AAssetManager *mgr = engine.app->activity->assetManager;
	AAsset* asset = NULL;
	struct font *f = NULL;
	int bytesread, fontsize;
	d_printf ("font load:%s", fname);

	if ((asset = AAssetManager_open(mgr, fname, AASSET_MODE_UNKNOWN)) == 0) {
        LOGW("Asset \"%s\" not found.", fname);
        return NULL;
    }

	/* load font file into memory */
	f = (struct font *) ml_malloc (sizeof (struct font));
    f->buffersize = AAsset_getLength(asset);
    f->buffer = (char *) ml_malloc (f->buffersize);
	bytesread = AAsset_read(asset, f->buffer, f->buffersize);	
    AAsset_close(asset);
	
	/* Load a font from memory */
	if (FT_New_Memory_Face(ft, f->buffer, f->buffersize, 0, &f->face)) {
		d_printf("Could not read file from memory.\n");
		exit (1);;
	}

	FT_Set_Pixel_Sizes(f->face, 0, 16);
	d_printf ("font loaded (size:%d).", f->buffersize);

	FT_Select_Charmap(f->face, FT_ENCODING_UNICODE);
	return f;
}

/**
 * Render text using the currently loaded font and currently set font size.
 * Rendering starts at coordinates (x, y), z is always 0.
 * The pixel coordinates that the FreeType2 library uses are scaled by (sx, sy).
 */
void font_draw (struct font *f, char *text, float x, float y, float sx, float sy) {
	char *p;
	int i;
	uint32_t u8chr;
	GLfloat vp[12] = { 	0.0, 0.0, 0.0,
						0.0, 0.0, 0.0,
						0.0, 0.0, 0.0,
						0.0, 0.0, 0.0 };
	GLfloat vt[8] = {0, 1,
					 1, 1,
					 1, 0,
					 0, 0};
	FT_GlyphSlot g = f->face->glyph;
	
	/* Create a texture that will be used to hold one "glyph" */
	GLuint tex;

	glGenTextures (1, &tex);
	glEnable (GL_TEXTURE_2D);
	glActiveTexture (GL_TEXTURE0);
	
	glBindTexture(GL_TEXTURE_2D, tex);
	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	
	/* We require 1 byte alignment when uploading texture data */
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

	glUniform1i (engine.gles_txenabled, 2);
	glUniform1i(engine.gles_txsampler, 0);

	/* Loop through all characters */
	for (p = text; *p;) {
		u8chr = u8_decode (p, &i);
		if (i == 0) {
			p++;
			continue;
		}
		p = p + i;

		/* Try to load and render the character */
		if ((i = FT_Load_Char(f->face, u8chr, FT_LOAD_RENDER))) {
			d_printf ("error:%d", i);
			continue;
		}

		/* Upload the "bitmap", which contains an 8-bit grayscale image, as an alpha texture */
		glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, g->bitmap.width, g->bitmap.rows, 0, GL_ALPHA, GL_UNSIGNED_BYTE, g->bitmap.buffer);
		/* Calculate the vertex and texture coordinates */
		// d_printf ("x: %f   advance:%d   %d", x, g->advance.x, g->advance.x >> 6);

		vp[0] = x;						vp[1] = y + g->bitmap.rows - g->bitmap_top;
		vp[3] = x + g->bitmap.width;	vp[4] = y + g->bitmap.rows - g->bitmap_top;
		vp[6] = x + g->bitmap.width;	vp[7] = y - g->bitmap.rows + g->bitmap.rows - g->bitmap_top;
		vp[9] = x;						vp[10] = y - g->bitmap.rows + g->bitmap.rows - g->bitmap_top;

		glVertexAttribPointer (engine.gles_pos, 3, GL_FLOAT, GL_FALSE, 0, vp);
		glVertexAttribPointer (engine.gles_txcoord, 2, GL_FLOAT, GL_FALSE, 0, vt);
		glDrawArrays(GL_TRIANGLE_FAN,0,4);

//		glVertexAttribPointer (engine.gles_pos, 3, GL_FLOAT, GL_FALSE, 0, vp);
// 		glDrawArrays(GL_LINES,0,2);

		
		/* Advance the cursor to the start of the next character */
		// x += (g->advance.x >> 6);
		// y -= (g->advance.y >> 6);
		if (g->bitmap.width == 0)	x += 2.0;
		else x += g->bitmap.width + 1;
	}

	glBindTexture (GL_TEXTURE_2D, 0);
	glDeleteTextures (1, &tex);
	glUniform1i (engine.gles_txenabled, 0);
	glDisable (GL_TEXTURE_2D);
};
