//#include <stdio.h>

//#include "demon.h"
//#include "villager.h"
//#include "weapons.h"

//int main()
//{
    /*
    char gun_name[50] = "BFG";
    struct gun *gun = init_gun(gun_name, 50, FAR, 16);

    printf("Gun name: %s\nDamage: %d\nRange: %d\nMagazine: %d/%d\n", gun->name, gun->damage, gun->range, gun->current_magazine, gun->magazine_size);


    //char gun_name[50] = "BFG";
    //struct gun *gun = init_gun(gun_name, 50, FAR, 16);

    gun->current_magazine = 2;
    printf("Before reload: %d\n", gun->current_magazine);
    reload(gun);
    printf("After reload: %d\n", gun->current_magazine);
    
    printf("villager\n");
    char name[50] = "Isabelle";
    //char gun_name[50] = "BFG";
    gun = init_gun(gun_name, 50, FAR, 16);
    struct villager *villager = init_villager(name, 50, gun, 1);
    update_villager_hp(villager, 100);

    char name[50] = "Isabelle";
    char gun_name[50] = "BFG";
    struct gun *gun = init_gun(gun_name, 50, CLOSE, 16);
    struct villager *villager = init_villager(name, 50, gun, 1);
    pp_villager(villager);
    destroy_villager(villager);
*/
 /*   char demon_name[50] = "Cyberdemon";
    struct demon *demon = init_demon(NIGHTMARE, demon_name, 40, 5, CLOSE);
    update_demon_hp(demon, 200);
*/
   /* char name[50] = "Isabelle";
    char gun_name[50] = "BFG";
    struct gun *gun = init_gun(gun_name, 50, FAR, 16);
    struct villager *villager =
    init_villager(name, 50, gun, 1);

    // Init demon
    char demon_name[50] = "Cyberdemon";
    struct demon *demon = init_demon(NIGHTMARE, demon_name, 40, 5, CLOSE);
    // // Shoot
    shoot(villager, demon);
    pp_villager(villager);
    pp_demon(demon);
    destroy_demon(demon);
    destroy_villager(villager);
*/
/*    char name[50] = "Isabelle";
    char gun_name[50] = "BFG";
    struct gun *gun = init_gun(gun_name, 50, FAR, 16);
    struct villager *villager =
    init_villager(name, 50, gun, 1);

    // Heal
    villager->cur_HP -= 40;
    pp_villager(villager);
    putchar('\n');
    heal(villager);
    pp_villager(villager);
    destroy_villager(villager);
*/
/*    char name[50] = "Isabelle";
    char gun_name[50] = "BFG";
    struct gun *gun = init_gun(gun_name, 50, FAR, 16);
    struct villager *villager =
    init_villager(name, 50, gun, 1);

// Walk
    pp_villager(villager);
    putchar('\n');
    walk(villager, 50);
    pp_villager(villager);
    destroy_villager(villager);
*/
    /*
    // Init Villager
    char name[50] = "Isabelle";
    char gun_name[50] = "BFG";
    struct gun *gun = init_gun(gun_name, 50, FAR, 16);

    struct villager *villager =
    init_villager(name, 50, gun, 1);

// Prepare Medicine
    villager->medicines -= 5;
    pp_villager(villager);
    prepare_medicine(villager);
    pp_villager(villager);
    destroy_villager(villager);
*/
    /*
    // Init Villager
    char name[50] = "Isabelle";
    char gun_name[50] = "BFG";
    struct gun *gun = init_gun(gun_name, 50, FAR, 16);
    struct villager *villager =
    init_villager(name, 50, gun, 1);

// Init demon
    char demon_name[50] = "Cyberdemon";
    struct demon *demon = init_demon(NIGHTMARE, demon_name, 40, 5, CLOSE);

// Chase
    pp_villager(villager);
    chase(demon, villager);
    pp_villager(villager);
    destroy_villager(villager);
    destroy_demon(demon);
*/
    // Init Villager
   /* char name[50] = "Isabelle";
    char gun_name[50] = "BFG";
    struct gun *gun = init_gun(gun_name, 50, FAR, 16);

    struct villager *villager =
    init_villager(name, 50, gun, 1);

// Init demon
    char demon_name[50] = "Cyberdemon";
    struct demon *demon = init_demon(NIGHTMARE, demon_name, 40, 5, FAR);

// Basic attack
    basic_attack(demon, villager);
    pp_villager(villager);
    destroy_villager(villager);
    destroy_demon(demon);
*/
    // Init Villager
 /*   char name[50] = "Isabelle";
    char gun_name[50] = "BFG";

    struct gun *gun = init_gun(gun_name, 50, FAR, 16);

    struct villager *villager =
    init_villager(name, 50, gun, 1);

// Init demon
    char demon_name[50] = "Cyberdemon";
    struct demon *demon = init_demon(NIGHTMARE, demon_name, 40, 5, FAR);

//    pp_villager(villager);
//    pp_demon(demon);



// Draining attack
    demon->cur_HP -= 10;
    draining_attack(demon, villager);
    pp_villager(villager);
    pp_demon(demon);
    destroy_demon(demon);
    destroy_villager(villager);
*/
    // Init Villager
   /* char name[50] = "Isabelle";
    char gun_name[50] = "BFG";

    struct gun *gun = init_gun(gun_name, 50, FAR, 16);

    struct villager *villager =
    init_villager(name, 50, gun, 1);

// Init demon
    char demon_name[50] = "Cyberdemon";
    struct demon *demon = init_demon(NIGHTMARE, demon_name, 40, 5, FAR);

// Heavy attack
    heavy_attack(demon, villager);
    pp_villager(villager);
    pp_demon(demon);
    destroy_demon(demon);
    destroy_villager(villager);
*/
  /*  // Init Villager
    char name[50] = "Isabelle";
    char gun_name[50] = "BFG";
    struct gun *gun = init_gun(gun_name, 50, FAR, 16);
    struct villager *villager =
    init_villager(name, 500, gun, 1);

// Init demon
    char demon_name[50] = "Cyberdemon";
    struct demon *demon = init_demon(NIGHTMARE, demon_name, 40, 5, CLOSE);

// Demon action
    demon_action(demon, villager); // Distance = Near but its range is close
    demon_action(demon, villager); // In range and high HP
    demon->cur_HP = demon->HP_max / 2;
    demon_action(demon, villager); // In range and mid HP
    demon->cur_HP = 1;
    demon_action(demon, villager); // In range and low HP

    destroy_villager(villager);
    destroy_demon(demon);

}*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "demon.h"
#include "villager.h"
#include "weapons.h"

#define PURPLE(string) "\x1b[35m" string "\x1b[0m"

#define MAX_BUFFER 1024

void villager_action(struct villager *villager, struct demon *demon, char *command)
{
    if (!strcmp(command, "a"))
    {
        shoot(villager, demon);
    }
    else if (!strcmp(command, "r"))
    {
        printf("You reload your gun");
        reload(villager->gun);
    }
    else if (!strcmp(command, "h"))
    {
        heal(villager);
    }
    else if (!strcmp(command, "z+"))
    {
        walk(villager, 1);
    }
    else if (!strcmp(command, "s-"))
    {
        walk(villager, -1);
    }
    else if (!strcmp(command, "p"))
    {
        prepare_medicine(villager);
    }
    else
        printf(PURPLE("No such action\n"));
}

static void end_fight(struct villager *villager, struct demon *demon)
{
    if (villager->cur_HP <= 0 || demon->cur_HP <= 0)
    {
        pp_villager(villager);
        pp_demon(demon);
        destroy_villager(villager);
        destroy_demon(demon);
        exit(0);
    }
}

int main()
{
    printf("\033[2J\033[1;1H");
    // Init gun and villager
    char name[50] = "Isabelle";
    char gun_name[50] = "BFG";
    struct gun *gun = init_gun(gun_name, 50, FAR, 16);
    struct villager *villager =
        init_villager(name, 50, gun, 1);

    // Init demon
    char demon_name[50] = "Cyberdemon";
    struct demon *demon = init_demon(NIGHTMARE, demon_name, 40, 5, CLOSE);
    pp_villager(villager);
    pp_demon(demon);

    char input[MAX_BUFFER];

    while (fgets(input, MAX_BUFFER, stdin) != NULL)
    {
        printf("\033[2J\033[1;1H");
        int i = 0;
        while (input[i] != '\0')
        {
            if (input[i] == '\n')
            {
                input[i] = '\0';
                break;
            }
            i++;
        }

        printf("\x1b[34m");
        villager_action(villager, demon, input);
        printf("\x1b[0m");

        end_fight(villager, demon);

        printf("\x1b[31m");
        demon_action(demon, villager);
        printf("\x1b[0m");

        end_fight(villager, demon);

        pp_villager(villager);
        pp_demon(demon);

        printf("> ");
    }

    destroy_demon(demon);
    destroy_villager(villager);
    return 0;
}


