#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "include/logger.h"
#include "include/map.h"
#include "include/matrix.h"

struct map* loadMap(const char *filename) {
    struct map* map = malloc(sizeof(struct map));
    FILE * mapfile = fopen(filename, "r");
    if (mapfile == NULL) {
        logData(LOGLEVEL_CRIT, "Failed to open map file: %s", __FILE__, __LINE__, (void*[]) {(void*)1, &filename});
        exit(1);
    }
    // calculate number of objects
    char linebuf[1024] = {0};
    int objectnum = 0;
    while (fscanf(mapfile, "%s", linebuf) != EOF) { // TODO: stack overflow potencially if mapfile line has a string with length > 1024!!!
        if (!strcmp(linebuf, "object")) {
            objectnum++;
        }
    }
    rewind(mapfile);
    map->object_count = objectnum;
    if (objectnum == 0) {
        logData(LOGLEVEL_ERR, "Mapfile %s does not contain any objects!", __FILE__, __LINE__, (void*[]) {(void*)1, &filename});
        map->objects = NULL;
        map->object_count = 0;
        return map;   
    }
    map->objects = malloc(sizeof(struct objects)*objectnum);
    objectnum = -1;
    int found[4];
    while (fscanf(mapfile, "%s", linebuf) != EOF) { // TODO: stack overflow potencially if mapfile line has a string with length > 1024!!!
        if (!strcmp(linebuf, "object")) {
            objectnum++;
        }
        if 
    }

    return map;
}