import unreal


MAP_PATH = "/Game/Agrarian/Maps/L_GroundZeroTerrain_Test"
WATER_SOURCE_LABEL = "AGR_GZ_FreshWaterSource_01"
EXPECTED_WATER_RESTORE = 45.0


def nearly_equal(left, right, tolerance=0.001):
    return abs(float(left) - float(right)) <= tolerance


def get_actor_label(actor):
    try:
        return actor.get_actor_label()
    except Exception:
        return actor.get_name()


def main():
    if not unreal.EditorLevelLibrary.load_level(MAP_PATH):
        raise RuntimeError(f"Could not load map: {MAP_PATH}")

    actors = unreal.EditorLevelLibrary.get_all_level_actors()
    water_sources = [actor for actor in actors if get_actor_label(actor) == WATER_SOURCE_LABEL]
    if len(water_sources) != 1:
        raise RuntimeError(f"Expected exactly one {WATER_SOURCE_LABEL}, found {len(water_sources)}")

    water_source = water_sources[0]
    actual_restore = water_source.get_editor_property("water_restore_amount")
    if not nearly_equal(actual_restore, EXPECTED_WATER_RESTORE):
        raise RuntimeError(f"Water restore expected {EXPECTED_WATER_RESTORE}, got {actual_restore}")

    if not isinstance(water_source, unreal.AgrarianWaterSource):
        raise RuntimeError(f"{WATER_SOURCE_LABEL} is not an AgrarianWaterSource")

    unreal.log(
        "Ground Zero water source verification complete: "
        f"{WATER_SOURCE_LABEL}, restore={actual_restore}."
    )


main()
