use libremetaverse::marketplace::{MarketplaceListing, MarketplaceManager};
use libremetaverse::{
    DirectoryManager, DirectoryManagerAgentSearchData, EstateTask, EstateTools, FriendsManager,
    GroupManager, GroupProfileEventArgs, ObjectManager, ParcelManager, ParcelSettings, Simulator,
    TerrainManager, TerrainPatch,
};
use libremetaverse_types::UUID;
use libremetaverse_types::compat::{
    CancellationToken, EventHandler, IAsyncEnumerable, Subscription,
};

fn member_id<T>(result: Result<T, libremetaverse::Error>) -> &'static str {
    result
        .err()
        .expect("failure-only shim unexpectedly returned a value")
        .csharp_member()
        .expect("failure-only shim error")
}

fn compile_search_call(directory: &DirectoryManager, token: CancellationToken) {
    let _: Result<IAsyncEnumerable<DirectoryManagerAgentSearchData>, libremetaverse::Error> =
        directory.search_people(String::new(), Some(token));
}

fn compile_group_and_friend_calls(
    groups: &GroupManager,
    friends: &FriendsManager,
    group_id: UUID,
    friend_id: UUID,
    handler: EventHandler<GroupProfileEventArgs>,
) {
    let _: Subscription = groups.subscribe_group_profile(handler);
    let _ = groups.request_group_profile(group_id);
    let _ = friends.offer_friendship_with_uuid(friend_id);
}

fn compile_parcel_call(parcels: &ParcelManager, parcel_id: UUID) {
    let _ = parcels.request_parcel_info(parcel_id);
}

fn compile_object_call(objects: &ObjectManager, simulator: Simulator) {
    let _ = objects.request_object(simulator, 1);
}

fn compile_estate_call(estate: &EstateTools) {
    let _ = estate.request_info();
    let _ = estate.request_top_scripts();
}

async fn compile_terrain_call(terrain: &TerrainManager, token: CancellationToken) {
    let _ = terrain.get_terrain_material_overrides(Some(token)).await;
}

async fn compile_marketplace_call(marketplace: &MarketplaceManager, token: CancellationToken) {
    let _ = marketplace.fetch_listings(Some(token)).await;
}

#[test]
fn world_and_social_flows_have_typed_callable_signatures() {
    let _ = compile_search_call;
    let _ = compile_group_and_friend_calls;
    let _ = compile_parcel_call;
    let _ = compile_object_call;
    let _ = compile_estate_call;
    let _ = compile_terrain_call;
    let _ = compile_marketplace_call;
}

#[test]
fn world_facing_constructors_fail_with_catalog_ids() {
    assert_eq!(
        member_id(EstateTask::new()),
        "M:LibreMetaverse.EstateTask.#ctor"
    );
    assert_eq!(
        member_id(MarketplaceListing::new()),
        "M:LibreMetaverse.Marketplace.MarketplaceListing.#ctor"
    );
    assert_eq!(
        member_id(ParcelSettings::new()),
        "M:LibreMetaverse.ParcelSettings.#ctor"
    );
    assert_eq!(
        member_id(TerrainPatch::new()),
        "M:LibreMetaverse.TerrainPatch.#ctor"
    );
}
