use elseware::common::serverstate::{ClientId, ServerState};
use entity::gateway::{EntityGateway, InMemoryGateway};
use entity::item;
use elseware::ship::ship::{ShipServerState, RecvShipPacket};

use libpso::packet::ship::*;
use libpso::packet::messages::*;

#[path = "common.rs"]
mod common;
use common::*;

#[async_std::test]
async fn test_pick_up_individual_item() {
    let mut entity_gateway = InMemoryGateway::default();

    let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
    let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await;

    let mut p1_inv = Vec::new();
    p1_inv.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Weapon(
                    item::weapon::Weapon {
                        weapon: item::weapon::WeaponType::Handgun,
                        grind: 0,
                        special: None,
                        attrs: [None, None, None],
                        tekked: true,
                    }
                ),
            }).await.unwrap());

    entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap();
    entity_gateway.set_character_inventory(&char2.id, &item::InventoryEntity::new(Vec::<item::InventoryItemEntity>::new())).await.unwrap();

    let mut ship = Box::new(ShipServerState::builder()
        .gateway(entity_gateway.clone())
        .build());
    log_in_char(&mut ship, ClientId(1), "a1", "a").await;
    log_in_char(&mut ship, ClientId(2), "a2", "a").await;

    join_lobby(&mut ship, ClientId(1)).await;
    join_lobby(&mut ship, ClientId(2)).await;

    create_room(&mut ship, ClientId(1), "room", "").await;
    join_room(&mut ship, ClientId(2), 0).await;

    let p1_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
    assert_eq!(p1_items.items.len(), 1);
    let p2_items = entity_gateway.get_character_inventory(&char2.id).await.unwrap();
    assert_eq!(p2_items.items.len(), 0);

    ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::PlayerDropItem(PlayerDropItem {
        client: 0,
        target: 0,
        unknown1: 0,
        map_area: 0,
        item_id: 0x10000,
        x: 0.0,
        y: 0.0,
        z: 0.0,
    })))).await.unwrap();

    let p1_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
    assert_eq!(p1_items.items.len(), 0);
    let p2_items = entity_gateway.get_character_inventory(&char2.id).await.unwrap();
    assert_eq!(p2_items.items.len(), 0);

    ship.handle(ClientId(2), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
        client: 0,
        target: 0,
        item_id: 0x10000,
        map_area: 0,
        unknown: [0; 3]
    })))).await.unwrap();

    let p1_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
    assert_eq!(p1_items.items.len(), 0);
    let p2_items = entity_gateway.get_character_inventory(&char2.id).await.unwrap();
    assert_eq!(p2_items.items.len(), 1);
}

#[async_std::test]
async fn test_pick_up_item_stack_of_items_already_in_inventory() {
    let mut entity_gateway = InMemoryGateway::default();

    let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
    let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await;

    let mut p1_monomate = Vec::new();
    p1_monomate.push(entity_gateway.create_item(
        item::NewItemEntity {
            item: item::ItemDetail::Tool(
                item::tool::Tool {
                    tool: item::tool::ToolType::Monomate
                }
            ),
        }).await.unwrap());

    let mut p2_items = Vec::new();
    for (_slot, tool) in vec![item::tool::ToolType::Monomate, item::tool::ToolType::Monofluid].into_iter().enumerate() {
        let mut item = Vec::new();
        for _ in 0..5usize {
            item.push(entity_gateway.create_item(
                item::NewItemEntity {
                    item: item::ItemDetail::Tool(
                        item::tool::Tool {
                            tool: tool
                        }
                    ),
                }).await.unwrap());
        }
        p2_items.push(item);
    }

    entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![p1_monomate])).await.unwrap();
    entity_gateway.set_character_inventory(&char2.id, &item::InventoryEntity::new(p2_items)).await.unwrap();

    let mut ship = Box::new(ShipServerState::builder()
        .gateway(entity_gateway.clone())
        .build());
    log_in_char(&mut ship, ClientId(1), "a1", "a").await;
    log_in_char(&mut ship, ClientId(2), "a2", "a").await;

    join_lobby(&mut ship, ClientId(1)).await;
    join_lobby(&mut ship, ClientId(2)).await;

    create_room(&mut ship, ClientId(1), "room", "").await;
    join_room(&mut ship, ClientId(2), 0).await;

    ship.handle(ClientId(2), RecvShipPacket::Message(Message::new(GameMessage::PlayerDropItem(PlayerDropItem {
        client: 0,
        target: 0,
        unknown1: 0,
        map_area: 0,
        item_id: 0x210000,
        x: 0.0,
        y: 0.0,
        z: 0.0,
    })))).await.unwrap();

    ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
        client: 0,
        target: 0,
        item_id: 0x210000,
        map_area: 0,
        unknown: [0; 3]
    })))).await.unwrap();

    let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
    assert_eq!(inventory_items.items.len(), 1);
    inventory_items.items[0].with_stacked(|items| {
        assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(),
                   vec![item::ItemEntityId(1), item::ItemEntityId(2), item::ItemEntityId(3), item::ItemEntityId(4), item::ItemEntityId(5), item::ItemEntityId(6)]);
        assert!(items.iter().all(|item| item.item.item_type() == item::ItemType::Tool(item::tool::ToolType::Monomate)));
    }).unwrap();
}

