#include "fossils.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//###############################################
//# CE QUI ARRIVE EST MOCHE ATTENTION LES YEUX  #
//###############################################

struct fossil *init_fossil(char *species, enum body_part body_part, size_t age, char has_wings){

    if(species == NULL) return NULL;

    struct fossil *fossil = malloc(sizeof(struct fossil));
    if(fossil == NULL)return NULL;

    int index = 0;
    while(*(species + index) != '\0') index ++;
    
    fossil->species = malloc((index + 1) * sizeof(char*));
    if(fossil == NULL){
        free(fossil);
        return NULL;
    }
    
    strcpy(fossil->species, species);
    fossil->part = body_part;
    fossil->age = age;
    fossil->has_wings = has_wings;
    
    return fossil;
}

int contain(enum body_part part, enum body_part parts[], int size){
    //cahr required_part[4] = {SKULL, TORSO, TAIL, LEGS};
    //on compte le nombre de part
    int is_in = 0;
    
    for(int i = 0; i < size; i++){
        if(part == parts[i]) is_in ++;
    }
    return is_in;
}

struct fossil *assemble_wingless(struct fossil *fossils[4]){

    if(fossils == NULL) return NULL;
    
    for(int i = 0; i < 4; i++){
        if(fossils[i] == NULL) return NULL;
    }

    char *specie = fossils[0]->species;
    int age = fossils[0]->age;
    int parts[4] = {};
    enum body_part required_part[4] = { SKULL, TORSO, TAIL, LEGS};
    
    //on test tt
    for(int i = 0; i < 4; i++){

        if(fossils[i]->has_wings != 0) return NULL;

        if(fossils[i]->species != specie && fossils[i]->age == age) return NULL;

        parts[i] = fossils[i]->part;       
    }
    
    //on test pour tt les parties
    // SI une part est en double alors on sort
    for (int part = 0; part < 4; part ++){
        if(contain(parts[part], required_part, 4) != 1) return NULL;
    }
   
    //ON FREE EN MASSE GRRR

    for(int foss = 0; foss < 4; foss ++){
        free(fossils[foss]->species);
        free(fossils[foss]);
        
        //on set a NULL
        fossils[foss] = NULL;
    } 
    
    return init_fossil(specie, COMPLETE, age, 0);
}

struct fossil *assemble_with_wings(struct fossil *fossils[5]){

    size_t age = fossils[0]->age;
    char* specie = fossils[0]->species;
   
    enum body_part required_part[5] = {SKULL, TORSO, TAIL, LEGS, WINGS};
    enum body_part parts[5] = {};

    for (int i = 0; i < 5; i++){

        if(fossils[i]->has_wings != 0) return NULL;

        if(fossils[i]->species != specie && fossils[i]->age != age) return NULL; 
        
        parts[i] = fossils[i]->part;    
    }
  
    for(int part = 0; part < 5; part ++){
            if(contain(parts[part], required_part, 5) != 1) return NULL;
    }

    // ON FREE EN MASSE ENCORE

    for(int foss = 0; foss < 5; foss++){
    
        free(fossils[foss]->species);
        free(fossils[foss]);
        fossils[foss] = NULL;
    }

    return init_fossil(specie, COMPLETE, age, 1);
}

struct fossil **assemble_all_fossils(struct fossil **fossils, size_t nb_fossils){

    
}
