#ifndef APP_H
#define APP_H

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

#include "ht.h"
#include "da.h"

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

#include "buffer.h"

DA_DEF_STRUCT(struct buffer, app_buffers);

enum keybind_dir {
	DIR_NO = -1,
	DIR_FORWARD = 0,
	DIR_BACKWARD,
};
struct app;
typedef void (*keybind_func)(struct app *, enum keybind_dir);

struct app_kbd {
	SDL_Keymod mod;
	keybind_func func;
	enum keybind_dir dir;
};

HT_DEF_STRUCT(struct app_kbd, app_kbd_ht);

struct app {
	struct window win;
	SDL_Renderer *rdr;
	SDL_GLContext glctx;
	struct buffer *cbuf;
	struct app_buffers bufs;
	bool running;
	uint64_t target_fps;
	double dt;
	uint32_t last_press;
	SDL_KeyCode last_pressed_key;
	struct {
		uint8_t tab_size;
	} cfg;
	bool show_fps;
	struct free_glyph_atlas *fga;
	struct cursor_render *cr;
	struct app_kbd_ht kbds;
};

#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);

void app_add_global_keybind(struct app *app, const char *keybind,
			    keybind_func func, enum keybind_dir dir);

#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 "sc.h"
#include "str.h"
#include "char.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,
		.cfg = {
			.tab_size = 8
		},
		.target_fps = 120,
	};
	HT_CREATE(app.kbds, 16);
	app.kbds.hash = ht_hash_nop;
	DA_CREATE(app.bufs);
	DA_APPEND(app.bufs, buffer_create());
	app.cbuf = &app.bufs.items[0];

	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.cbuf->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.cbuf);
	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.cbuf, '\n', app.cbuf->cur, &err);
	col = (app.cbuf->cur - start_line) - 1;
	if ( err == BUFFER_ERR_NOT_FOUND ) {
		col = app.cbuf->cur;
	}

	size_t tabs_n = 0;
	tabs_n = buffer_count_char_between(app.cbuf, '\t', start_line,
					   app.cbuf->cur, &err);
	col += ( tabs_n > 0 ) * ((tabs_n * app.cfg.tab_size) - tabs_n);
	double row_px = -(((double)row)
			  * app.fga->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.cbuf));

	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.cbuf->cam.pos;
	struct vec2 cvel = app.cbuf->cam.vel;
	cvel = vec2_sub(cur_pos, cpos);
	cpos = vec2_add(cpos, vec2_mul(cvel, vec2s(dt)));
	app.cbuf->cam.pos = cpos;
	app.cbuf->cam.vel = cvel;
	return (struct ret_app_vec2) {
		.f1 = app,
		.f2 = cpos
	};
}

static SDL_KeyCode
char_to_keycode(char c)
{
	return char_to_lower(c);
}

static SDL_KeyCode
str_to_keycode(struct str str)
{
	if ( str_eq_cstr(str, "<left>", 6) ) {
		return SDLK_LEFT;
	}
	if ( str_eq_cstr(str, "<down>", 6) ) {
		return SDLK_DOWN;
	}
	if ( str_eq_cstr(str, "<up>", 4) ) {
		return SDLK_UP;
	}
	if ( str_eq_cstr(str, "<right>", 7) ) {
		return SDLK_RIGHT;
	}
	if ( str_eq_cstr(str, "<delete>", 8) ) {
		return SDLK_DELETE;
	}
	if ( str_eq_cstr(str, "<backspace>", 11) ) {
		return SDLK_BACKSPACE;
	}
	return SDLK_UNKNOWN;
}

static SDL_Keymod
char_to_keymod(char c)
{
	switch ( c ) {
	case 'N': return KMOD_NONE; break;
	/* case '': return KMOD_LSHIFT; break; */
	/* case '': return KMOD_RSHIFT; break; */
	/* case '': return KMOD_LCTRL; break; */
	/* case '': return KMOD_RCTRL; break; */
	/* case '': return KMOD_LALT; break; */
	/* case '': return KMOD_RALT; break; */
	/* case '': return KMOD_LGUI; break; */
	/* case '': return KMOD_RGUI; break; */
	/* case '': return KMOD_NUM; break; */
	/* case '': return KMOD_CAPS; break; */
	/* case '': return KMOD_MODE; break; */
	/* case '': return KMOD_SCROLL; break; */
	case 'C': return KMOD_CTRL; break;
	case 'S': return KMOD_SHIFT; break;
	case 'A': return KMOD_ALT; break;
	case 'M': return KMOD_GUI; break;
	default: return -1; break;
	}
}



#define IF_GOTO(cond, to)				\
	if ( (cond) ) {				\
		fprintf(stderr, #cond"\n");		\
		goto to;				\
	}

void
app_add_global_keybind(struct app *app, const char *keybind,
		       keybind_func func,
		       enum keybind_dir dir)
{
	struct str kbd_str = str_from_cstr_ns(keybind);
	if ( kbd_str.size == 1 ) {
		SDL_KeyCode key = char_to_keycode(kbd_str.data[0]);
		IF_GOTO(key == SDLK_UNKNOWN, invalid_key);
		struct app_kbd kbd = {
			.mod = KMOD_NONE,
			.func = func,
			.dir = dir
		};
		HT_ISET(app->kbds, (int64_t)(key), kbd);
		return;
	}
	if ( kbd_str.data[0] == '<' ) {
		IF_GOTO(str_contain_char(kbd_str, '-'), invalid_key)
		SDL_KeyCode key = str_to_keycode(kbd_str);
		IF_GOTO(key == SDLK_UNKNOWN, invalid_key);
		struct app_kbd kbd = {
			.mod = KMOD_NONE,
			.func = func,
			.dir = dir
		};
		HT_ISET(app->kbds, (int64_t)(key), kbd);
		return;
	}

	struct str_da words = str_split(kbd_str, ' ');
	IF_GOTO(words.size != 1, invalid_key);
	/* for */ {
		struct str_da chars = str_split(words.items[0], '-');
		IF_GOTO(chars.size != 2, invalid_key);
		IF_GOTO(chars.items[0].size != 1, invalid_key);
		SDL_Keymod mod = char_to_keymod(chars.items[0].data[0]);
		IF_GOTO((int32_t)(mod) == -1, invalid_key);

		SDL_KeyCode key;
		if ( chars.items[1].size == 1 ) {
			key = char_to_keycode(chars.items[1].data[0]);
			IF_GOTO(key == SDLK_UNKNOWN, invalid_key);
		} else if ( chars.items[1].data[0] == '<' ) {
			key = str_to_keycode(chars.items[1]);
			IF_GOTO(key == SDLK_UNKNOWN, invalid_key);
		} else {
			goto invalid_key;
		}
		DA_DESTROY(chars);

		struct app_kbd kbd = {
			.mod = mod,
			.func = func,
			.dir = dir
		};
		HT_ISET(app->kbds, (int64_t)(key + mod), kbd);
	}

	DA_DESTROY(words);

	return;
invalid_key: ;
	fprintf(stderr, "[ERROR] Invalid keybind `%s`\n",
		keybind);
	exit(EXIT_FAILURE);
}

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

#endif