#[async_std::test]
async fn test_pick_up_item_stack_of_items_not_already_held() {
    let mut entity_gateway = InMemoryGateway::default();

    let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
    let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await;

    let mut p2_monomate = Vec::new();
    p2_monomate.push(entity_gateway.create_item(
        item::NewItemEntity {
            item: item::ItemDetail::Tool(
                item::tool::Tool {
                    tool: item::tool::ToolType::Monomate
                }
            ),
        }).await.unwrap());

    entity_gateway.set_character_inventory(&char2.id, &item::InventoryEntity::new(vec![p2_monomate])).await.unwrap();

    let mut ship = Box::new(ShipServerState::builder()
        .gateway(entity_gateway.clone())
        .build());
    log_in_char(&mut ship, ClientId(1), "a1", "a").await;
    log_in_char(&mut ship, ClientId(2), "a2", "a").await;

    join_lobby(&mut ship, ClientId(1)).await;
    join_lobby(&mut ship, ClientId(2)).await;

    create_room(&mut ship, ClientId(1), "room", "").await;
    join_room(&mut ship, ClientId(2), 0).await;

    ship.handle(ClientId(2), RecvShipPacket::Message(Message::new(GameMessage::PlayerDropItem(PlayerDropItem {
        client: 0,
        target: 0,
        unknown1: 0,
        map_area: 0,
        item_id: 0x210000,
        x: 0.0,
        y: 0.0,
        z: 0.0,
    })))).await.unwrap();

    ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
        client: 0,
        target: 0,
        item_id: 0x210000,
        map_area: 0,
        unknown: [0; 3]
    })))).await.unwrap();

    let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
    assert_eq!(inventory_items.items.len(), 1);
    inventory_items.items[0].with_stacked(|items| {
        assert_eq!(items.len(), 1);
        assert_eq!(items[0].id, item::ItemEntityId(1));
        assert_eq!(items[0].item.item_type(), item::ItemType::Tool(item::tool::ToolType::Monomate));
    }).unwrap();
}

