#include "villager.h"
#include "demon.h"
#include "weapons.h"
#include <printf.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct villager *init_villager(char name[50], int HP_max, struct gun *gun, int gun_mastery){
    struct villager *villager = malloc(sizeof(struct villager));
    if (villager == NULL) return NULL;

    //init des var du villager
    strcpy(villager->name, name);
    villager->medicines =  10;
    villager->distance = NEAR;
    villager->cur_HP = HP_max;
    villager->gun = gun;
    villager->gun_mastery = gun_mastery;
    villager->HP_max = HP_max;
    return villager;
}

void update_villager_hp(struct villager *villager, int amount){

    if(villager == NULL) return;
    villager->cur_HP -= amount;
    if(villager->cur_HP > villager->HP_max) villager->cur_HP = villager->HP_max;
    if (villager->cur_HP <= 0) printf("You died.\n");
}

void destroy_villager(struct villager *villager){

    if (villager == NULL) return;
    free(villager->gun);
    free(villager);
}

void shoot(struct villager *villager, struct demon *demon){

    if (villager == NULL || demon == NULL) return;
    if (villager->gun == NULL){
        printf("Equip a gun before trying to use one.\n");
        return;
    }
    
    if(villager->gun->current_magazine <= 0) {
        printf("Empty gun : You gotta reload.\n");
        return;
    }

    //villager shoot le demon
    villager->gun->current_magazine --;
    
    if((villager->distance >= demon->range) && (villager->gun->range >= demon->range)){
        
        update_demon_hp(demon, villager->gun->damage + villager->gun_mastery);
        printf("BAM! %s lost %i HP.\n",demon->name, villager->gun_mastery + villager->gun->damage);
    }
    else printf("No demon in range.\n");

}
void heal(struct villager *villager){

    if(villager == NULL) return;

    if(villager->medicines <= 0) {
        printf("No medicine left, maybe prepare some ?\n");
        return;
    }

    villager->medicines --;
    
    int gain = villager->cur_HP * 1.25;
    villager->cur_HP += gain;
    
    if (villager->cur_HP > villager->HP_max) villager->cur_HP = villager->HP_max;
    printf("Nice, you healed %i HP.\n", gain);

    return;
}

enum distance walk(struct villager *villager, int direction){

    // peut aller x<0 ou x>3
    // A voir si la mouli accepte

    if(villager == NULL) return NEAR;
/*
    if (direction >= 0){ 
        if(villager->distance == CLOSE)printf("Too close.\n");
        else{
            printf("You decided to move.\n");
            villager->distance += direction;
        }
    }
    else {
        if (villager->distance == FAR) printf("I can't run away.\n");
        else
         {
            printf("You decided to move.\n");
            villager->distance ;
        }
    }
  */
    // trop loin
    if((villager->distance == FAR) && (direction < 0)){
        printf( "I can't run away.\n");
        return villager->distance;
    }
    // trop proche
    else if((villager->distance == CLOSE) && (direction >= 0)) {
        printf("Too close.\n");
        return villager->distance;
    }
    //printf("Distance act: %d\n", villager->distance);
    villager->distance -= direction;
    //printf("Distance apres chg: %d\n",villager->distance);
    return villager->distance;    
}

int prepare_medicine(struct villager *villager){

    if (villager == NULL) return 0;
    
    //on peut pas ajouter plus, il est deja au max
    if(villager->medicines >= 10) return villager->medicines;
    
    villager->medicines ++;
    printf("Preparation ready !\n");
    
    return villager->medicines;

}
