#include "game_world.h"

#include <stdlib.h>
#include <assert.h>

#define MIN_LEVEL_WIDTH 8 // TODO: SS - Move these out of here.
#define MIN_LEVEL_HEIGHT 8

Game_World *game_world_create(uint32_t seed, uint16_t level_width, uint16_t level_height) {
    assert(level_width >= MIN_LEVEL_WIDTH); // We should probably have failed earlier.
    assert(level_height >= MIN_LEVEL_HEIGHT);

    Game_World *w = (Game_World *)calloc(1, sizeof(Game_World));
    assert(w != NULL);
    w->seed = seed;

    w->max_entities = level_width * level_height;
    w->entities = (Entity *)calloc(w->max_entities, sizeof(Entity));

    grid_initialize(&w->grid, level_width, level_height);

    
    { // TODO: SS - Create the level.
        {
            Grid_Cell *cell = grid_get_cell(&w->grid, 5, 3);
            assert(cell != NULL);
            cell->entity.type = Entity_Type_Food;
        }

        {
            Grid_Cell *cell = grid_get_cell(&w->grid, 11, 4);
            assert(cell != NULL);
            cell->entity.type = Entity_Type_Food;
        }

        {
            Grid_Cell *cell = grid_get_cell(&w->grid, 15, 9);
            assert(cell != NULL);
            cell->entity.type = Entity_Type_Food;
        }

        {
            Grid_Cell *cell = grid_get_cell(&w->grid, 24, 15);
            assert(cell != NULL);
            cell->entity.type = Entity_Type_Snake_Head;
        }
        {
            Grid_Cell *cell = grid_get_cell(&w->grid, 23, 15);
            assert(cell != NULL);
            cell->entity.type = Entity_Type_Snake_Body;
        }
    }


    return w;
}

void game_world_destroy(Game_World *world) {
    assert(world != NULL);
    
    world->seed = 0;
    world->max_entities = 0;
    free(world->entities);
    grid_dispose(&world->grid);
    free(world);
}