#[async_std::test]
async fn test_pick_up_meseta_when_inventory_full() {
    let mut entity_gateway = InMemoryGateway::default();

    let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
    let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await;

    let mut p1_items = Vec::new();
    for _ in 0..30usize {
        p1_items.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Weapon(
                    item::weapon::Weapon {
                        weapon: item::weapon::WeaponType::Saber,
                        grind: 0,
                        special: None,
                        attrs: [None, None, None],
                        tekked: true,
                    }
                ),
            }).await.unwrap());
    }

    entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_items)).await.unwrap();
    entity_gateway.set_character_meseta(&char2.id, item::Meseta(300)).await.unwrap();

    let mut ship = Box::new(ShipServerState::builder()
        .gateway(entity_gateway.clone())
        .build());
    log_in_char(&mut ship, ClientId(1), "a1", "a").await;
    log_in_char(&mut ship, ClientId(2), "a2", "a").await;

    join_lobby(&mut ship, ClientId(1)).await;
    join_lobby(&mut ship, ClientId(2)).await;

    create_room(&mut ship, ClientId(1), "room", "").await;
    join_room(&mut ship, ClientId(2), 0).await;

    ship.handle(ClientId(2), RecvShipPacket::Message(Message::new(GameMessage::DropCoordinates(DropCoordinates {
        client: 0,
        target: 0,
        item_id: 0xFFFFFFFF,
        map_area: 0,
        room: 0,
        x: 0.0,
        z: 0.0,
        amount: 23,
    })))).await.unwrap();

    ship.handle(ClientId(2), RecvShipPacket::Message(Message::new(GameMessage::PlayerNoLongerHasItem(PlayerNoLongerHasItem {
        client: 0,
        target: 0,
        item_id: 0xFFFFFFFF,
        amount: 23,
    })))).await.unwrap();

    ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
        client: 0,
        target: 0,
        item_id: 0x00810001,
        map_area: 0,
        unknown: [0; 3]
    })))).await.unwrap();

    let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
    assert_eq!(inventory_items.items.len(), 30);

    let c1_meseta = entity_gateway.get_character_meseta(&char1.id).await.unwrap();
    let c2_meseta = entity_gateway.get_character_meseta(&char2.id).await.unwrap();
    assert!(c1_meseta.0 == 23);
    assert!(c2_meseta.0 == 277);
}

#[async_std::test]
async fn test_pick_up_partial_stacked_item_when_inventory_is_otherwise_full() {
    let mut entity_gateway = InMemoryGateway::default();

    let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
    let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await;

    let mut p1_inv = Vec::new();
    for _slot in 0..29usize {
        p1_inv.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Weapon(
                    item::weapon::Weapon {
                        weapon: item::weapon::WeaponType::Saber,
                        grind: 0,
                        special: None,
                        attrs: [None, None, None],
                        tekked: true,
                    }
                ),
            }).await.unwrap().into());
    }

    p1_inv.push(item::InventoryItemEntity::Stacked(vec![entity_gateway.create_item(
        item::NewItemEntity {
            item: item::ItemDetail::Tool(
                item::tool::Tool {
                    tool: item::tool::ToolType::Monomate,
                }
            ),
        }).await.unwrap()]));

    let mut p2_monomates = Vec::new();
    p2_monomates.push(entity_gateway.create_item(
        item::NewItemEntity {
            item: item::ItemDetail::Tool(
                item::tool::Tool {
                    tool: item::tool::ToolType::Monomate,
                }
            ),
        }).await.unwrap());

    entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap();
    entity_gateway.set_character_inventory(&char2.id, &item::InventoryEntity::new(vec![p2_monomates])).await.unwrap();

    let mut ship = Box::new(ShipServerState::builder()
        .gateway(entity_gateway.clone())
        .build());
    log_in_char(&mut ship, ClientId(1), "a1", "a").await;
    log_in_char(&mut ship, ClientId(2), "a2", "a").await;

    join_lobby(&mut ship, ClientId(1)).await;
    join_lobby(&mut ship, ClientId(2)).await;

    create_room(&mut ship, ClientId(1), "room", "").await;
    join_room(&mut ship, ClientId(2), 0).await;

    ship.handle(ClientId(2), RecvShipPacket::Message(Message::new(GameMessage::PlayerDropItem(PlayerDropItem {
        client: 0,
        target: 0,
        unknown1: 0,
        map_area: 0,
        item_id: 0x210000,
        x: 0.0,
        y: 0.0,
        z: 0.0,
    })))).await.unwrap();

    ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
        client: 0,
        target: 0,
        item_id: 0x210000,
        map_area: 0,
        unknown: [0; 3]
    })))).await.unwrap();

    let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
    assert_eq!(inventory_items.items.len(), 30);
    inventory_items.items[29].with_stacked(|items| {
        assert_eq!(items.len(), 2);
    }).unwrap();
}

