#include "entetes.h"

//******
//Fonction affichageGrille
//Elle est appelée de façon systématique lors d'un clic (elle rafraîchie les images)
//******

void affichageGrille(Total *ptotal){ 
 int i,j;
 for(i=0;i<N;i++){
 	for(j=0;j<N;j++){
	int n = case2button(i,j);
 		if(ptotal->grille.tab[i][j].etatMine==DECOUVERT){
			gtk_widget_set_sensitive(GTK_WIDGET(ptotal->pbtn[n]), FALSE);
			if(ptotal->grille.tab[i][j].nbMinesVoisines==0){
				int n = case2button(i,j);
				GtkWidget *pImageDecouvert;
				pImageDecouvert = gtk_image_new_from_file("./obj/decouvert.png");
				gtk_button_set_image(GTK_BUTTON(ptotal->pbtn[n]), pImageDecouvert);
			}
			if(ptotal->grille.tab[i][j].nbMinesVoisines==1){
				GtkWidget *pImageDecouvert;
				pImageDecouvert = gtk_image_new_from_file("./obj/1.png");
				gtk_button_set_image(GTK_BUTTON(ptotal->pbtn[n]), pImageDecouvert);
			}
			if(ptotal->grille.tab[i][j].nbMinesVoisines==2){
				GtkWidget *pImageDecouvert;
				pImageDecouvert = gtk_image_new_from_file("./obj/2.png");
				gtk_button_set_image(GTK_BUTTON(ptotal->pbtn[n]), pImageDecouvert);
			}
			if(ptotal->grille.tab[i][j].nbMinesVoisines==3){
				GtkWidget *pImageDecouvert;
				pImageDecouvert = gtk_image_new_from_file("./obj/3.png");
				gtk_button_set_image(GTK_BUTTON(ptotal->pbtn[n]), pImageDecouvert);
			}
			if(ptotal->grille.tab[i][j].nbMinesVoisines==4){
				GtkWidget *pImageDecouvert;
				pImageDecouvert = gtk_image_new_from_file("./obj/4.png");
				gtk_button_set_image(GTK_BUTTON(ptotal->pbtn[n]), pImageDecouvert);
			}
			if(ptotal->grille.tab[i][j].nbMinesVoisines==5){
				GtkWidget *pImageDecouvert;
				pImageDecouvert = gtk_image_new_from_file("./obj/5.png");
				gtk_button_set_image(GTK_BUTTON(ptotal->pbtn[n]), pImageDecouvert);
			}
			if(ptotal->grille.tab[i][j].nbMinesVoisines==6){
				GtkWidget *pImageDecouvert;
				pImageDecouvert = gtk_image_new_from_file("./obj/6.png");
				gtk_button_set_image(GTK_BUTTON(ptotal->pbtn[n]), pImageDecouvert);
			}
			if(ptotal->grille.tab[i][j].nbMinesVoisines==7){
				GtkWidget *pImageDecouvert;
				pImageDecouvert = gtk_image_new_from_file("./obj/7.png");
				gtk_button_set_image(GTK_BUTTON(ptotal->pbtn[n]), pImageDecouvert);
			}
			if(ptotal->grille.tab[i][j].nbMinesVoisines==8){
				GtkWidget *pImageDecouvert;
				pImageDecouvert = gtk_image_new_from_file("./obj/8.png");
				gtk_button_set_image(GTK_BUTTON(ptotal->pbtn[n]), pImageDecouvert);
			}
			if(ptotal->grille.tab[i][j].nbMinesVoisines==9){
				GtkWidget *pImageDecouvert;
				pImageDecouvert = gtk_image_new_from_file("./obj/bombe.png");
				gtk_button_set_image(GTK_BUTTON(ptotal->pbtn[n]), pImageDecouvert);
			}		}
		if(ptotal->grille.tab[i][j].etatMine==CACHE){
			GtkWidget *pImageCache;
			pImageCache = gtk_image_new_from_file("./obj/cache.png");
			gtk_button_set_image(GTK_BUTTON(ptotal->pbtn[n]), pImageCache);
		}
		if(ptotal->grille.tab[i][j].etatMine==MARQUE){
			GtkWidget *pImageFlag;
			pImageFlag = gtk_image_new_from_file("./obj/flag.png");
			gtk_button_set_image(GTK_BUTTON(ptotal->pbtn[n]), pImageFlag);
		}
	}
 }

if(ptotal->grille.jeu.isOver==1){	/* si le jeu est terminé, on désactive tous les boutons */
	for(i=0;i<N;i++){
     		for(j=0;j<N;j++){
            		int n = case2button(i,j);
            		gtk_widget_set_sensitive(GTK_WIDGET(ptotal->pbtn[n]), FALSE);
		}	
	}
	if(ptotal->grille.jeu.isWinner==0){	/* si le joueur a perdu */	
		gtk_label_set_label(GTK_LABEL(ptotal->pLabelFin),"Vous avez perdu ! Dommage !");
	}
	if(ptotal->grille.jeu.isWinner==1){	/* si le joueur a gagné */
		int score=time(0)-ptotal->grille.jeu.startTime;	/* le score est le temps de fin moins le temps de début */
		gchar message[255]; /* déclaration de la chaîne à afficher */
 		sprintf(message, "Vous avez gagné ! Bravo ! \n Votre score est de %i s", score);
		gtk_label_set_label(GTK_LABEL(ptotal->pLabelFin), message); /*update du label */
	}	

  }

}

