#ifndef _MESSAGE_C
#define _MESSAGE_C

#include "message.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
	int col;
	char *msg;
} message;

static message messages[MSG_MAX] = {
	[MSG_CHECK] = {.col = 0xFF00, .msg = "CHECKPOINT!"},
	[MSG_LAP] = {.col = 0xFF0000, .msg = "LAP COMPLETE!"},
	[MSG_COMPLETE] = {.col = 0xFF, .msg = "RACE COMPLETE!"},
	[MSG_LASTLAP] = {.col = 0xFF, .msg = "FINAL LAP"},
	[MSG_3] = {.col = 0xFF, .msg = "3"},
	[MSG_2] = {.col = 0xFF, .msg = "2"},
	[MSG_1] = {.col = 0xFFFF, .msg = "1"},
	[MSG_GO] = {.col = 0xFF00, .msg = "GO!"},
};

gp_font *font_white,*font_red,*font_green,*font_yellow,*font_blue;

static gp_font_def font_def = {
	.width = 12,
	.flags = GP_FONT_UP + GP_FONT_DOWN + GP_FONT_LAST,
	.id = (int) "<Micron$Dir>.font",
	.load = gp_loadfont_paint
};

int init_messages()
{
	/* Load font */
	font_white = gp_loadfont(&font_def,12,0xEEEEEE,-1,0);
	if (font_white == 0)
	{
		printf("Error loading font\n");
		return 1;
	}
	font_red = gp_loadfont(&font_def,12,0xFF,-1,0);
	font_green = gp_loadfont(&font_def,12,0xFF00,-1,0);
	font_blue = gp_loadfont(&font_def,12,0xFF0000,-1,0);
	font_yellow = gp_loadfont(&font_def,12,0xFFFF,-1,0);
	return 0;
}

void plotmessage(int m,gp_screen *scr)
{
	int x,y,i;
	gp_font *f;
	if ((m < 0) || (m >= MSG_MAX))
		return;
	i = strlen(messages[m].msg);
	f = gp_loadfont(&font_def,12,messages[m].col,-1,0);
	x = scr->width-i*f->width;
	y = scr->height-f->height;
	gp_font_puts(f,messages[m].msg,i,scr,x/2,y/4);
	gp_freefont(f);
}

#endif