#[async_std::test]
async fn test_can_not_pick_up_item_when_inventory_full() {
    let mut entity_gateway = InMemoryGateway::default();

    let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
    let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await;

    let mut p1_inv = Vec::new();
    for _slot in 0..30usize {
        p1_inv.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Weapon(
                    item::weapon::Weapon {
                        weapon: item::weapon::WeaponType::Saber,
                        grind: 0,
                        special: None,
                        attrs: [None, None, None],
                        tekked: true,
                    }
                ),
            }).await.unwrap());
    }

    let mut p2_inv = Vec::new();
    p2_inv.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Weapon(
                    item::weapon::Weapon {
                        weapon: item::weapon::WeaponType::Handgun,
                        grind: 0,
                        special: None,
                        attrs: [None, None, None],
                        tekked: true,
                    }
                ),
            }).await.unwrap());

    entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap();
    entity_gateway.set_character_inventory(&char2.id, &item::InventoryEntity::new(p2_inv)).await.unwrap();

    let mut ship = Box::new(ShipServerState::builder()
        .gateway(entity_gateway.clone())
        .build());
    log_in_char(&mut ship, ClientId(1), "a1", "a").await;
    log_in_char(&mut ship, ClientId(2), "a2", "a").await;

    join_lobby(&mut ship, ClientId(1)).await;
    join_lobby(&mut ship, ClientId(2)).await;

    create_room(&mut ship, ClientId(1), "room", "").await;
    join_room(&mut ship, ClientId(2), 0).await;

    ship.handle(ClientId(2), RecvShipPacket::Message(Message::new(GameMessage::PlayerDropItem(PlayerDropItem {
        client: 0,
        target: 0,
        unknown1: 0,
        map_area: 0,
        item_id: 0x210000,
        x: 0.0,
        y: 0.0,
        z: 0.0,
    })))).await.unwrap();

    ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
        client: 0,
        target: 0,
        item_id: 0x210000,
        map_area: 0,
        unknown: [0; 3]
    })))).await.unwrap();

    let p1_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
    assert_eq!(p1_items.items.len(), 30);
    let p2_items = entity_gateway.get_character_inventory(&char2.id).await.unwrap();
    assert_eq!(p2_items.items.len(), 0);

    ship.handle(ClientId(2), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
        client: 0,
        target: 0,
        item_id: 0x210000,
        map_area: 0,
        unknown: [0; 3]
    })))).await.unwrap();

    let p1_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
    assert_eq!(p1_items.items.len(), 30);
    let p2_items = entity_gateway.get_character_inventory(&char2.id).await.unwrap();
    assert_eq!(p2_items.items.len(), 1);
}

#[async_std::test]
async fn test_can_not_drop_more_meseta_than_is_held() {
    let mut entity_gateway = InMemoryGateway::default();

    let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;

    entity_gateway.set_character_meseta(&char1.id, item::Meseta(300)).await.unwrap();

    let mut ship = Box::new(ShipServerState::builder()
        .gateway(entity_gateway.clone())
        .build());
    log_in_char(&mut ship, ClientId(1), "a1", "a").await;

    join_lobby(&mut ship, ClientId(1)).await;

    create_room(&mut ship, ClientId(1), "room", "").await;

    ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::DropCoordinates(DropCoordinates {
        client: 0,
        target: 0,
        item_id: 0xFFFFFFFF,
        map_area: 0,
        room: 0,
        x: 0.0,
        z: 0.0,
        amount: 301,
    })))).await.unwrap();

    let split_attempt = ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::PlayerNoLongerHasItem(PlayerNoLongerHasItem {
        client: 0,
        target: 0,
        item_id: 0xFFFFFFFF,
        amount: 301,
    })))).await;
    assert!(split_attempt.is_err());

    let c1_meseta = entity_gateway.get_character_meseta(&char1.id).await.unwrap();
    assert!(c1_meseta.0 == 300);
}

