import unreal


CHARACTER_BLUEPRINT_PATH = "/Game/Agrarian/Blueprints/Characters/BP_AgrarianPlayerCharacter"
EXPECTED_MESH_PATH = "/Game/Characters/Mannequins/Meshes/SKM_Quinn_Simple"
EXPECTED_ANIM_BLUEPRINT_PATH = "/Game/Characters/Mannequins/Anims/Unarmed/ABP_Unarmed"
REQUIRED_ANIMATION_ASSETS = [
    "/Game/Characters/Mannequins/Anims/Unarmed/BS_Idle_Walk_Run",
    "/Game/Characters/Mannequins/Anims/Unarmed/MM_Idle",
    "/Game/Characters/Mannequins/Anims/Unarmed/Jump/MM_Jump",
    "/Game/Characters/Mannequins/Anims/Unarmed/Jump/MM_Fall_Loop",
    "/Game/Characters/Mannequins/Anims/Unarmed/Jump/MM_Land",
]


def object_path(value):
    if not value:
        return ""
    return value.get_path_name().split(".")[0]


def main():
    failures = []

    for asset_path in [EXPECTED_MESH_PATH, EXPECTED_ANIM_BLUEPRINT_PATH] + REQUIRED_ANIMATION_ASSETS:
        if not unreal.EditorAssetLibrary.does_asset_exist(asset_path):
            failures.append(f"{asset_path} missing")

    character_class = unreal.EditorAssetLibrary.load_blueprint_class(CHARACTER_BLUEPRINT_PATH)
    if not character_class:
        failures.append(f"{CHARACTER_BLUEPRINT_PATH} generated class missing")
    else:
        character_cdo = unreal.get_default_object(character_class)
        mesh_component = character_cdo.get_editor_property("mesh")
        if not mesh_component:
            failures.append(f"{CHARACTER_BLUEPRINT_PATH} mesh component missing")
        else:
            skeletal_mesh_path = object_path(mesh_component.get_skeletal_mesh_asset())
            if skeletal_mesh_path != EXPECTED_MESH_PATH:
                failures.append(
                    f"{CHARACTER_BLUEPRINT_PATH} skeletal mesh expected "
                    f"{EXPECTED_MESH_PATH}, got {skeletal_mesh_path}"
                )

            anim_class = mesh_component.get_editor_property("anim_class")
            anim_class_path = anim_class.get_path_name().split(".")[0] if anim_class else ""
            if anim_class_path != EXPECTED_ANIM_BLUEPRINT_PATH:
                failures.append(
                    f"{CHARACTER_BLUEPRINT_PATH} anim class expected "
                    f"{EXPECTED_ANIM_BLUEPRINT_PATH}, got {anim_class_path}"
                )

    anim_blueprint_class = unreal.EditorAssetLibrary.load_blueprint_class(EXPECTED_ANIM_BLUEPRINT_PATH)
    if not anim_blueprint_class:
        failures.append(f"{EXPECTED_ANIM_BLUEPRINT_PATH} generated class missing")
    elif "ABP_Unarmed_C" not in anim_blueprint_class.get_name():
        failures.append(
            f"{EXPECTED_ANIM_BLUEPRINT_PATH} generated class should be ABP_Unarmed_C, "
            f"got {anim_blueprint_class.get_name()}"
        )

    if failures:
        raise RuntimeError("Basic animation Blueprint verification failed: " + "; ".join(failures))

    unreal.log(
        "Basic animation Blueprint verification complete: "
        "BP_AgrarianPlayerCharacter uses SKM_Quinn_Simple with ABP_Unarmed and required "
        "idle, locomotion, jump, fall, and land assets are present."
    )


main()
