# Path: mgz/util.py
# class Version(Enum):
#     """Version enumeration.
# 
#     Using consts from https://github.com/goto-bus-stop/recanalyst/blob/master/src/Model/Version.php
#     for consistency.
#     """
#     AOK = 1
#     AOC = 4
#     AOC10 = 5
#     AOC10C = 8
#     USERPATCH12 = 12
#     USERPATCH13 = 13
#     USERPATCH14 = 11
#     USERPATCH15 = 20
#     DE = 21
#     USERPATCH14RC2 = 22
#     MCP = 30
#     HD = 19


from construct import (Array, Byte, Computed, Embedded, Flag, IfThenElse,
                       Int32ul, Padding, Struct, Int16sl, If, Peek)
from mgz.util import Version

# Represents a single map tile, defined by terrain type and elevation.
tile = "tile"/Struct(
    "terrain_type"/Byte,
    Embedded(IfThenElse(lambda ctx: ctx._._.version == Version.DE,
        Embedded(Struct(
            Padding(1), # copy of previous byte
            "elevation"/Byte,
            "unk0"/Int16sl,
            "unk1"/Int16sl,
            If(lambda ctx: ctx._._._.save_version >= 13.03 or ctx._._.check.val > 1000, "unk2"/Int16sl)
        )),
        Embedded(IfThenElse(lambda ctx: ctx.terrain_type == 255, Struct(
            "terrain_type"/Byte,
            "elevation"/Byte,
            Padding(1)
        ), Struct(
            "elevation"/Byte
        )))
    ))
)


# Map size and terrain.
map_info = "map_info"/Struct(
    "size_x"/Int32ul,
    "size_y"/Int32ul,
    "tile_num"/Computed(lambda ctx: ctx.size_x * ctx.size_y),
    "zone_num"/Int32ul,
    Array(lambda ctx: ctx.zone_num, Struct(

# Path: mgz/util.py
# class Version(Enum):
#     """Version enumeration.
# 
#     Using consts from https://github.com/goto-bus-stop/recanalyst/blob/master/src/Model/Version.php
#     for consistency.
#     """
#     AOK = 1
#     AOC = 4
#     AOC10 = 5
#     AOC10C = 8
#     USERPATCH12 = 12
#     USERPATCH13 = 13
#     USERPATCH14 = 11
#     USERPATCH15 = 20
#     DE = 21
#     USERPATCH14RC2 = 22
#     MCP = 30
#     HD = 19


# Path: mgz/header/map_info.py
from construct import (Array, Byte, Computed, Embedded, Flag, IfThenElse,
                       Int32ul, Padding, Struct, Int16sl, If, Peek)
from mgz.util import Version


# Represents a single map tile, defined by terrain type and elevation.
tile = "tile"/Struct(
    "terrain_type"/Byte,
    Embedded(IfThenElse(lambda ctx: ctx._._.version == Version.DE,
        Embedded(Struct(
            Padding(1), # copy of previous byte
            "elevation"/Byte,
            "unk0"/Int16sl,
            "unk1"/Int16sl,
            If(lambda ctx: ctx._._._.save_version >= 13.03 or ctx._._.check.val > 1000, "unk2"/Int16sl)
        )),
        Embedded(IfThenElse(lambda ctx: ctx.terrain_type == 255, Struct(
            "terrain_type"/Byte,
            "elevation"/Byte,
            Padding(1)
        ), Struct(
            "elevation"/Byte
        )))
    ))
)


# Map size and terrain.
map_info = "map_info"/Struct(
    "size_x"/Int32ul,
    "size_y"/Int32ul,
    "tile_num"/Computed(lambda ctx: ctx.size_x * ctx.size_y),
    "zone_num"/Int32ul,
    Array(lambda ctx: ctx.zone_num, Struct(

        IfThenElse(lambda ctx: ctx._._.version in (Version.DE, Version.HD),