#include <game_private.h>
#include <building.h>
#include <container.h>
#include <font.h>
#include <gfx.h>
#include <gui/bar.h>
#include <gui/button.h>
#include <gui/line_edit.h>
#include <gui/rounded_rect.h>
#include <gui/checkbox.h>
#include <terrain.h>
#include <unit.h>
#include <stdbool.h>

static const struct container c[] =
{
    {
        .path = "sidewalk",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &terrain_sprites[SIDEWALK]
    },

    {
        .path = "roof1",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &terrain_sprites[ROOF1]
    },

    {
        .path = "roof2",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &terrain_sprites[ROOF2]
    },

    {
        .path = "cursor",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &cursor_sprite
    },

    {
        .path = "btn_left",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &gui_button_sprites[GUI_BUTTON_LEFT]
    },

    {
        .path = "btn_mid",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &gui_button_sprites[GUI_BUTTON_MID]
    },

    {
        .path = "btn_right",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &gui_button_sprites[GUI_BUTTON_RIGHT]
    },

    {
        .path = "btn_small",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &gui_button_sprites[GUI_BUTTON_SMALL]
    },

    {
        .path = "font",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &font_sprite
    },

    {
        .path = "grass",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &terrain_sprites[GRASS]
    },

    {
        .path = "unit1",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &unit_sprites[UNIT_1_SPRITE]
    },

    {
        .path = "unit2",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &unit_sprites[UNIT_2_SPRITE]
    },

    {
        .path = "unit3",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &unit_sprites[UNIT_3_SPRITE]
    },

    {
        .path = "unit4",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &unit_sprites[UNIT_4_SPRITE]
    },

    {
        .path = "unit5",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &unit_sprites[UNIT_5_SPRITE]
    },

    {
        .path = "unit6",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &unit_sprites[UNIT_6_SPRITE]
    },

    {
        .path = "sel_down_left",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &gui_rounded_rect_sprites[GUI_ROUNDED_RECT_DOWN_LEFT]
    },

    {
        .path = "sel_down_right",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &gui_rounded_rect_sprites[GUI_ROUNDED_RECT_DOWN_RIGHT]
    },

    {
        .path = "sel_mid_down",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &gui_rounded_rect_sprites[GUI_ROUNDED_RECT_MID_DOWN]
    },

    {
        .path = "sel_mid_top",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &gui_rounded_rect_sprites[GUI_ROUNDED_RECT_MID_TOP]
    },

    {
        .path = "sel_mid_v",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &gui_rounded_rect_sprites[GUI_ROUNDED_RECT_MID_VERT]
    },

    {
        .path = "sel_up_left",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &gui_rounded_rect_sprites[GUI_ROUNDED_RECT_UP_LEFT]
    },

    {
        .path = "sel_up_right",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &gui_rounded_rect_sprites[GUI_ROUNDED_RECT_UP_RIGHT]
    },

    {
        .path = "checkbox",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &gui_checkbox_sprite
    },

    {
        .path = "line_edit_left",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &gui_line_edit_sprites[GUI_LINE_EDIT_LEFT]
    },

    {
        .path = "line_edit_mid",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &gui_line_edit_sprites[GUI_LINE_EDIT_MID]
    },

    {
        .path = "line_edit_right",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &gui_line_edit_sprites[GUI_LINE_EDIT_RIGHT]
    },

    {
        .path = "car1",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &unit_sprites[UNIT_CAR_1_SPRITE]
    },

    {
        .path = "pavement",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &terrain_sprites[PAVEMENT]
    },

    {
        .path = "car2",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &unit_sprites[UNIT_CAR_2_SPRITE]
    },

    {
        .path = "building1",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &terrain_sprites[BUILDING1],
    },

    {
        .path = "building2",
        .type = CONTAINER_TYPE_SPRITE,
        .data.sprite = &terrain_sprites[BUILDING2],
    }
};

static bool init;

void game_free(void)
{
    if (init)
    {
        container_free(c, sizeof c / sizeof *c);
        init = false;
    }
}

int game_resinit(void)
{
    if (!init)
    {
        if (container_load("jancity.cnt", c, sizeof c / sizeof *c))
        {
            perror("container_load");
            return -1;
        }

        init = true;
    }

    return 0;
}
