#include "../header.h"



void    init_play(t_vie *vie, int size_tab)
{
    (*vie).totale = (size_tab * size_tab) / 2;
    (*vie).restante = (*vie).totale;
}


int     play_tours(char **labyrinthe, t_vie *vie, t_point *position, int *fd, char **affichage, int type_affichage)
{
    char dir;
    
    ft_clear();
    ftruncate(fd[2], 0);
    lseek(fd[2], 0, SEEK_SET);
    ftruncate(fd[5], 0);
    lseek(fd[5], 0, SEEK_SET);

    dprintf(fd[2], "%d\n", position->line);
    dprintf(fd[2], "%d\n", position->col);
    dprintf(fd[5], "%d\n", vie->restante);
    dprintf(fd[5], "%d\n", vie->totale);
    
    init_affichage_map(type_affichage, affichage, labyrinthe, *position, fd);
    affichage_infos(*vie);
    
    dir = demander_direction();
    
    if (dir == 'z')
    {
        if (((*position).line == 1) || (labyrinthe[(*position).line - 1][(*position).col] == MUR_EXT) || (labyrinthe[(*position).line - 1][(*position).col] == ENTREE))
        {
            printf("Vous ne pouvez pas vous déplacer ici\n");
            return (play_tours(labyrinthe, vie, position, fd, affichage, type_affichage));
        }
        else
            (*position).line = (*position).line - 2;
    }
    else if (dir == 'q')
    {
        if (((*position).col == 1) || (labyrinthe[(*position).line][(*position).col - 1] == MUR_EXT))
        {
            printf("Vous ne pouvez pas vous déplacer ici\n");
            return (play_tours(labyrinthe, vie, position, fd, affichage, type_affichage));
        }
        else
            (*position).col = (*position).col - 2;

    }
    else if (dir == 'd')
    {
        if (labyrinthe[(*position).line][(*position).col + 1] == MUR_EXT)
        {
            printf("Vous ne pouvez pas vous déplacer ici\n");
            return (play_tours(labyrinthe, vie, position, fd, affichage, type_affichage));
        }
        else
            (*position).col = (*position).col + 2;
    }
    else if (dir == 's')
    {
        if (labyrinthe[(*position).line + 1][(*position).col] == MUR_EXT)
        {
            printf("Vous ne pouvez pas vous déplacer ici\n");
            return (play_tours(labyrinthe, vie, position, fd, affichage, type_affichage));
        }
        else
            (*position).line = (*position).line + 2;
    }
    (*vie).restante--;
    
    if (labyrinthe[(*position).line][(*position).col] == BONUS_MAP)
    {
        labyrinthe[(*position).line][(*position).col] = SP;
        ftruncate(fd[0], 0);
        lseek(fd[0], 0, SEEK_SET);
        print_tab(labyrinthe, 0, fd[0]);
        if (type_affichage != 2)
            return (1);
    }
    if (labyrinthe[(*position).line][(*position).col] == BONUS_TEMPS)
    {
        labyrinthe[(*position).line][(*position).col] = SP;
        ftruncate(fd[0], 0);
        lseek(fd[0], 0, SEEK_SET);
        print_tab(labyrinthe, 0, fd[0]);
        vie->restante = vie->restante + 10;
    }
    if (labyrinthe[(*position).line][(*position).col] == SORTIE)
        return (0);
    if ((*vie).restante == 0)
        return (-1);
    ft_clear();
    return(play_tours(labyrinthe, vie, position, fd, affichage, type_affichage));
}

int     play(char **labyrinthe, int size_tab, int *fd, char **affichage, t_point position, t_vie vie)
{
    int     current;
    
    if (affichage == NULL)
        current = play_tours(labyrinthe, &vie, &position, fd, affichage, 1);

    if ((affichage != NULL) || (current == 1))
    {
        if (affichage == NULL)
            affichage = map_fil(size_tab, labyrinthe);
        labyrinthe[position.line][position.col] = SP;
        current = play_tours(labyrinthe, &vie, &position, fd, affichage, 2);
    }
    if (affichage != NULL)
    {
        free_2d((void **)affichage, 0, size_tab);
        free(affichage);
    }
    if (current == -1)
    {
        printf("Game Over !\n");
        return (-1);
    }
    return (0);
}