//! Exact Rust translations of `DetachMeCommandTests`.

use libremetaverse_compat_tests::block_on;
use libremetaverse_compat_tests::rlv_support::{RlvHarness, SampleInventoryTree, guid};
use libremetaverse_rlv::{InventoryMap, RlvAttachmentPoint};
use libremetaverse_types::compat::Guid;

const RETRO: &str = "b2222222-bbbb-4bbb-8bbb-bbbbbbbbbbbb";
const PRIM2: &str = "11111111-0002-4aaa-8aaa-ffffffffffff";
const PRIM3: &str = "11111111-0003-4aaa-8aaa-ffffffffffff";

fn run(nostrip: bool, copy: bool, locked: bool) -> Vec<Guid> {
    let h = RlvHarness::new().expect("RlvService constructor");
    let mut tree = SampleInventoryTree::build().unwrap();
    if nostrip {
        tree.clothing.set_name("nostrip Clothing".into());
        tree.retro_pants.set_name("nostrip Retro Pants".into());
    }
    tree.retro_pants
        .set_attached_to(Some(Some(RlvAttachmentPoint::Pelvis)));
    tree.retro_pants
        .set_attached_prim_id(Some(Some(guid(PRIM2))));
    tree.happy_shirt
        .set_attached_to(Some(Some(RlvAttachmentPoint::Spine)));
    tree.happy_shirt
        .set_attached_prim_id(Some(Some(guid(PRIM3))));
    if copy {
        tree.hats
            .add_item(
                guid(RETRO),
                if nostrip {
                    "nostrip Retro Pants"
                } else {
                    "Retro Pants"
                }
                .into(),
                false,
                Some(Some(RlvAttachmentPoint::Pelvis)),
                Some(Some(guid(PRIM2))),
                None,
                None,
            )
            .unwrap();
    }
    h.queries.lock().unwrap().inventory_map = Some(InventoryMap::new(tree.root, vec![]).unwrap());
    if locked {
        block_on(h.process("@detachthis:Clothing/Hats=n")).unwrap();
    }
    block_on(h.process_from(
        "@detachme=force",
        PRIM2,
        if nostrip {
            "nostrip Retro Pants"
        } else {
            "Retro Pants"
        },
    ))
    .unwrap();
    h.actions.lock().unwrap().detachments[0].clone()
}

// parity-case: LibreMetaverse.Tests/RLV/Commands/DetachMeCommandTests.cs::DetachMeCommandTests.DetachMeForce::test fde065233689ec75c12e9607acf06c5859a91efea58e143ecc508ebec784a567 translated
#[test]
fn detach_me() {
    assert_eq!(run(false, false, false), vec![guid(RETRO)]);
}
// parity-case: LibreMetaverse.Tests/RLV/Commands/DetachMeCommandTests.cs::DetachMeCommandTests.DetachMeForce_WithNoStripItemName_SpecialException::test ee511230131312be9b788033aac8766ea3cb3c1f8d7c5b9b26a4bd6a83cd695c translated
#[test]
fn detach_me_ignores_nostrip() {
    assert_eq!(run(true, false, false), vec![guid(RETRO)]);
}
// parity-case: LibreMetaverse.Tests/RLV/Commands/DetachMeCommandTests.cs::DetachMeCommandTests.DetachMeForce_WithLockedFolderRestriction::test 05fefae0563306af0c491006f18915ff1b250b7972c82c160f20f06e07335887 translated
#[test]
fn detach_me_honors_locked_linked_folder() {
    assert!(run(false, true, true).is_empty());
}
// parity-case: LibreMetaverse.Tests/RLV/Commands/DetachMeCommandTests.cs::DetachMeCommandTests.DetachMeForce_MultipleLinksSameItem::test 1ae8a318762625819371fb109fd673e0d31a2c0354add1e65802eb54cb71e303 translated
#[test]
fn detach_me_deduplicates_multiple_links() {
    assert_eq!(run(false, true, false), vec![guid(RETRO)]);
}
