import json
import os.path

from extract_common import *

writer = ExtractFileWriter("game_tileset")


def extract_by_property(tiles, key):
    extracted = []
    for tile in tiles:
        if 'properties' not in tile:
            continue
        props = tile['properties']
        props = [True for prop in props if prop['name'] == key]
        if any(props):
            extracted.append(tile)
    return extracted


def remove_properties(data, dst_file):
    data = json.loads(data)
    new_tiles = []
    for tile in data['tiles']:
        v = {'id': tile['id']}
        if 'objectgroup' in tile:
            v['objectgroup'] = tile['objectgroup']
        new_tiles.append(v)
    data['tiles'] = new_tiles
    del data['wangsets']
    with open(dst_file, 'w') as f:
        #pprint.pprint(data, f)
        json.dump(data, f, indent=None, separators=(',', ':'), ensure_ascii=True)


content = open("../rawAssets/game.tsj").read()
tiles = json.loads(content)["tiles"]
remove_properties(content, "../assets/game.tsj")
terrain_tiles = extract_by_property(tiles, "terrain")
building_tiles = extract_by_property(tiles, "building")
entity_tiles = extract_by_property(tiles, "entity")
item_tiles = extract_by_property(tiles, "item")

writer.header_guard_start()
script_name = os.path.basename(__file__)
writer.output(f"// This file was generated by: {script_name}\n\n")
writer.include("<breeze.h>")
writer.include("<string.h>")

writer.empty_line()
writer.output_anim_sequence_struct()
writer.output_anim_frame_struct()

terrain_writer = EnumWriter(writer, terrain_tiles, "terrain")
building_writer = EnumWriter(writer, building_tiles, "building")
entity_writer = EnumWriter(writer, entity_tiles, "entity")

terrain_writer.output_enum()
terrain_writer.output_tile_has_anim("terrainHasAnimation")
terrain_writer.output_tile_anim_sequence("terrainGetAnimationSequence")
terrain_writer.output_tile_anim_frame("terrainGetAnimationFrame")
writer.empty_line()

building_writer.output_enum()
building_writer.output_tile_to_enum("getTileBuilding")
building_writer.output_enum_to_tile("getBuildingTile")
building_writer.output_enum_to_str("getBuildingStr")
building_writer.output_enum_tile_size("getBuildingSize")
writer.empty_line()

anim_writer = EnumWriter(writer, entity_tiles, "entity", "anim")

anim_writer.output_enum()
anim_writer.output_anim_enum()

anim_writer.output_enum_to_tile("getEntityTile")
anim_writer.output_enum_to_str("getEntityStr")
anim_writer.output_anim_enum_to_str("getEntityAnimationStr")
anim_writer.output_has_anim("entityHasAnimation")
anim_writer.output_anim_sequence("entityGetAnimationSequence")
anim_writer.output_anim_frame("entityGetAnimationFrame")

#print(item_tiles)
item_writer = EnumWriter(writer, item_tiles, "item", "item_anim")
item_writer.output_enum()
#item_writer.output_anim_enum()
item_writer.output_enum_to_tile("getItemTile")
#item_writer.output_has_handle("itemHasHandle")
#item_writer.output_handle("itemGetHandle")


writer.header_guard_stop()

writer.to_file("../game/game_tileset.h")
