#include <stdio.h>
#include <time.h>
#include <allegro.h>

bool Score (int time);
void Quit ();

void Win (int time) {
	
	int sec = time % 60;
	int min = (time - sec) / 60;
	
	char text [100];
	sprintf(text, "in %d minutes and %d seconds", min, sec);
	
	printf("%s", text);
	
	bool best = Score(time);
	
	clear_to_color(screen, makecol(0, 0, 255));
	
	textout_centre_ex(screen, font, "CONGRATULATIONS", SCREEN_W / 2, SCREEN_H / 2, makecol(255, 255, 255), makecol(0, 0, 255));
	textout_centre_ex(screen, font, "You have finished ChuckMarine", SCREEN_W / 2, SCREEN_H / 2 + 15, makecol(255, 255, 255), makecol(0, 0, 255));
	textout_centre_ex(screen, font, text, SCREEN_W / 2, SCREEN_H / 2 + 25, makecol(255, 255, 255), makecol(0, 0, 255));
	if (best) textout_centre_ex(screen, font, "NEW HIGHSCORE", SCREEN_W / 2, SCREEN_H / 2 + 45, makecol(255, 255, 255), makecol(0, 0, 255));
	textout_centre_ex(screen, font, "Press [Q] to quit", SCREEN_W / 2, SCREEN_H / 2 + 65, makecol(255, 255, 255), makecol(0, 0, 255));		
	
	while (true) {
		
		if (key[KEY_Q]) {
			
			Quit();
			break;
			
		}
		
	}
	
}

bool Score (int time) {
	
	FILE* fp;
	
	fp = fopen("cfg/score.cfg", "r");
	int score;
	fscanf(fp, "%d", &score);
	fclose(fp);
	
	if (score < time) {
		
		printf("better, rewrite\n");
		
		fp = fopen("cfg/score.cfg", "w");
		fprintf(fp, "%d", time);
		fclose(fp);
		
		return 1;
		
	}
	
	return 0;
	
}

void Quit () {
	
	printf("quit\n");
	
}

int main () {
	
	allegro_init();
	install_keyboard();
	set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 1024, 768, 0, 0);
	
	srandom(time(NULL));
	Win(random() % 5000);
	
}
END_OF_MAIN();
