#include <stdio.h>
#include "Raster.h"

/* nouvelles fonctions de Raster :
extern void RasterSetColorPixel (int x, int y, int grey);
extern int  RasterGetPixel (int x, int y);
extern char* RasterGetString (char* msg, char* dflt);
extern char RasterPaintPixels (int grey, char* keys);
extern char* RasterSaveFile (char*);
extern char* RasterLoadFile (char*);
*/

/* couleurs */
#define LIGHTGRAY	25
#define GRAY		50
#define DARKGRAY	75

/* 
 *	algorithme ameliore
 */
struct {
	int	setPixel;
	int	getPixel;
	int	nbPixels;
	int prof;
	int	maxProf;
} Stats;

void
RemplirTache (int x, int y, int c0, int c)
{
	/* **** A COMPLETER **** */
}

void
Tache (int x, int y, int couleur)
{
	int c0 = RasterGetPixel (x, y);
	
	Stats.setPixel = 0;
	Stats.getPixel = 0;
	Stats.nbPixels = 0;
	Stats.prof = 0;
	Stats.maxProf = 0;
	
	/* s'assurer que les deux couleurs sont differentes */
	if (couleur == c0) {
		couleur = (c0 == BLACK) ? GRAY : BLACK;
	}
	RemplirTache (x, y, c0, couleur);
	
	RasterMessage ("%d setPixel, %d getPixel, %d pixels, maxProf = %d. <ESPACE> pour continuer",
		Stats.setPixel, Stats.getPixel, Stats.nbPixels, Stats.maxProf);	
	RasterGetKey (" ");
//	printf ("%d setPixel, %d getPixel, %d pixels, maxProf = %d\n",
//		Stats.setPixel, Stats.getPixel, Stats.nbPixels, Stats.maxProf);	
}

/* 
 *	algorithme naif
 */
struct {
	int	setPixel;
	int	getPixel;
	int	appels;
	int prof;
	int	maxProf;
} StatsNaif;

void
RemplirTacheNaif (int x, int y, int c0, int c)
{
	StatsNaif.appels++;
	StatsNaif.prof++;
	if (StatsNaif.prof > StatsNaif.maxProf)
		StatsNaif.maxProf = StatsNaif.prof;
	
	StatsNaif.setPixel++;
	RasterSetColorPixel (x, y, c);
	
	StatsNaif.getPixel += 4;
	if (RasterGetPixel (x+1, y) == c0)
		RemplirTacheNaif (x+1, y, c0, c);
	if (RasterGetPixel (x-1, y) == c0)
		RemplirTacheNaif (x-1, y, c0, c);
	if (RasterGetPixel (x, y+1) == c0)
		RemplirTacheNaif (x, y+1, c0, c);
	if (RasterGetPixel (x, y-1) == c0)
		RemplirTacheNaif (x, y-1, c0, c);
	
	StatsNaif.prof--;
}

void
TacheNaif (int x, int y, int couleur)
{
	int c0 = RasterGetPixel (x, y);
	
	StatsNaif.setPixel = 0;
	StatsNaif.getPixel = 0;
	StatsNaif.appels = 0;
	StatsNaif.prof = 0;
	StatsNaif.maxProf = 0;
	
	/* s'assurer que les deux couleurs sont differentes */
	if (couleur == c0) {
		couleur = (c0 == BLACK) ? GRAY : BLACK;
	}
	RemplirTacheNaif (x, y, c0, couleur);

	RasterMessage ("%d setPixel, %d getPixel, %d appels, maxProf = %d. <ESPACE> pour continuer",
		StatsNaif.setPixel, StatsNaif.getPixel, StatsNaif.appels, StatsNaif.maxProf);	
	RasterGetKey (" ");
//	printf ("%d setPixel, %d getPixel, %d appels, maxProf = %d\n",
//		StatsNaif.setPixel, StatsNaif.getPixel, StatsNaif.appels, StatsNaif.maxProf);	
}

/*
 *	main program
 */

int
main ()
{
	boolean done = FALSE;
	point p;
	int color;
	boolean painting = FALSE;
	char *name = 0;
	
	RasterInit ("Tache", 100, 60, 8, GRID, SQUARE_PIXEL);
	
	while (! done) {
		char key;
		RasterMessage ("p/eindre n/aif t/ache s/auver c/harger E/ffacer q/uitter");
		RasterGetInput (&p, &key);
		switch (key) {
			case 'p':	/* peindre */
				RasterMessage ("Mode peinture. n/oir b/lanc g/ris c/lair f/once <ESPACE> pour sortir");
				color = BLACK;
				painting = TRUE;
				while (painting) {
					switch (RasterPaintPixels (color, "nbgcf ")) {
						case 'n':
							color = BLACK;
							break;
						case 'b':
							color = WHITE;
							break;
						case 'g':
							color = GRAY;
							break;
						case 'c':
							color = LIGHTGRAY;
							break;
						case 'f':
							color = DARKGRAY;
							break;
						case ' ':
							painting = FALSE;
							break;
					}
				}
				break;
			case 'n':	/* naif */
				RasterMessage ("Algorithme naif : donner le point de depart");
				p = RasterGetPoint ();
				TacheNaif (p.x, p.y, LIGHTGRAY);
				break;
			case 't':	/* tache */
				RasterMessage ("Algorithme ameliore : donner le point de depart");
				p = RasterGetPoint ();
				Tache (p.x, p.y, DARKGRAY);
				break;
			case 's':	/* sauver */
				name = RasterGetString ("Sauver sous", name);
				if (name) {
					char* msg = RasterSaveFile (name);
					if (msg) {
						RasterMessage ("%s : %s. <ESPACE> pour continuer", name, msg);
						RasterGetKey (" ");
					}
				}
				break;
			case 'c':	/* charger */
				name = RasterGetString ("Fichier a charger", name);
				if (name) {
					char* msg = RasterLoadFile (name);
					if (msg) {
						RasterMessage ("%s : %s. <ESPACE> pour continuer", name, msg);
						RasterGetKey (" ");
					}
				}
				break;
			case 'E':	/* Effacer */
				RasterClear ();
				break;
			case 'q':	/* quitter */
				done = TRUE;
				break;
			case '\0':	/* click */
				/* inverser l'etat du pixel : affiche ou efface */
				if (RasterGetPixel (p.x, p.y) == BACKGROUND)
					RasterSetColorPixel (p.x, p.y, BLACK);
				else
					RasterResetPixel (p.x, p.y);
				break;
		}
	}
	
	RasterEnd ();
}
