#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "card.h"

card_t cards[2000];
int counter = 0;

void get_csv(char* line)
{
    char* token;
    int internal_counter = 0, i;
    token = strtok(line, ",");

    while (token != NULL)
    {
        internal_counter++;
        switch (internal_counter)
        {
        case 1:
            cards[counter - 1].id = atoi(token);
            break;

        case 2:
            cards[counter - 1].name = token;

            break;

        case 3:
            cards[counter - 1].cost = token;
            break;

        case 4:
            cards[counter - 1].converted_cost = atoi(token);
            break;

        case 5:
            cards[counter - 1].type = token;
            break;

        case 6:
            cards[counter - 1].text = token;
            break;

        case 7:
            cards[counter - 1].stats = token;
            break;

        case 8:
            if (strcmp("\"rare\"", token) == 0)
                cards[counter - 1].rarity = rare;
            else if (strcmp("\"common\"", token) == 0)
                cards[counter - 1].rarity = common;
            else if (strcmp("\"uncommon\"", token) == 0)
                cards[counter - 1].rarity = uncommon;
            else if (strcmp("\"mythic\"", token) == 0)
                cards[counter - 1].rarity = mythic;
            break;
        }

        token = strtok(NULL, ",");
    }
}

int main()
{
    FILE* stream = fopen("cards.csv", "r");
    int i;

    char line[1024];
    while (fgets(line, 1024, stream))
    {
        if (counter > 0)
            get_csv(line);
        counter++;
    }

    for (i = 0; i<counter; i++)
    {
        printf("\n%d,", cards[i].id);
        printf("%s,", cards[i].cost);
        printf("%s,", cards[i].name);
        printf("%s,", cards[i].stats);
        printf("%s,", cards[i].text);
        printf("%s,", cards[i].type);
        switch (cards[i].rarity)
        {
            case rare:
                printf("rare");
                break;

            case common:
                printf("common");
                break;

            case uncommon:
                printf("uncommon");
                break;

            case mythic:
                printf("mythic");
                break;
        }
    }
}