#ifndef APP_H
#define APP_H

#include <GL/glew.h>
#include <SDL2/SDL.h>

#include "da.h"

#include "vec2.h"
#include "window.h"

#include "buffer.h"

struct app {
	struct window win;
	SDL_Renderer *rdr;
	SDL_GLContext glctx;
	struct buffer buf;
	bool running;
	uint64_t target_fps;
	double dt;
	uint32_t last_press;
	struct {
		uint8_t tab_size;
	} cfg;
	bool show_fps;
	struct free_glyph_render *fgr;
	struct cursor_render *cr;
};

#include "free_glyph.h"

struct ret_app_vec2 {
	struct app f1;
	struct vec2 f2;
};

struct app app_create(const char *win_title);
void app_destroy(struct app app);

void app_set_text_color(struct app app, uint32_t color);
uint32_t app_get_text_color(struct app app);

void app_render_cursor_in_pos(struct app app, struct vec2 pos, uint32_t color);
void app_render_cursor(struct app app, uint32_t color);
void app_render_char(struct app app, const char c, struct vec2 pos,
		     double scale);

void app_render_buffer(struct app app, struct vec2 pos,
		       uint32_t color, double scale);
void app_render_text(struct app app, const char *text, size_t text_size,
		     struct vec2 pos, uint32_t color, double scale);

struct vec2 app_calc_cur_pos(struct app app);

void MessageCallback(GLenum source, GLenum type, GLuint id,
		     GLenum severity, GLsizei length,
		     const GLchar *msg, const void *up);

struct vec2i app_calc_cursor(struct app app);

struct ret_app_vec2 app_calc_buffer_cam(struct app app, double dt);

#if defined(APP_IMP) || defined(IMP)

#define UNHEX_COLOR(hex)			\
	(uint8_t) ((hex >> 24) & 0xFF),	\
	(uint8_t) ((hex >> 16) & 0xFF),	\
	(uint8_t) ((hex >> 8 ) & 0xFF),	\
	(uint8_t) ((hex >> 0 ) & 0xFF)

#include <GL/glew.h>

#include "sc.h"

struct app
app_create(const char *win_title)
{
	SCE(SDL_Init(SDL_INIT_VIDEO));

	struct app app = {
		.win = window_create(win_title),
		.rdr = NULL,
		.running = true,
		.buf = buffer_create(),
		.cfg = {
			.tab_size = 8
		},
		.target_fps = 120,
	};

	app.dt = (1.0 / (double)app.target_fps) * 3;

	{
		SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
		SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
		SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
				    SDL_GL_CONTEXT_PROFILE_CORE);

		int32_t major = 0;
		int32_t minor = 0;

		SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);
		SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor);
		printf("GL Version: %d.%d\n", major, minor);
	}

	/* SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, GL_DOUBLEBUFFER); */
	/* SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1); */
	/* SDL_GL_SetSwapInterval(0); */

	app.glctx = SDL_GL_CreateContext(app.win.ptr);
	SCP(app.glctx);

	if ( GLEW_OK != glewInit() ) {
		fprintf(stderr, "Could Not Initilize GLEW!\n");
		exit(EXIT_FAILURE);
	}

	if ( ! GLEW_ARB_draw_instanced ) {
		fprintf(stderr,
			"WARNING! GLEW_ARB_draw_instanced is not availible\n");
		exit(EXIT_FAILURE);
	}
	if ( ! GLEW_ARB_instanced_arrays ) {
		fprintf(stderr,
			"WARNING! GLEW_ARB_instanced_arrays"
			" is not availible\n");
		exit(EXIT_FAILURE);
	}

	if ( GLEW_ARB_debug_output ) {
		glEnable(GL_DEBUG_OUTPUT);
		glDebugMessageCallback(MessageCallback, 0);
	} else {
		fprintf(stderr,
			"WARNING! GLEW_ARB_debug_output is not availible\n");
	}

	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	return app;
}

void
app_destroy(struct app app)
{
	SDL_DestroyWindow(app.win.ptr);
	SDL_GL_DeleteContext(app.glctx);
	free(app.buf.data.items);
	SDL_Quit();
}


#define BRANCHLESS_IF(cond, when_true, when_false) \
	(((cond)) * (when_true) + (!(cond)) * (when_false))

struct vec2
app_calc_cur_pos(struct app app)
{
	size_t col = 0;
	size_t row = buffer_calc_cur_row(&app.buf);
	enum buffer_err err;
	/* RET_UNWRAP2(col, row, */
	/* 	    struct ret_size_t_size_t, */
	/* 	    buffer_calc_cur_pos(app.buf)); */
	size_t start_line;
	start_line = buffer_index_bw_char(&app.buf, '\n', app.buf.cur, &err);
	col = (app.buf.cur - start_line) - 1;
	if ( err == BUFFER_ERR_NOT_FOUND ) {
		col = app.buf.cur;
	}

	size_t tabs_n = 0;
	tabs_n = buffer_count_char_between(&app.buf, '\t', start_line,
					   app.buf.cur, &err);
	col += ( tabs_n > 0 ) * ((tabs_n * app.cfg.tab_size) - tabs_n);
	double row_px = -(((double)row)
			  * app.fgr->atlas_dim.y);

	double col_px = (((double) col)
			 * ((double) 1));

	return vec2(col_px, row_px);
}


void
MessageCallback(GLenum source, GLenum type, GLuint id,
		GLenum severity, GLsizei length,
		const GLchar *msg, const void *up)
{
	(void) source; (void) id; (void) length; (void) up;

	fprintf(stderr,
		"GL CALLBACK: %s type = 0x%x, severity = 0x%x, message = %s\n",
		(type == GL_DEBUG_TYPE_ERROR ? "*** GL ERROR ***" : ""),
		type, severity, msg);
}

struct vec2i
app_calc_cursor(struct app app)
{
	size_t row, col;
	RET_UNWRAP2(col, row,
		    struct ret_size_t_size_t, buffer_calc_cur_pos(&app.buf));

	return vec2i((int32_t)col, (int32_t)row);
}

struct ret_app_vec2
app_calc_buffer_cam(struct app app, double dt)
{
	struct vec2 cur_pos = app_calc_cur_pos(app);
	struct vec2 cpos = app.buf.cam.pos;
	struct vec2 cvel = app.buf.cam.vel;
	cvel = vec2_sub(cur_pos, cpos);
	cpos = vec2_add(cpos, vec2_mul(cvel, vec2s(dt)));
	app.buf.cam.pos = cpos;
	app.buf.cam.vel = cvel;
	return (struct ret_app_vec2) {
		.f1 = app,
		.f2 = cpos
	};
}

#endif /* defined(APP_IMP) || defined(IMP) */

#endif
