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

void load_room(Game *g, char *line) {
  char *token = strtok(line, "|");

  g->rooms->rooms[g->rooms->count].name = malloc(strlen(token) + 1);
  strcpy(g->rooms->rooms[g->rooms->count].name, token);

  g->rooms->rooms[g->rooms->count].visited = false;

  g->rooms->count++;
}

#include "data/rooms.c"
void game_load_rooms(Game *g) {
  g->rooms = malloc(sizeof(Rooms));
  g->rooms->count = 0;
  parse_multiline_string(g, data_rooms_txt, &load_room);
  printf("loaded rooms\n");
}

void free_room(Room r) {
  free(r.name);
}

void free_rooms(Rooms r) {
  for (int i = 0; i < r.count; i++) {
    free_room(r.rooms[i]);
  }
}

Room *find_room(Rooms *r, const char *name) {
  for (int i = 0; i < r->count; i++) {
    if (strcmp(r->rooms[i].name, name) == 0) {
      return &r->rooms[i];
    }
  }

  printf("Couldn't find room %s\n", name);
  return NULL;
}
