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

void load_flag(Game *g, char *line) {
  char *token = strtok(line, "|");
  Flag *flag = &g->flags->flags[g->flags->count];
  flag->key = malloc(strlen(token) + 1);
  strcpy(flag->key, token);

  token = strtok(NULL, "|");
  flag->value = atoi(token);

  g->flags->count++;
}

#include "data/flags.c"
void game_load_flags(Game *g) {
  g->flags = malloc(sizeof(Flags));
  g->flags->count = 0;
  parse_multiline_string(g, data_flags_txt, &load_flag);
  printf("loaded flags\n");
}

int flag_value(Flags *f, char *key) {
  Flag *flag = find_flag(f, key);
  return flag ? flag->value : -1;
}

Flag *find_flag(Flags *f, char *key) {
  for (int i = 0; i < f->count; i++) {
    if (strcmp(f->flags[i].key, key) == 0) return &f->flags[i];
  }

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