#[async_std::test]
async fn test_pick_up_stack_that_would_exceed_stack_limit() {
    let mut entity_gateway = InMemoryGateway::default();

    let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
    let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await;

    let mut p1_monomates = Vec::new();
    for _ in 0..6usize {
        p1_monomates.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Tool(
                    item::tool::Tool {
                        tool: item::tool::ToolType::Monomate,
                    }
                ),
            }).await.unwrap());
    }

    let mut p2_monomates = Vec::new();
    for _ in 0..6usize {
        p2_monomates.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Tool(
                    item::tool::Tool {
                        tool: item::tool::ToolType::Monomate,
                    }
                ),
            }).await.unwrap());
    }
    entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![p1_monomates])).await.unwrap();
    entity_gateway.set_character_inventory(&char2.id, &item::InventoryEntity::new(vec![p2_monomates])).await.unwrap();

    let mut ship = Box::new(ShipServerState::builder()
        .gateway(entity_gateway.clone())
        .build());
    log_in_char(&mut ship, ClientId(1), "a1", "a").await;
    log_in_char(&mut ship, ClientId(2), "a2", "a").await;

    join_lobby(&mut ship, ClientId(1)).await;
    join_lobby(&mut ship, ClientId(2)).await;

    create_room(&mut ship, ClientId(1), "room", "").await;
    join_room(&mut ship, ClientId(2), 0).await;

    ship.handle(ClientId(2), RecvShipPacket::Message(Message::new(GameMessage::PlayerDropItem(PlayerDropItem {
        client: 0,
        target: 0,
        unknown1: 0,
        map_area: 0,
        item_id: 0x210000,
        x: 0.0,
        y: 0.0,
        z: 0.0,
    })))).await.unwrap();

    let packets = ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
        client: 0,
        target: 0,
        item_id: 0x210000,
        map_area: 0,
        unknown: [0; 3]
    })))).await.unwrap();
    assert!(packets.len() == 0);

    let p1_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
    assert_eq!(p1_items.items.len(), 1);
    p1_items.items[0].with_stacked(|items| {
        assert_eq!(items.len(), 6);
    }).unwrap();
    let p2_items = entity_gateway.get_character_inventory(&char2.id).await.unwrap();
    assert_eq!(p2_items.items.len(), 0);
}

#[async_std::test]
async fn test_can_not_pick_up_meseta_when_full() {
    let mut entity_gateway = InMemoryGateway::default();

    let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
    let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await;

    entity_gateway.set_character_meseta(&char1.id, item::Meseta(999999)).await.unwrap();
    entity_gateway.set_character_meseta(&char2.id, item::Meseta(300)).await.unwrap();

    let mut ship = Box::new(ShipServerState::builder()
        .gateway(entity_gateway.clone())
        .build());
    log_in_char(&mut ship, ClientId(1), "a1", "a").await;
    log_in_char(&mut ship, ClientId(2), "a2", "a").await;

    join_lobby(&mut ship, ClientId(1)).await;
    join_lobby(&mut ship, ClientId(2)).await;

    create_room(&mut ship, ClientId(1), "room", "").await;
    join_room(&mut ship, ClientId(2), 0).await;

    ship.handle(ClientId(2), RecvShipPacket::Message(Message::new(GameMessage::DropCoordinates(DropCoordinates {
        client: 0,
        target: 0,
        item_id: 0xFFFFFFFF,
        map_area: 0,
        room: 0,
        x: 0.0,
        z: 0.0,
        amount: 23,
    })))).await.unwrap();

    ship.handle(ClientId(2), RecvShipPacket::Message(Message::new(GameMessage::PlayerNoLongerHasItem(PlayerNoLongerHasItem {
        client: 0,
        target: 0,
        item_id: 0xFFFFFFFF,
        amount: 23,
    })))).await.unwrap();

    let packets = ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
        client: 0,
        target: 0,
        item_id: 0x00810001,
        map_area: 0,
        unknown: [0; 3]
    })))).await.unwrap();
    assert!(packets.len() == 0);

    let c1_meseta = entity_gateway.get_character_meseta(&char1.id).await.unwrap();
    let c2_meseta = entity_gateway.get_character_meseta(&char2.id).await.unwrap();
    assert!(c1_meseta.0 == 999999);
    assert!(c2_meseta.0 == 277);
}

