/*
 * Copyright (C) 2024  Tristan Riehs
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
#include "config.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include "disp.h"
#define DEFAULT_HEIGHT 200
#define DEFAULT_WIDTH (DEFAULT_HEIGHT*16/9)
#define MAX_FONTSIZE 100
struct state {
	int x;
	int y;
	int input;
	bool right;
	int ms;
};
#define STATE_COUNT 16
static struct state states[STATE_COUNT] = {0};
static int current_state = 0;
void
init(void)
{
#ifndef NDEBUG
	SetTraceLogLevel(LOG_DEBUG);
#else
	SetTraceLogLevel(LOG_ERROR);
#endif
	InitWindow(DEFAULT_WIDTH, DEFAULT_HEIGHT, "calculer");
	if (!IsWindowReady())
	{
		fprintf(stderr, "calculer: error initialiazing Raylib \
window\n");
		exit(1);
	}
	SetTargetFPS(60);
	SetWindowState(FLAG_WINDOW_RESIZABLE);
	BeginDrawing();
	ClearBackground(RAYWHITE);
	EndDrawing();
	current_state = 0;
	TraceLog(LOG_INFO, "window successfully initialized");
}
static int
min(int x, int y)
{
	if (x < y)
		return x;
	else
		return y;
}
static void
draw_calc(void)
{
	char txt[64] = {0};
	int win_width, win_height;
	int txt_width;
	int font_size;
	sprintf(txt, "%d + %d", states[current_state].x, states[current_state].y);
	win_width = GetScreenWidth();
	win_height = GetScreenHeight();
	font_size = min(win_height/3, MAX_FONTSIZE);
	txt_width = MeasureText(txt, font_size);
	TraceLog(LOG_DEBUG, "win_width = %d", win_width);
	TraceLog(LOG_DEBUG, "win_height = %d", win_height);
	TraceLog(LOG_DEBUG, "txt_width = %d", txt_width);
	TraceLog(LOG_DEBUG, "font_size = %d", font_size);
	int max_txt_width = win_width*3/4/2;
	if (txt_width > max_txt_width)
	{
		float scale = ((float) txt_width)/max_txt_width;
		font_size = font_size/scale;
		txt_width = MeasureText(txt, font_size);
	}
	DrawText(txt, (win_width/4)-(txt_width/2),
		 win_height/6, font_size, GRAY);
	TraceLog(LOG_INFO, "calulation %d + %d successfully drawn",
		 states[current_state].x, states[current_state].y);
}
static void
draw_input(void)
{
	char txt[64] = {0};
	int win_width, win_height;
	int txt_width;
	int font_size;
	sprintf(txt, "%d", states[current_state].input);
	win_width = GetScreenWidth();
	win_height = GetScreenHeight();
	font_size = min(win_height/4, 3*MAX_FONTSIZE/4);
	txt_width = MeasureText(txt, font_size);
	if (txt_width > win_width)
	{
		float scale = txt_width/win_width;
		font_size = font_size/scale;
		txt_width = MeasureText(txt, font_size);
	}
	DrawText(txt, (win_width/4)-(txt_width/2),
		 win_height*4/6, font_size, DARKGRAY);
	TraceLog(LOG_INFO, "input successfully drawn");
}
static void
draw_res(void)
{
	char txt[64] = {0};
	Color col;
	int win_width, win_height;
	int txt_width;
	int font_size;
	win_width = GetScreenWidth();
	win_height = GetScreenHeight();
	font_size = min(win_height/STATE_COUNT, MAX_FONTSIZE/2);
	txt_width = MeasureText("60 + 60 = 120 10000ms", font_size);
	if (txt_width > win_width/2)
	{
		float scale = ((float) txt_width)/(win_width/2);
		font_size = font_size/scale;
	}
	for (int i = 1; i < STATE_COUNT; i++)
	{
		int j = current_state - i;
		if (j < 0)
			j += STATE_COUNT;
		TraceLog(LOG_DEBUG, "i = %d", i);
		TraceLog(LOG_DEBUG, "j = %d", j);
		
		if (states[j].ms == 0)
			break;
		if (states[j].right)
			col = GREEN;
		else
			col = RED;
		sprintf(txt, "%d + %d = %d %dms",
			states[j].x, states[j].y,
			states[j].input, states[j].ms);
		txt_width = MeasureText(txt, font_size);
		DrawText(txt, win_width-txt_width,
			 (int) font_size*1.2*(i-1), font_size, col);
	}
}
static void
redraw(void)
{
	BeginDrawing();
	ClearBackground(RAYWHITE);
	draw_calc();
	draw_input();
	draw_res();
	EndDrawing();
}
void
display_calc(int x, int y)
{
	current_state = (current_state + 1)%STATE_COUNT;
	states[current_state].x = x;
	states[current_state].y = y;
	states[current_state].ms = 0;
	states[current_state].input = 0;
	/* BeginDrawing(); */
	/* ClearBackground(RAYWHITE); */
	/* draw_calc(); */
	/* EndDrawing(); */
	redraw();
}
static void
add_input_digit(int digit)
{
	states[current_state].input *= 10;
	states[current_state].input += digit;
	redraw();
}
static void
check_digit(int digit)
{
	if (IsKeyPressed(KEY_ZERO + digit))
	{
		add_input_digit(digit);
		TraceLog(LOG_INFO, "digit %d added to input", digit);
		TraceLog(LOG_INFO, "input is now %d", states[current_state].input);
	}
}
static void
check_digits(void)
{
	for (int i = 0; i < 10; i++)
		check_digit(i);
}
int
read_input(void)
{
	TraceLog(LOG_INFO, "waiting for user input");
	states[current_state].input = 0;
	while (!WindowShouldClose())
	{
		PollInputEvents();
		if (IsWindowResized())
			redraw();
		if (IsKeyPressed(KEY_ENTER))
			return states[current_state].input;
		if (IsKeyPressed(KEY_Q) || IsKeyPressed(KEY_A))
			return DISP_QUIT;
		if (IsKeyPressed(KEY_R) || IsKeyPressed(KEY_G))
			return DISP_RELOAD;
		if (IsKeyPressed(KEY_BACKSPACE))
		{
			states[current_state].input =
				states[current_state].input / 10;
			redraw();
		}
		check_digits();
	}
	return DISP_QUIT;
}
void
display_res(bool right, int ms)
{
	states[current_state].right = right;
	states[current_state].ms = ms;
	redraw();
}
void
destroy(void)
{
	CloseWindow();
}
#ifndef NDEBUG
void *
pre_reload(void)
{
	char *data = malloc(sizeof(states) + sizeof(current_state));
	memcpy(data, &states, sizeof(states));
	memcpy(data + sizeof(states), ¤t_state, sizeof(current_state));
	TraceLog(LOG_INFO, "pre-reload actions done");
	return data;
}
void
post_reload(void *state)
{
	char *data = state;
	current_state = *(data + sizeof(states));
	memcpy(&states, data, sizeof(states));
	free(data);
	TraceLog(LOG_INFO, "post-reload actions done");
}
#endif