import unreal


MAP_PATH = "/Game/Agrarian/Maps/L_GroundZeroTerrain_Test"
CHARACTER_CLASS_PATH = "/Game/Agrarian/Blueprints/Characters/BP_AgrarianPlayerCharacter"
SHELTER_RECIPE_PATH = "/Game/Agrarian/DataAssets/Recipes/DA_Recipe_PrimitiveShelter"
FRAME_RECIPE_PATH = "/Game/Agrarian/DataAssets/Recipes/DA_Recipe_PrimitiveFrame"
WALL_PANEL_RECIPE_PATH = "/Game/Agrarian/DataAssets/Recipes/DA_Recipe_PrimitiveWallPanel"
ROOF_PANEL_RECIPE_PATH = "/Game/Agrarian/DataAssets/Recipes/DA_Recipe_PrimitiveRoofPanel"
SHELTER_CLASS_PATH = "/Game/Agrarian/Blueprints/Structures/BP_PrimitiveShelter"
WOOD_NODE_LABEL = "AGR_WoodResourceNode_01"
FIBER_NODE_LABEL = "AGR_FiberResourceNode_01"
RABBIT_LABEL = "AGR_RabbitWildlife_01"


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


def load_blueprint_class(path):
    generated_class = unreal.EditorAssetLibrary.load_blueprint_class(path)
    if not generated_class:
        raise RuntimeError(f"Could not load Blueprint class: {path}")
    return generated_class


def find_actor_by_label(label):
    for actor in unreal.EditorLevelLibrary.get_all_level_actors():
        if get_actor_label(actor) == label:
            return actor
    return None


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

    character_class = load_blueprint_class(CHARACTER_CLASS_PATH)
    shelter_class = load_blueprint_class(SHELTER_CLASS_PATH)
    shelter_recipe = unreal.EditorAssetLibrary.load_asset(SHELTER_RECIPE_PATH)
    if not shelter_recipe:
        raise RuntimeError(f"Could not load shelter recipe: {SHELTER_RECIPE_PATH}")
    frame_recipe = unreal.EditorAssetLibrary.load_asset(FRAME_RECIPE_PATH)
    if not frame_recipe:
        raise RuntimeError(f"Could not load frame recipe: {FRAME_RECIPE_PATH}")
    wall_panel_recipe = unreal.EditorAssetLibrary.load_asset(WALL_PANEL_RECIPE_PATH)
    if not wall_panel_recipe:
        raise RuntimeError(f"Could not load wall panel recipe: {WALL_PANEL_RECIPE_PATH}")
    roof_panel_recipe = unreal.EditorAssetLibrary.load_asset(ROOF_PANEL_RECIPE_PATH)
    if not roof_panel_recipe:
        raise RuntimeError(f"Could not load roof panel recipe: {ROOF_PANEL_RECIPE_PATH}")

    wood_node = find_actor_by_label(WOOD_NODE_LABEL)
    if not wood_node:
        raise RuntimeError(f"Could not find placed wood node: {WOOD_NODE_LABEL}")
    fiber_node = find_actor_by_label(FIBER_NODE_LABEL)
    if not fiber_node:
        raise RuntimeError(f"Could not find placed fiber node: {FIBER_NODE_LABEL}")
    rabbit = find_actor_by_label(RABBIT_LABEL)
    if not rabbit:
        raise RuntimeError(f"Could not find placed rabbit wildlife: {RABBIT_LABEL}")

    result = unreal.AgrarianEditorAutomationLibrary.run_natural_shelter_loop_smoke_test(
        character_class,
        wood_node,
        fiber_node,
        rabbit,
        frame_recipe,
        wall_panel_recipe,
        roof_panel_recipe,
        shelter_recipe,
        shelter_class,
    )
    unreal.log(result)
    if not str(result).startswith("PASS:"):
        raise RuntimeError(result)

    unreal.log("Agrarian playable loop smoke verification complete.")


main()
