#include <graph.h>
#include <stdio.h>
#include <stdlib.h>
#include "main.h"
#include <time.h>   


void AfficherTimerEtScore(long unsigned int *score, int minutes,int secondes)   /*Afficher le temps passé et le score*/
{   
    char buf[100];
    char buff[100]; /* Stockage du score et du timer */
    couleur text;
    text=CouleurParComposante(78, 93, 47);

	ChoisirEcran(1);
    CopierZone(2,1,0,0,930,710,0,0);

    ChoisirCouleurDessin(text);
    
	snprintf(buf,100,"TEMPS : %02d:%02d",minutes,secondes);
    snprintf(buff,100,"SCORE : %07ld",*score);
	EcrireTexte(60,675,buf,2);
    EcrireTexte(600,675,buff,2);

	CopierZone(1,0,0,0,930,710,0,0);
}

void InitialiserPastilles(PIXELS *pastilles, PIXELS *serpent, int longueur_serpent) {
    int i;
    couleur r;

    r = CouleurParNom("red");
    ChoisirCouleurDessin(r);

    srand(time(NULL));

    for (i = 0; i < PASTILLES; i++) {
        pastilles[i] = gen_pastille(serpent,pastilles,longueur_serpent);
        ChargerImage("./images/PommePastille.png",pastilles[i].x,pastilles[i].y,0,0,T_PIXEL,T_PIXEL);
    }
}

void MourrirSerpent(PIXELS *serpent, int longueur_serpent)
{
    int i = 0;
    for(i=1;i<longueur_serpent;i++)
    {
        if(serpent[0].x == serpent[i].x && serpent[0].y == serpent[i].y )
        {
            FermerGraphique();
        }
    }
    if (serpent[0].x<=0 || serpent[0].x>W_GAME || serpent[0].y<=0 || serpent[0].y>H_GAME)
    {
        FermerGraphique();
    }
}

int MangerPastille(PIXELS *serpent, PIXELS* pastilles,unsigned long *score,int longueur_serpent)
    {
        couleur r;
        int i = 0;
        r = CouleurParNom("red");
        ChoisirCouleurDessin(r);
        for(i=0;i<PASTILLES;i++)
        {
            if(serpent[0].x == pastilles[i].x && serpent[0].y == pastilles[i].y)
            {
                pastilles[i] = gen_pastille(serpent,pastilles,longueur_serpent);
                ChargerImage("./images/PommePastille.png",pastilles[i].x,pastilles[i].y,0,0,T_PIXEL,T_PIXEL);
                *score+=5;
                return 1;
            }
        }
    return 0;    
    }


/*int PastilleSurSerpent(PIXELS pastille, int *serpent, size_t longueur_serpent)
{
    int i;

    for(i=0;i<longueur_serpent;i+=2)
    {
        if (pastille.x==serpent[i] && pastille.y ==serpent[i+1])
        {  
            return 1;
        }
    }
    return 0;
}*/


void DeplacementSerpent(int direction ,PIXELS *serpent, int longueur)
{
    int tempX = serpent[0].x;
    int tempY = serpent[0].y;
    int i;
    couleur g;
    couleur j;
    ChoisirEcran(2);
    j=CouleurParNom("yellow");
    g=CouleurParComposante(171, 204, 104);


    for (i = 1; i < longueur; i++) {
        int tempX2 = serpent[i].x;
        int tempY2 = serpent[i].y;

        serpent[i].x = tempX;
        serpent[i].y = tempY;

        tempX = tempX2;
        tempY = tempY2;

        if(i == longueur-1)
        {
            ChoisirCouleurDessin(g);
            RemplirRectangle(serpent[i].x,serpent[i].y,T_PIXEL,T_PIXEL);
        }
    }

    ChoisirCouleurDessin(j);
    if(direction == 0) /* Direction vers la gauche */
    {
        serpent[0].x-=T_PIXEL;
        ChargerImage("./images/SnakePart.png",serpent[0].x,serpent[0].y,0,0,T_PIXEL,T_PIXEL);
    }
    else if(direction == 1) /* Direction vers le haut */
    {
        serpent[0].y-=T_PIXEL;
        ChargerImage("./images/SnakePart.png",serpent[0].x,serpent[0].y,0,0,T_PIXEL,T_PIXEL);

    }
    else if(direction == 2) /* Direction vers la droite */
    {
        serpent[0].x+=T_PIXEL;
        ChargerImage("./images/SnakePart.png",serpent[0].x,serpent[0].y,0,0,T_PIXEL,T_PIXEL);
    }
    else if(direction == 3) /* Direction vers le bas */
    {
        serpent[0].y+=T_PIXEL;
        ChargerImage("./images/SnakePart.png",serpent[0].x,serpent[0].y,0,0,T_PIXEL,T_PIXEL);
    }
}