#include "utils/logger.h"
#include "render/font.h"
#include "render/shader.h"
#include "render/vertexbuffer.h"
#include "render/indexbuffer.h"
#include "render/renderdevice.h"
#include "render/shadersystem.h"
#include "render/ui.h"
#include "render/texture2d.h"
#include "render/texturesmanager.h"

#include <stb_easy_font.h>

struct FontGlobals {
	VertexBuffer* vb;
	IndexBuffer* ib;
	Shader* shader;

	UIVertex* vertices = nullptr;
	uint16_t* indices = nullptr;
	uint16_t count = 0;
	uint16_t position = 0;
	uint16_t Indexposition = 0;
	uint16_t currentIdx = 0;
} g_font;

void fontInit()
{
	// Create vertex and index buffer
	
	// same as ui
	g_font.vb = g_renderDevice->CreateVertexBuffer( nullptr, MAX_UI_VERTICES, true );
	
	// Create shader

	InputLayoutDesc_t inputLayout[] =
	{
		{ VERTEXATTR_VEC2, SHADERSEMANTIC_POSITION },
		{ VERTEXATTR_VEC2, SHADERSEMANTIC_TEXCOORD },
		{ VERTEXATTR_VEC4, SHADERSEMANTIC_COLOR },
	};

	g_font.shader = g_shaderSystem->CreateShader("ui_base", "content/shaders/ui_base.vs", "content/shaders/ui_base.ps", inputLayout, sizeof(inputLayout) / sizeof(inputLayout[0]));
	g_font.shader->m_stride = sizeof( UIVertex );	
}

void fontShutdown()
{
	delete g_font.ib;
	delete g_font.vb;
}

inline unsigned char toByteColor(const Vec4& color)
{

}

void fontDrawString(const char* text, float x, float y, float w, float h, const Vec4& color)
{
	// lock buffers

	UIVertex* vertices = (UIVertex*)g_font.vb->MapBuffer(BufferAccess::WRITE_ONLY);

	//int num_quads = stb_easy_font_print(x, y, (char*)text, , buffer, sizeof(buffer));

	//static char buffer[99999]; // ~500 chars
	//int num_quads;

	//num_quads = stb_easy_font_print(x, y, (char*)text, NULL, buffer, sizeof(buffer));

	//glColor3f(r, g, b);
	//glEnableClientState(GL_VERTEX_ARRAY);
	//glVertexPointer(2, GL_FLOAT, 16, buffer);
	//glDrawArrays(GL_QUADS, 0, num_quads * 4);
	//glDisableClientState(GL_VERTEX_ARRAY);
}
