#include "render.h"

#include <SDL_error.h>
#include <SDL_pixels.h>
#include <SDL_rect.h>
#include <SDL_render.h>
#include <SDL_surface.h>
#include <SDL_ttf.h>
#include <SDL_video.h>
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <time.h>

#include "../error.h"
#include "../game/game.h"
#include "../game/tetromino/shapes.h"
#include "../util/types.h"
#include "../util/vec.h"
#include "colour/colour32.h"
#include "colour/colour8.h"

#define COLOUR_SCORE COLOUR32_YELLOW

SDL_Renderer *rend = NULL;
TTF_Font *font = NULL;
struct gamedata const *gdat = NULL;

static SDL_Surface *score_surface = NULL;
static SDL_Texture *score_texture = NULL;

static inline i32 colpos(uint column) {
	return column * BLOCK_WIDTH + 1 + TET_PADDING;
}

static inline i32 rowpos(uint row) {
	return row * BLOCK_HEIGHT + 1 + TET_PADDING;
}

static void draw_score_text(void) {
	static u16 prev_pts = 0xFFFF; // initialise to maximum, so the code below is triggered on first run

	if (prev_pts ^ gdat->pnts) {
		char score_text[6] = "0"; // max (base 10) digits of a u16 + null terminator
		prev_pts = gdat->pnts;
		if (gdat->pnts) sprintf(score_text, "%hu0", gdat->pnts);

		SDL_FreeSurface(score_surface);
		SDL_DestroyTexture(score_texture);
		score_surface = TTF_RenderText_Solid(font, score_text, (SDL_Colour){COLOUR_SCORE.r, COLOUR_SCORE.g, COLOUR_SCORE.b, COLOUR_SCORE.a});
		score_texture = SDL_CreateTextureFromSurface(rend, score_surface);
	}

	assert(score_surface && score_texture);
	SDL_Rect text_rect = {colpos(COLUMNS + 1), rowpos(0), score_surface->w, score_surface->h};
	SDL_RenderCopy(rend, score_texture, NULL, &text_rect);
}

static inline int draw_block(SDL_Renderer *const renderer, i8vec2 pos) {
	const SDL_Rect block = {colpos(pos[VX]), rowpos(pos[VY]), BLOCK_WIDTH - 1, BLOCK_HEIGHT - 1};
	return SDL_RenderFillRect(renderer, &block);
}

static void draw_shape(const u8 id, i8vec2 pos) {
	set_colour8(rend, colour_from_id(id));
	i8vec2 bpos[4];
	shape_getblocks(id, bpos);
	draw_block(rend, pos + bpos[0]);
	draw_block(rend, pos + bpos[1]);
	draw_block(rend, pos + bpos[2]);
	draw_block(rend, pos + bpos[3]);
}

static void render_level(void) {
	for (int y = 0; y < ROWS; y++) {
		const u8 *row = gdat->rows[y];

		for (int x = 0; x < COLUMNS; x++) {
			if (row[x] != 0) {
				set_colour8(rend, row[x]);
				draw_block(rend, (i8vec2){x, y});
			}
		}
	}
}

void render_init(SDL_Window *win, struct gamedata const *game_data) {
	rend = SDL_CreateRenderer(win, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
	if (rend == NULL) fatal(ERROR_SDL_RENDERING_INIT, "Renderer failed to be created! SDL Error: %s", SDL_GetError());

	font = TTF_OpenFont("pixeldroid_botic-regular.ttf", PX_DENS);
	if (font == NULL) error("Failed to open font! TTF Error: %s", TTF_GetError());

	gdat = game_data;
}

void render_update(void) {
	set_colour32(rend, COLOUR32_BLACK);
	SDL_RenderClear(rend);
	set_colour32(rend, COLOUR32_WHITE);

	static const SDL_Rect field_size = {TET_PADDING, TET_PADDING, TET_WIDTH + 1, TET_HEIGHT + 1};
	SDL_RenderDrawRect(rend, &field_size);

	if (font) draw_score_text();

	render_level();
	draw_shape(gdat->pdat.cur, gdat->pdat.sel);
	draw_shape(gdat->pdat.nxt[gdat->pdat.idx], (i8vec2){COLUMNS + 1, 3});

	SDL_RenderPresent(rend);
}

void render_free(void) {
	assert(rend);
	SDL_DestroyRenderer(rend);
	rend = NULL;

	TTF_CloseFont(font);
	SDL_FreeSurface(score_surface);
	SDL_DestroyTexture(score_texture);
	font = NULL;
	score_surface = NULL;
	score_texture = NULL;
}
