#include "affichage.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

GdkGC * gc1, * gc2, * gc3, * fond_transparent;

#ifndef M_PI
# define M_PI    3.14159265358979323846
# define M_PI_2  1.57079632679489661923
# define M_PI_4  0.78539816339744830962
# define M_PI_8  0.3926990816987242
#endif

void affiche_pixmap(Info * info, GdkPixmap * pixmap, GtkWidget* dessin)
{
        int i;
        int taille_plouc_x;
        int taille_plouc_y;
        GdkPixbuf * image_plouc_mange;
        GdkPixbuf * image_plouc_touche;
        GdkPixbuf * image_gomme;
        GdkPixbuf * image_monstres;
        GdkPixbuf * image_monstres_bloc;
        GdkPixbuf * image_plouc;
        GdkPixbuf * image_splash;
        GdkPixbuf * image_yeah;
        /* variable pour calculer la gomme la plus proche du ploucman*/
        int i_gomme=0;
        int i_monstre=0;
        float tmpx;
        float tmpy;
        double longueur;
        double longueurfinale;
        Perso * anim;

        /* initilisation des variables pour l'animation */
        if (info->perso == NULL)
        {
                info->perso = malloc(sizeof(anim));
                ((Perso*)info->perso)->num_image[0] = 0;
                ((Perso*)info->perso)->num_image[1] = 0;
                ((Perso*)info->perso)->image = (void **) malloc((sizeof(GdkPixbuf *))*16);
                ((Perso*)info->perso)->image[0] = gdk_pixbuf_new_from_file ("./plouc_man_anim/cool.png", NULL);
                ((Perso*)info->perso)->image[1] = gdk_pixbuf_new_from_file ("./plouc_gommes/gommes.png", NULL);
                ((Perso*)info->perso)->image[2] = gdk_pixbuf_new_from_file ("./plouc_monstres/monstres.png", NULL);
                ((Perso*)info->perso)->image[3] = gdk_pixbuf_new_from_file ("./plouc_background/fond.jpg", NULL);
                ((Perso*)info->perso)->image[4] = gdk_pixbuf_new_from_file ("./plouc_man_anim/plouc-gauche.png", NULL);
                ((Perso*)info->perso)->image[5] = gdk_pixbuf_new_from_file ("./plouc_man_anim/plouc-haut-gauche.png", NULL);
                ((Perso*)info->perso)->image[6] = gdk_pixbuf_new_from_file ("./plouc_man_anim/plouc-haut.png", NULL);
                ((Perso*)info->perso)->image[7] = gdk_pixbuf_new_from_file ("./plouc_man_anim/plouc-haut-droite.png", NULL);
                ((Perso*)info->perso)->image[8] = gdk_pixbuf_new_from_file ("./plouc_man_anim/plouc-droite.png", NULL);
                ((Perso*)info->perso)->image[9] = gdk_pixbuf_new_from_file ("./plouc_man_anim/plouc-bas-droite.png", NULL);
                ((Perso*)info->perso)->image[10] = gdk_pixbuf_new_from_file ("./plouc_man_anim/plouc-bas.png", NULL);
                ((Perso*)info->perso)->image[11] = gdk_pixbuf_new_from_file ("./plouc_man_anim/plouc-bas-gauche.png", NULL);
                ((Perso*)info->perso)->image[12] = gdk_pixbuf_new_from_file ("./plouc_man_anim/touche_par_monstre_autre.png", NULL);
                ((Perso*)info->perso)->image[13] = gdk_pixbuf_new_from_file ("./plouc_man_anim/splash.png", NULL);
                ((Perso*)info->perso)->image[14] = gdk_pixbuf_new_from_file ("./plouc_man_anim/yeah.png", NULL);
                ((Perso*)info->perso)->image[15] = gdk_pixbuf_new_from_file ("./plouc_monstres/monstres_bloc.png", NULL);
        }
        /* raccourci pratique */
        anim = (Perso*) info->perso;

        /* prechargement des images */
        image_plouc_mange  = (GdkPixbuf *) (anim->image[0] );
        image_plouc_touche  = (GdkPixbuf *) (anim->image[12] );
        image_gomme = (GdkPixbuf *) (anim->image[1]);
        image_monstres = (GdkPixbuf *) (anim->image[2]);
        image_monstres_bloc = (GdkPixbuf *) (anim->image[15]);
        image_splash = (GdkPixbuf *) (anim->image[13]);
        image_yeah = (GdkPixbuf *) (anim->image[14]);

/**************************************************************************************************/
/*Calcul de la gomme la plus proche pour changer l'image du ploucman et calcul du monstre le plus */^M
/**************************************************************************************************/

        longueurfinale = (double)(info->xmax*sqrt(2));
        /* on cherche parmis le tableau de positions des gommes celle qui est la plus proche */
        for(i=0;i<info->n_cibles;i++)
        {
                if(info->cibles[i] == 0)
                {
                        tmpx = (double)(info->position.x - info->pos_cibles[i].x);
                        tmpy = (double)(info->position.y - info->pos_cibles[i].y);
                        longueur = sqrt((tmpx)*(tmpx) + (tmpy)*(tmpy));


                        if(longueur < longueurfinale)
                        {
                                longueurfinale = longueur;
                                i_gomme=i;
                        }
                }
        }


        longueurfinale = (double)(info->xmax*sqrt(2));
        /* on cherche parmis le tableau de positions des monstres celui qui est le plus proche*/
        for(i=0;i<info->n_monstres;i++)
        {
                tmpx = (double)(info->position.x - info->pos_monstres[i].x);
                tmpy = (double)(info->position.y - info->pos_monstres[i].y);
                longueur = sqrt((tmpx)*(tmpx) + (tmpy)*(tmpy));

                if(longueur < longueurfinale)
                {
                        longueurfinale = longueur;
                        i_monstre=i;
                }
        }

/****************************/
/*Animation des plouc_gommes*/^M
/****************************/
        (anim->num_image[0])++;
        if ((anim->num_image[0])%78==0)
                (anim->num_image[0])=0;

/************************/
/*Animation du plouc_man*/
/************************/

        /* taille par défaut */
        taille_plouc_x=40;
        taille_plouc_y=38;

        if (anim->old >= -M_PI_8 && anim->old <= M_PI_8)
        {
                /* direction gauche */
                image_plouc = (GdkPixbuf *) (anim->image[4] );
        }
        else if (anim->old > M_PI_8 && anim->old <= 3*M_PI_8)
        {
                /* direction haut-gauche */
                image_plouc = (GdkPixbuf *) (anim->image[5] );
                taille_plouc_x=49;
                taille_plouc_y=47;
        }
        else if (anim->old > 3*M_PI_8 && anim->old <= 5*M_PI_8)
        {
                /* direction haut */
                image_plouc = (GdkPixbuf *) (anim->image[6] );
        }
        else if (anim->old > 5*M_PI_8 && anim->old <= 7*M_PI_8)
        {
                /* direction haut-droite */
                image_plouc = (GdkPixbuf *) (anim->image[7] );
                taille_plouc_x=49;
                taille_plouc_y=47;
        }
        else if (anim->old > 7*M_PI_8 && anim->old <= -7*M_PI_8)
        {
                /* direction droite */
                image_plouc = (GdkPixbuf *) (anim->image[8] );
        }
        else if (anim->old > -7*M_PI_8 && anim->old <= -5*M_PI_8)
        {
                /* direction bas-droite */
                image_plouc = (GdkPixbuf *) (anim->image[9] );
                taille_plouc_x=48;
                taille_plouc_y=45;
        }
        else if (anim->old > -5*M_PI_8 && anim->old <= -3*M_PI_8)
        {
                /* direction bas */
                image_plouc = (GdkPixbuf *) (anim->image[10] );
        }
        else if (anim->old > -3*M_PI_8 && anim->old < -M_PI_8)
        {
                /* direction bas-gauche */
                image_plouc = (GdkPixbuf *) (anim->image[11] );
                taille_plouc_x=48;
                taille_plouc_y=45;
        }
        else
        {
                /* direction droite */
                image_plouc = (GdkPixbuf *) (anim->image[8] );
        }

/*********************************************************/
/*Animation du plouc_man et chargement de l'image adéquat*/
/*********************************************************/
        (anim->num_image[1])++;
        if ((anim->num_image[1])%12==0)
                (anim->num_image[1])=0;

/*********************************/
/*      Affichage basique        */
/* efface tout et réaffiche tout */
/*********************************/

        if (gc1 && gc2 && gc3)
        {

        affichage_background(info,pixmap,dessin);

        for (i=0;i<info->n_cibles;i++)
        {
                /* Affichage de la gomme si elle n'a pas encore été mangé */
                if (info->cibles[i] == 0)
                {
                                gdk_draw_pixbuf(pixmap,
                                             gc3,
                                             image_gomme,
                                             30*((anim->num_image[0])/3),
                                             0,
                                             info->pos_cibles[i].x-15,
                                             info->pos_cibles[i].y-13,
                                             30,
                                             27,
                                             GDK_RGB_DITHER_MAX,
                                             0,
                                             0);
                }
        }

        /* si le plouc se fait toucher par un monstre */
        if ((fabs(info->pos_monstres[i_monstre].x - info->position.x)<=5) && (fabs(info->pos_monstres[i_monstre].y - info->position.y)<=5))
        {
                gdk_draw_pixbuf(pixmap,
                                gc3,
                                image_splash,
                                350*(((anim->num_image[1])/3)%2),
                                0,
                                75,
                                175,
                                350,
                                150,
                                GDK_RGB_DITHER_MAX,
                                0,
                                0);

                gdk_draw_pixbuf(pixmap,
                                gc3,
                                image_plouc_touche,
                                24*(((anim->num_image[1])/3)%2),
                                0,
                                info->position.x-7,
                                info->position.y-7,
                                24,
                                28,
                                GDK_RGB_DITHER_MAX,
                                0,
                                0);
        }
        /* si le plouc est près d'une gomme on affiche l'image ou il prend de l'énergie */
        else if ((fabs(info->pos_cibles[i_gomme].x - info->position.x)<=20) && (fabs(info->pos_cibles[i_gomme].y - info->position.y)<=20))
        {
                /* le ploucman prend de l'ernergie (il mange une gomme ou passe près d'une gomme */
                gdk_draw_pixbuf(pixmap,
                                gc3,
                                image_yeah,
                                333*(((anim->num_image[1])/3)%2),
                                0,
                                83,
                                175,
                                333,
                                116,
                                GDK_RGB_DITHER_MAX,
                                0,
                                0);

                gdk_draw_pixbuf(pixmap,
                                gc3,
                                image_plouc_mange,
                                31*(((anim->num_image[1])/3)%2),
                                0,
                                info->position.x-7,
                                info->position.y-7,
                                31,
                                35,
                                GDK_RGB_DITHER_MAX,
                                0,
                                0);
        }
        else
        {
                                /* affichage normal quand il marche trankill du PloucMan */
                                gdk_draw_pixbuf(pixmap,
                                             gc3,
                                             image_plouc,
                                             20*((anim->num_image[1])/3),
                                             0,
                                             info->position.x-7,
                                             info->position.y-7,
                                             20,
                                             30,
                                             GDK_RGB_DITHER_MAX,
                                             0,
                                             0);
        }

        /* Affichage des monstres */
        for (i=0;i<info->n_monstres;i++)
        {
                /* affichage des monstres méchant qui sont bloqués dans la zone centrale */
                if (i%4 == 0 || (i-1)%4==0)
                {
                gdk_draw_pixbuf(pixmap,
                                gc3,
                                image_monstres_bloc,
                                15*(((anim->num_image[1])/3)%2),
                                0,
                                info->pos_monstres[i].x-2,
                                info->pos_monstres[i].y-2,
                                15,
                                13,
                                GDK_RGB_DITHER_MAX,
                                0,
                                0);
                }
                /* affichage des autres mmonstres */
                else
                {
                gdk_draw_pixbuf(pixmap,
                                gc3,
                                image_monstres,
                                15*(((anim->num_image[1])/3)%2),
                                0,
                                info->pos_monstres[i].x-2,
                                info->pos_monstres[i].y-2,
                                15,
                                13,
                                GDK_RGB_DITHER_MAX,
                                0,
                                0);
                }

        }

        }

}

GdkGC * new_color(gint r,gint v,gint b, GtkWidget * widget)
{

        GdkColor color;
        GdkGC * gc;
        gc = gdk_gc_new (widget->window);
        color.red = r;
        color.green = v;
        color.blue = b;
        gdk_gc_set_rgb_fg_color (gc, &color);
        return gc;
}


void init_affichage(GtkWidget* dessin)
{

        gc1 = new_color(50000,0,0,dessin);
        gc2 = new_color(50000,30000,10000,dessin);
        gc3 = new_color(0,0,0,dessin);
}

/* fonction pour afficher le fond d'écran */
void affichage_background(Info * info,GdkPixmap * pixmap,GtkWidget* dessin)
{
        int i,j;
        GdkPixbuf *image_fond;

        image_fond = (GdkPixbuf *) (((Perso*)info->perso)->image[3] );

        for (i=0;i*100<(info->xmax);i++)
        {
                for (j=0;j*100<(info->ymax);j++)
                {
                        gdk_draw_pixbuf(pixmap,
                                gc3,
                                image_fond,
                                0,
                                0,
                                i*100,
                                j*100,
                                100,
                                100,
                                GDK_RGB_DITHER_MAX,
                                0,
                                0);
                }
        }
}