#include "buildings.h"

#include "components.h"
#include "game_state.h"
#include "map_layers.h"

bool canPlaceBuilding(BzTileMap *map, BuildingType type, BzTile tileX, BzTile tileY) {
    i32 sizeX, sizeY;
    getBuildingSize(type, &sizeX, &sizeY);
    if (sizeX == 0 || sizeY == 0) return false;

    // Ensure that it is within the map
    if (tileX < 0 || tileX + sizeX > map->width ||
        tileY < 0 || tileY + sizeY > map->height)
        return false;

    Rectangle buildArea = {tileX * map->tileWidth, tileY * map->tileHeight,
                           sizeX * map->tileWidth, sizeY * map->tileHeight};

    // Need to check neighbour tiles
    //tileX -= 1;
    //tileY -= 1;
    //sizeX += 2;
    //sizeY += 2;

    BzTileLayer *buildLayer = bzTileMapGetLayer(map, LAYER_BUILDINGS);
    BzTileset *tileset = bzTileLayerGetTileset(map, buildLayer);

    for (i32 y = tileY; y < tileY + sizeY; y++) {
        for (i32 x = tileX; x < tileX + sizeX; x++) {
            BzTile tile = bzTileLayerGetTile(buildLayer, x, y);
            tile = bzTilesetGetTile(tileset, tile);
            tile = getTileBuilding(tile);
            if (tile == BUILDINGS_ROAD)
                return false;
            if (bzTileMapHasCollision(map, x, y)) {
                return false;
            }
        }
    }

    return true;
}

ecs_entity_t placeBuilding(BzTileMap *map, BuildingType type, BzTile tileX, BzTile tileY) {
    Game *game = ecs_singleton_get_mut(ECS, Game);
    i32 sizeX, sizeY;
    getBuildingSize(type, &sizeX, &sizeY);

    BzTileLayer *buildingLayer = bzTileMapGetLayer(map, LAYER_BUILDINGS);
    BzTileset *buildingTileset = bzTileLayerGetTileset(map, buildingLayer);
    BzTile buildingTile = getBuildingTile(type);
    BZ_ASSERT(buildingTile != -1);

    // Create entity
    ecs_entity_t e = ecs_new_id(ECS);

    ecs_set(ECS, e, TilePosition, { .x = tileX, .y = tileY });
    ecs_set(ECS, e, TileSize, { .sizeX = sizeX, .sizeY = sizeY });
    ecs_set(ECS, e, Owner, { .playerID = BUILDINGS_PLAYER_RED });

    for (i32 y = tileY; y < tileY + sizeY; y++) {
        for (i32 x = tileX; x < tileX + sizeX; x++) {
            BzTile layerTile = buildingTile + buildingTileset->startID;
            bzTileLayerSetTile(buildingLayer, layerTile, x, y, 1, 1);
            buildingTile++;

            bzTileMapUpdateCollisions(map, x, y, map->tileWidth, map->tileHeight);
        }
        buildingTile += buildingTileset->width - sizeX;
    }

    return e;

}
