import unreal


def load(path):
    asset = unreal.EditorAssetLibrary.load_asset(path)
    if not asset:
        raise RuntimeError(f"Could not load {path}")
    return asset


def mapping_found(context, action, key_name):
    mapping_data = context.get_editor_property("default_key_mappings")
    for mapping in list(mapping_data.get_editor_property("mappings")):
        mapping_key = mapping.get_editor_property("key")
        if (
            mapping.get_editor_property("action") == action
            and str(mapping_key.get_editor_property("key_name")) == key_name
        ):
            return True
    return False


def main():
    action = load("/Game/Input/Actions/IA_ToggleCamera")
    context = load("/Game/Input/IMC_Default")
    character_bp = load("/Game/Agrarian/Blueprints/Characters/BP_AgrarianPlayerCharacter")
    character_cdo = unreal.get_default_object(character_bp.generated_class())

    missing = []
    for key_name in ["V", "Gamepad_RightThumbstick"]:
        if not mapping_found(context, action, key_name):
            missing.append(f"missing mapping {key_name}")

    assigned_action = character_cdo.get_editor_property("ToggleCameraAction")
    if assigned_action != action:
        missing.append("BP_AgrarianPlayerCharacter ToggleCameraAction is not IA_ToggleCamera")

    if missing:
        raise RuntimeError("Camera toggle input verification failed: " + "; ".join(missing))

    unreal.log("Agrarian camera toggle input verification complete.")


main()
