#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "game.h"
#include "room.h"
#include "item.h"
#include "util.h"

void load_item(Game *g, char *line) {
  char *token = strtok(line, "|");
  Item *item = &g->items->items[g->items->count];
  item->name = malloc(strlen(token) + 1);
  strcpy(item->name, token);

  token = strtok(NULL, "|");
  item->indefinite = malloc(strlen(token) + 1);
  strcpy(item->indefinite, token);

  token = strtok(NULL, "|");
  item->pickuppable = strcmp("true", token) == 0;

  token = strtok(NULL, "|");
  item->description = malloc(strlen(token) + 1);
  strcpy(item->description, token);

  token = strtok(NULL, "|");
  if (strcmp(token, "*") == 0) {
    item->location = NULL;
  } else {
	item->location = find_room(g->rooms, token);
  }
  item->in_inventory = false;

  g->items->count++;
}

#include "data/items.c"
void game_load_items(Game *g) {
  g->items = malloc(sizeof(Items));
  g->items->count = 0;
  parse_multiline_string(g, data_items_txt, &load_item);
  printf("loaded items\n");
}

#define ITEM_IN_ROOM "There is %s here."
void log_item_in_room(Game *g, Item *i) {
  char *response = malloc(strlen(ITEM_IN_ROOM) + strlen(i->indefinite) + 1);
  sprintf(response, ITEM_IN_ROOM, i->indefinite);
  push_line_to_log(g->input->log, response);
  free(response);
}

#define ITEM_IN_INVENTORY "You have %s."
void log_item_in_inventory(Game *g, Item *i) {
  char *response = malloc(strlen(ITEM_IN_INVENTORY) + strlen(i->indefinite) + 1);
  sprintf(response, ITEM_IN_INVENTORY, i->indefinite);
  push_line_to_log(g->input->log, response);
  free(response);
}

void log_items_in_room(Game *g, Room *r) {
  for (int i = 0; i < g->items->count; i++) {
    if (g->items->items[i].location == r) log_item_in_room(g, &g->items->items[i]);
  }
}

Item *find_item(Items *items, char *item_name) {
  for (int i = 0; i < items->count; i++) {
    if (strcmp(items->items[i].name, item_name) == 0) return &items->items[i];
  }

  return NULL;
}

void take_item(Game *g, char *item_name) {
  Item *item = find_item(g->items, item_name);
  item->location = NULL;
  item->in_inventory = true;
}

void drop_item(Game *g, char *item_name) {
  Item *item = find_item(g->items, item_name);
  item->location = g->current_room;
  item->in_inventory = false;
}

void destroy_item(Game *g, char *item_name) {
  Item *item = find_item(g->items, item_name);
  item->location = NULL;
  item->in_inventory = false;
}

void check_inventory(Game *g) {
  bool empty = true;
  for (int i = 0; i < g->items->count; i++) {
    if (g->items->items[i].in_inventory) {
      empty = false;
      log_item_in_inventory(g, &g->items->items[i]);
    }
  }

  if (empty) {
    push_line_to_log(g->input->log, "Your inventory is empty.");
  }
}