#[async_std::test]
async fn test_meseta_caps_at_999999_when_trying_to_pick_up_more() {
    let mut entity_gateway = InMemoryGateway::default();

    let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
    let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await;

    entity_gateway.set_character_meseta(&char1.id, item::Meseta(999998)).await.unwrap();
    entity_gateway.set_character_meseta(&char2.id, item::Meseta(300)).await.unwrap();

    let mut ship = Box::new(ShipServerState::builder()
        .gateway(entity_gateway.clone())
        .build());
    log_in_char(&mut ship, ClientId(1), "a1", "a").await;
    log_in_char(&mut ship, ClientId(2), "a2", "a").await;

    join_lobby(&mut ship, ClientId(1)).await;
    join_lobby(&mut ship, ClientId(2)).await;

    create_room(&mut ship, ClientId(1), "room", "").await;
    join_room(&mut ship, ClientId(2), 0).await;

    ship.handle(ClientId(2), RecvShipPacket::Message(Message::new(GameMessage::DropCoordinates(DropCoordinates {
        client: 0,
        target: 0,
        item_id: 0xFFFFFFFF,
        map_area: 0,
        room: 0,
        x: 0.0,
        z: 0.0,
        amount: 23,
    })))).await.unwrap();

    ship.handle(ClientId(2), RecvShipPacket::Message(Message::new(GameMessage::PlayerNoLongerHasItem(PlayerNoLongerHasItem {
        client: 0,
        target: 0,
        item_id: 0xFFFFFFFF,
        amount: 23,
    })))).await.unwrap();

    ship.handle(ClientId(1), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
        client: 0,
        target: 0,
        item_id: 0x00810001,
        map_area: 0,
        unknown: [0; 3]
    })))).await.unwrap();

    let c1_meseta = entity_gateway.get_character_meseta(&char1.id).await.unwrap();
    let c2_meseta = entity_gateway.get_character_meseta(&char2.id).await.unwrap();
    assert!(c1_meseta.0 == 999999);
    assert!(c2_meseta.0 == 277);
}

#[async_std::test]
async fn test_player_drops_partial_stack_and_other_player_picks_it_up() {
    let mut entity_gateway = InMemoryGateway::default();

    let (_user1, char1) = new_user_character(&mut entity_gateway, "a1", "a", 1).await;
    let (_user2, char2) = new_user_character(&mut entity_gateway, "a2", "a", 1).await;

    let mut monomates = Vec::new();
    for _ in 0..5usize {
        monomates.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Tool(
                    item::tool::Tool {
                        tool: item::tool::ToolType::Monomate,
                    }
                ),
            }).await.unwrap());
    }

    entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![monomates])).await.unwrap();

    let mut ship = Box::new(ShipServerState::builder()
        .gateway(entity_gateway.clone())
        .build());
    log_in_char(&mut ship, ClientId(1), "a1", "a").await;
    log_in_char(&mut ship, ClientId(2), "a2", "a").await;

    join_lobby(&mut ship, ClientId(1)).await;
    join_lobby(&mut ship, ClientId(2)).await;

    create_room(&mut ship, ClientId(1), "room", "").await;
    join_room(&mut ship, ClientId(2), 0).await;

    ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::DropCoordinates(DropCoordinates {
        client: 0,
        target: 0,
        item_id: 0x10000,
        map_area: 0,
        room: 0,
        x: 0.0,
        z: 0.0,
        amount: 2,
    })))).await.unwrap();

    ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::PlayerNoLongerHasItem(PlayerNoLongerHasItem {
        client: 0,
        target: 0,
        item_id: 0x10000,
        amount: 2,
    })))).await.unwrap();

    ship.handle(ClientId(2), RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::PickupItem(PickupItem {
        client: 0,
        target: 0,
        item_id: 0x10001,
        map_area: 0,
        unknown: [0; 3]
    })))).await.unwrap();

    let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
    assert_eq!(inventory_items.items.len(), 1);
    inventory_items.items[0].with_stacked(|items| {
        assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(),
                   vec![item::ItemEntityId(3), item::ItemEntityId(4), item::ItemEntityId(5)]);
    }).unwrap();

    let inventory_items = entity_gateway.get_character_inventory(&char2.id).await.unwrap();
    assert_eq!(inventory_items.items.len(), 1);
    inventory_items.items[0].with_stacked(|items| {
        assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(),
                   vec![item::ItemEntityId(1), item::ItemEntityId(2)]);
    }).unwrap();
}

/*
#[async_std::test]
async fn test_try_and_pick_up_individual_item_twice() {
    panic!()
}

#[async_std::test]
async fn test_try_and_pick_up_stacked_item_twice() {
    panic!()
}

#[async_std::test]
async fn test_try_and_pick_up_meseta_twice() {
    panic!()
}
*/