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

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

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

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

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

    let item = entity_gateway.create_item(
        item::NewItemEntity {
            item: item::ItemDetail::Weapon(
                item::weapon::Weapon {
                    weapon: item::weapon::WeaponType::Vulcan,
                    grind: 0,
                    special: None,
                    attrs: [None, None, None],
                    tekked: true,
                    modifiers: Vec::new(),
                }
            ),
            location: item::ItemLocation::Bank {
                character_id: char1.id,
                name: item::BankName("".to_string())
            }
        }).await.unwrap();

    entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![item]), item::BankName("".into())).await.unwrap();

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

    let packets = ship.handle(ClientId(1), &RecvShipPacket::MenuSelect(MenuSelect {
        menu: BLOCK_MENU_ID,
        item: 1,
    })).await.unwrap().collect::<Vec<_>>();

    assert!(matches!(&packets[0], (_, SendShipPacket::FullCharacter(fc)) if fc.character.bank.items[0].data1[0..3] == [0x00, 0x08, 0x04] ));
}

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

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

    let mut bank = Vec::new();
    for _ in 0..3 {
        bank.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Weapon(
                    item::weapon::Weapon {
                        weapon: item::weapon::WeaponType::Vulcan,
                        grind: 0,
                        special: None,
                        attrs: [None, None, None],
                        tekked: true,
                        modifiers: Vec::new(),
                    }
                ),
                location: item::ItemLocation::Bank {
                character_id: char1.id,
                    name: item::BankName("".to_string())
                }
            }).await.unwrap());
    }

    entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(bank), item::BankName("".into())).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;

    let packets = ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
        client: 0,
        target: 0,
        unknown: 0,
    })))).await.unwrap().collect::<Vec<_>>();

    assert!(matches!(&packets[0], (_, SendShipPacket::BankItemList (bank_item_list))
                     if bank_item_list.item_count == 3
                     && bank_item_list.size == 0x18 * 3 + 0x14
                     && bank_item_list.items[0].data1[0..3] == [0x00, 0x08, 0x04]
                     && bank_item_list.items[1].data1[0..3] == [0x00, 0x08, 0x04]
                     && bank_item_list.items[2].data1[0..3] == [0x00, 0x08, 0x04]
    ));
}


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

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

    let mut monomates = Vec::new();
    for _ in 0..3usize {
        monomates.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Tool (
                    item::tool::Tool {
                        tool: item::tool::ToolType::Monomate,
                    }
                ),
                location: item::ItemLocation::Bank {
                    character_id: char1.id,
                    name: item::BankName("".to_string())
                }
            }).await.unwrap());
    }

    entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![monomates]), item::BankName("".into())).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;

    let packets = ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
        client: 0,
        target: 0,
        unknown: 0,
    })))).await.unwrap().collect::<Vec<_>>();

    assert!(matches!(&packets[0], (_, SendShipPacket::BankItemList (bank_item_list))
                     if bank_item_list.item_count == 1
                     && bank_item_list.size == 0x18 + 0x14
                     && bank_item_list.items[0].data1[0..3] == [0x03, 0x00, 0x00]
                     && bank_item_list.items[0].amount == 3
    ));
}


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

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

    let item1 = entity_gateway.create_item(
        item::NewItemEntity {
            item: item::ItemDetail::Weapon(
                item::weapon::Weapon {
                    weapon: item::weapon::WeaponType::Vulcan,
                    grind: 0,
                    special: None,
                    attrs: [None, None, None],
                    tekked: true,
                    modifiers: Vec::new(),
                }
            ),
            location: item::ItemLocation::Bank {
                character_id: char1.id,
                name: item::BankName("".to_string())
            }
        }).await.unwrap();
    let monomate = entity_gateway.create_item(
        item::NewItemEntity {
            item: item::ItemDetail::Tool (
                item::tool::Tool {
                    tool: item::tool::ToolType::Monomate,
                }
            ),
            location: item::ItemLocation::Bank {
                character_id: char1.id,
                name: item::BankName("".to_string())
            }
        }).await.unwrap();
    let item2 = entity_gateway.create_item(
        item::NewItemEntity {
            item: item::ItemDetail::Weapon(
                item::weapon::Weapon {
                    weapon: item::weapon::WeaponType::Calibur,
                    grind: 0,
                    special: None,
                    attrs: [None, None, None],
                    tekked: true,
                    modifiers: Vec::new(),
                }
            ),
            location: item::ItemLocation::Bank {
                character_id: char1.id,
                name: item::BankName("".to_string())
            }
        }).await.unwrap();

    let bank = vec![item::BankItemEntity::Individual(item1), vec![monomate].into(), item2.into()];
    entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(bank), item::BankName("".into())).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;

    let packets = ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
        client: 0,
        target: 0,
        unknown: 0,
    })))).await.unwrap().collect::<Vec<_>>();

    assert!(matches!(&packets[0], (_, SendShipPacket::BankItemList (bank_item_list))
                     if bank_item_list.item_count == 3
                     && bank_item_list.size == 0x18 * 3 + 0x14
                     && bank_item_list.items[0].data1[0..3] == [0x00, 0x02, 0x04]
                     && bank_item_list.items[1].data1[0..3] == [0x00, 0x08, 0x04]
                     && bank_item_list.items[2].data1[0..3] == [0x03, 0x00, 0x00]
    ));
}


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

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

    let item0 = 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,
                    modifiers: Vec::new(),
                }
            ),
            location: item::ItemLocation::Inventory {
                character_id: char1.id,
            }
        }).await.unwrap();
    let item1 = 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,
                    modifiers: Vec::new(),
                }
            ),
            location: item::ItemLocation::Inventory {
                character_id: char1.id,
            }
        }).await.unwrap();

    entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![item0, item1])).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::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
        client: 0,
        target: 0,
        unknown: 0,
    })))).await.unwrap().for_each(drop);

    let packets = ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
        client: 0,
        target: 0,
        item_id: 0x10001,
        action: 0,
        item_amount: 0,
        meseta_amount: 0,
        unknown: 0,
    })))).await.unwrap().collect::<Vec<_>>();

    assert!(packets.len() == 1);
    assert!(matches!(&packets[0], (ClientId(2), SendShipPacket::Message(Message {msg: GameMessage::PlayerNoLongerHasItem(player_no_longer_has_item)}))
                     if player_no_longer_has_item.item_id == 0x10001
                     && player_no_longer_has_item.amount == 0
    ));

    let bank_items = entity_gateway.get_character_bank(&char1.id, item::BankName("".into())).await.unwrap();
    assert_eq!(bank_items.items.len(), 1);
    bank_items.items[0].with_individual(|item| {
        assert_eq!(item.id, item::ItemEntityId(2));
    }).unwrap();
}

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

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

    let mut monomates = Vec::new();
    for _ in 0..3usize {
        monomates.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Tool(
                    item::tool::Tool {
                        tool: item::tool::ToolType::Monomate,
                    }
                ),
                location: item::ItemLocation::Inventory {
                    character_id: char1.id,
                }
            }).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::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
        client: 0,
        target: 0,
        unknown: 0,
    })))).await.unwrap().for_each(drop);

    let packets = ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
        client: 0,
        target: 0,
        item_id: 0x10000,
        action: 0,
        item_amount: 3,
        meseta_amount: 0,
        unknown: 0,
    })))).await.unwrap().collect::<Vec<_>>();

    assert!(packets.len() == 1);
    assert!(matches!(&packets[0], (ClientId(2), SendShipPacket::Message(Message {msg: GameMessage::PlayerNoLongerHasItem(player_no_longer_has_item)}))
                     if player_no_longer_has_item.item_id == 0x10000
                     && player_no_longer_has_item.amount == 3
    ));

    let bank_items = entity_gateway.get_character_bank(&char1.id, item::BankName("".into())).await.unwrap();
    assert_eq!(bank_items.items.len(), 1);
    bank_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)]);
    }).unwrap();
}

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

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

    let mut monomates = Vec::new();
    for _ in 0..3usize {
        monomates.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Tool(
                    item::tool::Tool {
                        tool: item::tool::ToolType::Monomate,
                    }
                ),
                location: item::ItemLocation::Inventory {
                    character_id: char1.id,
                }
            }).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::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
        client: 0,
        target: 0,
        unknown: 0,
    })))).await.unwrap().for_each(drop);

    let packets = ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
        client: 0,
        target: 0,
        item_id: 0x10000,
        action: 0,
        item_amount: 2,
        meseta_amount: 0,
        unknown: 0,
    })))).await.unwrap().collect::<Vec<_>>();

    assert!(packets.len() == 1);
    assert!(matches!(&packets[0], (ClientId(2), SendShipPacket::Message(Message {msg: GameMessage::PlayerNoLongerHasItem(player_no_longer_has_item)}))
                     if player_no_longer_has_item.item_id == 0x10000
                     && player_no_longer_has_item.amount == 2
    ));


    let bank_items = entity_gateway.get_character_bank(&char1.id, item::BankName("".into())).await.unwrap();
    assert_eq!(bank_items.items.len(), 1);
    bank_items.items[0].with_stacked(|items| {
        assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(),
                   vec![item::ItemEntityId(1), item::ItemEntityId(2)]);
    }).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)]);
    }).unwrap();
}


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

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

    let mut bank_monomates = Vec::new();
    let mut inventory_monomates = Vec::new();
    for _ in 0..2usize {
        inventory_monomates.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Tool(
                    item::tool::Tool {
                        tool: item::tool::ToolType::Monomate,
                    }
                ),
                location: item::ItemLocation::Inventory {
                    character_id: char1.id,
                }
            }).await.unwrap());

        bank_monomates.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Tool(
                    item::tool::Tool {
                        tool: item::tool::ToolType::Monomate,
                    }
                ),
                location: item::ItemLocation::Bank {
                    character_id: char1.id,
                    name: item::BankName("".into()),
                }
            }).await.unwrap());
    }

    entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![inventory_monomates])).await.unwrap();
    entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![bank_monomates]), item::BankName("".into())).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::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
        client: 0,
        target: 0,
        unknown: 0,
    })))).await.unwrap().for_each(drop);

    let packets = ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
        client: 0,
        target: 0,
        item_id: 0x10000,
        action: 0,
        item_amount: 2,
        meseta_amount: 0,
        unknown: 0,
    })))).await.unwrap().collect::<Vec<_>>();

    assert!(packets.len() == 1);
    assert!(matches!(&packets[0], (ClientId(2), SendShipPacket::Message(Message {msg: GameMessage::PlayerNoLongerHasItem(player_no_longer_has_item)}))
                     if player_no_longer_has_item.item_id == 0x10000
                     && player_no_longer_has_item.amount == 2
    ));

    let bank_items = entity_gateway.get_character_bank(&char1.id, item::BankName("".into())).await.unwrap();
    assert_eq!(bank_items.items.len(), 1);
    bank_items.items[0].with_stacked(|items| {
        assert_eq!(items.iter().map(|i| i.id).collect::<BTreeSet<_>>(),
                   vec![item::ItemEntityId(1), item::ItemEntityId(2), item::ItemEntityId(3), item::ItemEntityId(4)].into_iter().collect::<BTreeSet<_>>() );
    }).unwrap();
}

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

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

    let mut inventory_monomates = Vec::new();
    for _ in 0..2usize {
        inventory_monomates.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Tool(
                    item::tool::Tool {
                        tool: item::tool::ToolType::Monomate,
                    }
                ),
                location: item::ItemLocation::Inventory {
                    character_id: char1.id,
                }
            }).await.unwrap());
    }

    let mut bank_monomates = Vec::new();
    for _ in 0..10 {
        bank_monomates.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Tool(
                    item::tool::Tool {
                        tool: item::tool::ToolType::Monomate,
                    }
                ),
                location: item::ItemLocation::Bank {
                    character_id: char1.id,
                    name: item::BankName("".into()),
                }
            }).await.unwrap());
    }

    entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![inventory_monomates])).await.unwrap();
    entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![bank_monomates]), item::BankName("".into())).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::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
        client: 0,
        target: 0,
        unknown: 0,
    })))).await.unwrap().for_each(drop);

    let packets = ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
        client: 0,
        target: 0,
        item_id: 0x10000,
        action: 0,
        item_amount: 2,
        meseta_amount: 0,
        unknown: 0,
    })))).await;

    assert!(packets.is_err());

    let bank_items = entity_gateway.get_character_bank(&char1.id, item::BankName("".into())).await.unwrap();
    assert_eq!(bank_items.items.len(), 1);
    bank_items.items[0].with_stacked(|items| {
        assert_eq!(items.len(), 10);
    }).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)]);
    }).unwrap();
}

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

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

    let mut inventory = Vec::new();
    inventory.push(entity_gateway.create_item(
        item::NewItemEntity {
            item: item::ItemDetail::Weapon(
                item::weapon::Weapon {
                    weapon: item::weapon::WeaponType::Vulcan,
                    grind: 0,
                    special: None,
                    attrs: [None, None, None],
                    tekked: true,
                    modifiers: Vec::new(),
                }
            ),
            location: item::ItemLocation::Inventory {
                character_id: char1.id,
            }
        }).await.unwrap());

    let mut bank = Vec::new();
    for _ in 0..200usize {
        bank.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Weapon(
                    item::weapon::Weapon {
                        weapon: item::weapon::WeaponType::Vulcan,
                        grind: 0,
                        special: None,
                        attrs: [None, None, None],
                        tekked: true,
                        modifiers: Vec::new(),
                    }
                ),
                location: item::ItemLocation::Bank {
                    character_id: char1.id,
                    name: item::BankName("".to_string())
                }
            }).await.unwrap());
    }

    entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(inventory)).await.unwrap();
    entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(bank), item::BankName("".into())).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::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
        client: 0,
        target: 0,
        unknown: 0,
    })))).await.unwrap().for_each(drop);

    let packets = ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
        client: 0,
        target: 0,
        item_id: 0x10000,
        action: 0,
        item_amount: 0,
        meseta_amount: 0,
        unknown: 0,
    })))).await;

    assert!(packets.is_err());

    let bank_items = entity_gateway.get_character_bank(&char1.id, item::BankName("".into())).await.unwrap();
    assert_eq!(bank_items.items.len(), 200);

    let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
    assert_eq!(inventory_items.items.len(), 1);
    inventory_items.items[0].with_individual(|item| {
        assert_eq!(item.id, item::ItemEntityId(1));
    }).unwrap();
}

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

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

    let mut monomates = Vec::new();
    for _ in 0..2usize {
        monomates.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Tool(
                    item::tool::Tool {
                        tool: item::tool::ToolType::Monomate,
                    }
                ),
                location: item::ItemLocation::Inventory {
                    character_id: char1.id,
                }
            }).await.unwrap());
    }

    let mut full_bank = Vec::new();
    for _ in 0..200usize {
        full_bank.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Weapon(
                    item::weapon::Weapon {
                        weapon: item::weapon::WeaponType::Vulcan,
                        grind: 0,
                        special: None,
                        attrs: [None, None, None],
                        tekked: true,
                        modifiers: Vec::new(),
                    }
                ),
                location: item::ItemLocation::Bank {
                    character_id: char1.id,
                    name: item::BankName("".to_string())
                }
            }).await.unwrap());
    }

    entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![monomates])).await.unwrap();
    entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(full_bank), item::BankName("".into())).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::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
        client: 0,
        target: 0,
        unknown: 0,
    })))).await.unwrap().for_each(drop);

    let packets = ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
        client: 0,
        target: 0,
        item_id: 0x10000,
        action: 0,
        item_amount: 2,
        meseta_amount: 0,
        unknown: 0,
    })))).await;

    assert!(packets.is_err());

    let bank_items = entity_gateway.get_character_bank(&char1.id, item::BankName("".into())).await.unwrap();
    assert_eq!(bank_items.items.len(), 200);

    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)]);
    }).unwrap();
}

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

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

    let mut monomates = Vec::new();
    for _ in 0..2usize {
        monomates.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Tool(
                    item::tool::Tool {
                        tool: item::tool::ToolType::Monomate,
                    }
                ),
                location: item::ItemLocation::Inventory {
                    character_id: char1.id,
                }
            }).await.unwrap());
    }

    let mut bank_monomates = Vec::new();
    for _ in 0..2usize {
        bank_monomates.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Tool(
                    item::tool::Tool {
                        tool: item::tool::ToolType::Monomate,
                    }
                ),
                location: item::ItemLocation::Bank {
                    character_id: char1.id,
                    name: item::BankName("".to_string())
                }
            }).await.unwrap());
    }

    let mut almost_full_bank: Vec<item::BankItemEntity> = Vec::new();
    for _ in 0..199usize {
        almost_full_bank.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Weapon(
                    item::weapon::Weapon {
                        weapon: item::weapon::WeaponType::Vulcan,
                        grind: 0,
                        special: None,
                        attrs: [None, None, None],
                        tekked: true,
                        modifiers: Vec::new(),
                    }
                ),
                location: item::ItemLocation::Bank {
                    character_id: char1.id,
                    name: item::BankName("".to_string())
                }
            }).await.unwrap().into());
    }
    almost_full_bank.push(bank_monomates.into());

    entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![monomates])).await.unwrap();
    entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(almost_full_bank), item::BankName("".into())).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::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
        client: 0,
        target: 0,
        unknown: 0,
    })))).await.unwrap().for_each(drop);

    ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
        client: 0,
        target: 0,
        item_id: 0x10000,
        action: 0,
        item_amount: 2,
        meseta_amount: 0,
        unknown: 0,
    })))).await.unwrap().for_each(drop);

    let bank_items = entity_gateway.get_character_bank(&char1.id, item::BankName("".into())).await.unwrap();
    assert_eq!(bank_items.items.len(), 200);
    bank_items.items[199].with_stacked(|items| {
        assert_eq!(items.len(), 4);
    }).unwrap();

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

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

    let (user1, mut char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
    char1.meseta = 300;
    entity_gateway.save_character(&char1).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::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
        client: 0,
        target: 0,
        unknown: 0,
    })))).await.unwrap().for_each(drop);

    ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
        client: 0,
        target: 0,
        item_id: 0xFFFFFFFF,
        action: 0,
        item_amount: 0,
        meseta_amount: 23,
        unknown: 0,
    })))).await.unwrap().for_each(drop);

    let characters = entity_gateway.get_characters_by_user(&user1).await.unwrap();
    let char = characters[0].as_ref().unwrap();
    assert!(char.meseta == 277);
    assert!(char.bank_meseta == 23);
}

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

    let (user1, mut char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
    char1.meseta = 300;
    char1.bank_meseta = 999980;
    entity_gateway.save_character(&char1).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::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
        client: 0,
        target: 0,
        unknown: 0,
    })))).await.unwrap().for_each(drop);

    ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
        client: 0,
        target: 0,
        item_id: 0xFFFFFFFF,
        action: 0,
        item_amount: 0,
        meseta_amount: 23,
        unknown: 0,
    })))).await.unwrap().for_each(drop);

    let characters = entity_gateway.get_characters_by_user(&user1).await.unwrap();
    let char = characters[0].as_ref().unwrap();
    assert!(char.meseta == 300);
    assert!(char.bank_meseta == 999980);
}


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

    let (user1, mut char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
    char1.meseta = 300;
    char1.bank_meseta = 999999;
    entity_gateway.save_character(&char1).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::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
        client: 0,
        target: 0,
        unknown: 0,
    })))).await.unwrap().for_each(drop);

    ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
        client: 0,
        target: 0,
        item_id: 0xFFFFFFFF,
        action: 0,
        item_amount: 0,
        meseta_amount: 23,
        unknown: 0,
    })))).await.unwrap().for_each(drop);

    let characters = entity_gateway.get_characters_by_user(&user1).await.unwrap();
    let char = characters[0].as_ref().unwrap();
    assert!(char.meseta == 300);
    assert!(char.bank_meseta == 999999);
}


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

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

    let mut bank = Vec::new();
    bank.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,
                    modifiers: Vec::new(),
                }
            ),
            location: item::ItemLocation::Bank {
                character_id: char1.id,
                name: item::BankName("".to_string())
            }
        }).await.unwrap());

    entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(bank), item::BankName("".into())).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::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
        client: 0,
        target: 0,
        unknown: 0,
    })))).await.unwrap().for_each(drop);

    let packets = ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
        client: 0,
        target: 0,
        item_id: 0x20000,
        action: 1,
        item_amount: 0,
        meseta_amount: 0,
        unknown: 0,
    })))).await.unwrap().collect::<Vec<_>>();

    assert!(packets.len() == 1);
    assert!(matches!(&packets[0], (ClientId(2), SendShipPacket::Message(Message {msg: GameMessage::CreateItem(create_item)}))
                     if create_item.item_id == 0x20000
    ));


    let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
    assert_eq!(inventory_items.items.len(), 1);
    inventory_items.items[0].with_individual(|item| {
        assert_eq!(item.id, item::ItemEntityId(1));
    }).unwrap();
}

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

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

    let mut monomates = Vec::new();
    for _ in 0..3usize {
        monomates.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Tool(
                    item::tool::Tool {
                        tool: item::tool::ToolType::Monomate,
                    }
                ),
                location: item::ItemLocation::Bank {
                    character_id: char1.id,
                    name: item::BankName("".to_string())
                }
            }).await.unwrap());
    }

    entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![monomates]), item::BankName("".into())).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::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
        client: 0,
        target: 0,
        unknown: 0,
    })))).await.unwrap().for_each(drop);

    let packets = ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
        client: 0,
        target: 0,
        item_id: 0x20000,
        action: 1,
        item_amount: 3,
        meseta_amount: 0,
        unknown: 0,
    })))).await.unwrap().collect::<Vec<_>>();

    assert!(packets.len() == 1);
    assert!(matches!(&packets[0], (ClientId(2), SendShipPacket::Message(Message {msg: GameMessage::CreateItem(create_item)}))
                     if create_item.item_id == 0x10002
    ));

    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)]);
    }).unwrap();
}

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

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

    let mut monomates = Vec::new();
    for _ in 0..3usize {
        monomates.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Tool(
                    item::tool::Tool {
                        tool: item::tool::ToolType::Monomate,
                    }
                ),
                location: item::ItemLocation::Bank {
                    character_id: char1.id,
                    name: item::BankName("".into())
                }
            }).await.unwrap());
    }
    entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![monomates]), item::BankName("".into())).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::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
        client: 0,
        target: 0,
        unknown: 0,
    })))).await.unwrap().for_each(drop);

    let packets = ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
        client: 0,
        target: 0,
        item_id: 0x20000,
        action: 1,
        item_amount: 2,
        meseta_amount: 0,
        unknown: 0,
    })))).await.unwrap().collect::<Vec<_>>();

    assert!(packets.len() == 1);
    assert!(matches!(&packets[0], (ClientId(2), SendShipPacket::Message(Message {msg: GameMessage::CreateItem(create_item)}))
                     if create_item.item_id == 0x10002
    ));

    let bank_items = entity_gateway.get_character_bank(&char1.id, item::BankName("".into())).await.unwrap();
    assert_eq!(bank_items.items.len(), 1);
    bank_items.items[0].with_stacked(|items| {
        assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(),
                   vec![item::ItemEntityId(3)]);
    }).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)]);
    }).unwrap();
}

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

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

    let mut inventory_monomates = Vec::new();
    let mut bank_monomates = Vec::new();
    for _ in 0..2usize {
        inventory_monomates.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Tool(
                    item::tool::Tool {
                        tool: item::tool::ToolType::Monomate,
                    }
                ),
                location: item::ItemLocation::Inventory {
                    character_id: char1.id,
                }
            }).await.unwrap());

        bank_monomates.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Tool(
                    item::tool::Tool {
                        tool: item::tool::ToolType::Monomate,
                    }
                ),
                location: item::ItemLocation::Bank {
                    character_id: char1.id,
                    name: item::BankName("".into()),
                }
            }).await.unwrap());
    }

    entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![inventory_monomates])).await.unwrap();
    entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![bank_monomates]), item::BankName("".into())).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::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
        client: 0,
        target: 0,
        unknown: 0,
    })))).await.unwrap().for_each(drop);

    let packets = ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
        client: 0,
        target: 0,
        item_id: 0x20000,
        action: 1,
        item_amount: 2,
        meseta_amount: 0,
        unknown: 0,
    })))).await.unwrap().collect::<Vec<_>>();

    assert!(packets.len() == 1);
    assert!(matches!(&packets[0], (ClientId(2), SendShipPacket::Message(Message {msg: GameMessage::CreateItem(create_item)}))
                     if create_item.item_id == 0x10000
    ));

    let bank_items = entity_gateway.get_character_bank(&char1.id, item::BankName("".into())).await.unwrap();
    assert_eq!(bank_items.items.len(), 0);

    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::<BTreeSet<_>>(),
                   vec![item::ItemEntityId(1), item::ItemEntityId(2), item::ItemEntityId(3), item::ItemEntityId(4)].into_iter().collect::<BTreeSet<_>>());
    }).unwrap();
}

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

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

    let mut bank_monomates = Vec::new();
    for _ in 0..2usize {
        bank_monomates.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Tool(
                    item::tool::Tool {
                        tool: item::tool::ToolType::Monomate,
                    }
                ),
                location: item::ItemLocation::Bank {
                    character_id: char1.id,
                    name: item::BankName("".into()),
                }
            }).await.unwrap());
    }

    let mut inventory_monomates = Vec::new();
    for _ in 0..10usize {
        inventory_monomates.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Tool(
                    item::tool::Tool {
                        tool: item::tool::ToolType::Monomate,
                    }
                ),
                location: item::ItemLocation::Inventory {
                    character_id: char1.id,
                }
            }).await.unwrap());
    }

    entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![inventory_monomates])).await.unwrap();
    entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![bank_monomates]), item::BankName("".into())).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::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
        client: 0,
        target: 0,
        unknown: 0,
    })))).await.unwrap().for_each(drop);

    let packets = ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
        client: 0,
        target: 0,
        item_id: 0x20000,
        action: 1,
        item_amount: 2,
        meseta_amount: 0,
        unknown: 0,
    })))).await;

    assert!(packets.is_err());

    let bank_items = entity_gateway.get_character_bank(&char1.id, item::BankName("".into())).await.unwrap();
    assert_eq!(bank_items.items.len(), 1);
    bank_items.items[0].with_stacked(|items| {
        assert_eq!(items.iter().map(|i| i.id).collect::<Vec<_>>(),
                   vec![item::ItemEntityId(1), item::ItemEntityId(2)]);
    }).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(), 10);
    }).unwrap();
}

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

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

    let mut bank = Vec::new();
    bank.push(entity_gateway.create_item(
        item::NewItemEntity {
            item: item::ItemDetail::Weapon(
                item::weapon::Weapon {
                    weapon: item::weapon::WeaponType::Vulcan,
                    grind: 0,
                    special: None,
                    attrs: [None, None, None],
                    tekked: true,
                    modifiers: Vec::new(),
                }
            ),
            location: item::ItemLocation::Bank {
                character_id: char1.id,
                name: item::BankName("".to_string())
            }
        }).await.unwrap());

    let mut inventory = Vec::new();
    for _ in 0..30usize {
        inventory.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Weapon(
                    item::weapon::Weapon {
                        weapon: item::weapon::WeaponType::Vulcan,
                        grind: 0,
                        special: None,
                        attrs: [None, None, None],
                        tekked: true,
                        modifiers: Vec::new(),
                    }
                ),
                location: item::ItemLocation::Inventory {
                    character_id: char1.id,
                }
            }).await.unwrap());
    }

    entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(inventory)).await.unwrap();
    entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(bank), item::BankName("".into())).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::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
        client: 0,
        target: 0,
        unknown: 0,
    })))).await.unwrap().for_each(drop);

    let packets = ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
        client: 0,
        target: 0,
        item_id: 0x20000,
        action: 1,
        item_amount: 0,
        meseta_amount: 0,
        unknown: 0,
    })))).await;
    assert!(packets.is_err());

    let bank_items = entity_gateway.get_character_bank(&char1.id, item::BankName("".into())).await.unwrap();
    assert_eq!(bank_items.items.len(), 1);

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

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

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

    let mut monomates = Vec::new();
    for _ in 0..2usize {
        monomates.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Tool(
                    item::tool::Tool {
                        tool: item::tool::ToolType::Monomate,
                    }
                ),
                location: item::ItemLocation::Bank {
                    character_id: char1.id,
                    name: item::BankName("".to_string())
                }
            }).await.unwrap());
    }

    let mut inventory = Vec::new();
    for _ in 0..30usize {
        inventory.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Weapon(
                    item::weapon::Weapon {
                        weapon: item::weapon::WeaponType::Vulcan,
                        grind: 0,
                        special: None,
                        attrs: [None, None, None],
                        tekked: true,
                        modifiers: Vec::new(),
                    }
                ),
                location: item::ItemLocation::Inventory {
                    character_id: char1.id,
                }
            }).await.unwrap());
    }

    entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(inventory)).await.unwrap();
    entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![monomates]), item::BankName("".into())).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::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
        client: 0,
        target: 0,
        unknown: 0,
    })))).await.unwrap().for_each(drop);

    let packets = ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
        client: 0,
        target: 0,
        item_id: 0x20000,
        action: 1,
        item_amount: 2,
        meseta_amount: 0,
        unknown: 0,
    })))).await;

    assert!(packets.is_err());


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

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

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

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

    let mut bank_item = Vec::new();
    for _ in 0..2usize {
        bank_item.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Tool(
                    item::tool::Tool {
                        tool: item::tool::ToolType::Monomate,
                    }
                ),
                location: item::ItemLocation::Bank {
                    character_id: char1.id,
                    name: item::BankName("".to_string())
                }
            }).await.unwrap());
    }
    entity_gateway.set_character_bank(&char1.id, &item::BankEntity::new(vec![bank_item]), item::BankName("".into())).await.unwrap();

    let mut items = Vec::new();
    for i in 0..29usize {
        items.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Weapon(
                    item::weapon::Weapon {
                        weapon: item::weapon::WeaponType::Vulcan,
                        grind: 0,
                        special: None,
                        attrs: [None, None, None],
                        tekked: true,
                        modifiers: Vec::new(),
                    }
                ),
                location: item::ItemLocation::Inventory {
                    character_id: char1.id,
                }
            }).await.unwrap().into());
    }

    let mut item29 = Vec::new();
    for _ in 0..2usize {
        item29.push(entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Tool(
                    item::tool::Tool {
                        tool: item::tool::ToolType::Monomate,
                    }
                ),
                location: item::ItemLocation::Inventory {
                    character_id: char1.id,
                }
            }).await.unwrap());
    }
    items.push(item::InventoryItemEntity::Stacked(item29));

    entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(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;
    join_lobby(&mut ship, ClientId(1)).await;
    create_room(&mut ship, ClientId(1), "room", "").await;

    ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
        client: 0,
        target: 0,
        unknown: 0,
    })))).await.unwrap().for_each(drop);

    ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
        client: 0,
        target: 0,
        item_id: 0x20000,
        action: 1,
        item_amount: 2,
        meseta_amount: 0,
        unknown: 0,
    })))).await.unwrap().for_each(drop);

    let bank_items = entity_gateway.get_character_bank(&char1.id, item::BankName("".into())).await.unwrap();
    assert!(bank_items.items.len() == 0);

    let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
    assert!(inventory_items.items.len() == 30);
    match &inventory_items.items[29] {
        item::InventoryItemEntity::Stacked(items) => {
            assert_eq!(items.len(), 4);
        },
        _ => panic!(),
    }
}

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

    let (user1, mut char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
    char1.bank_meseta = 300;
    entity_gateway.save_character(&char1).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::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
        client: 0,
        target: 0,
        unknown: 0,
    })))).await.unwrap().for_each(drop);

    ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
        client: 0,
        target: 0,
        item_id: 0xFFFFFFFF,
        action: 1,
        item_amount: 0,
        meseta_amount: 23,
        unknown: 0,
    })))).await.unwrap().for_each(drop);

    let characters = entity_gateway.get_characters_by_user(&user1).await.unwrap();
    let char = characters[0].as_ref().unwrap();
    assert!(char.meseta == 23);
    assert!(char.bank_meseta == 277);
}

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

    let (user1, mut char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
    char1.meseta = 999980;
    char1.bank_meseta = 300;
    entity_gateway.save_character(&char1).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::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
        client: 0,
        target: 0,
        unknown: 0,
    })))).await.unwrap().for_each(drop);

    ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
        client: 0,
        target: 0,
        item_id: 0xFFFFFFFF,
        action: 1,
        item_amount: 0,
        meseta_amount: 23,
        unknown: 0,
    })))).await.unwrap().for_each(drop);

    let characters = entity_gateway.get_characters_by_user(&user1).await.unwrap();
    let char = characters[0].as_ref().unwrap();
    assert!(char.meseta == 999980);
    assert!(char.bank_meseta == 300);
}

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

    let (user1, mut char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
    char1.meseta = 999999;
    char1.bank_meseta = 300;
    entity_gateway.save_character(&char1).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::DirectMessage(DirectMessage::new(0, GameMessage::BankRequest(BankRequest {
        client: 0,
        target: 0,
        unknown: 0,
    })))).await.unwrap().for_each(drop);

    ship.handle(ClientId(1), &RecvShipPacket::DirectMessage(DirectMessage::new(0, GameMessage::BankInteraction(BankInteraction {
        client: 0,
        target: 0,
        item_id: 0xFFFFFFFF,
        action: 1,
        item_amount: 0,
        meseta_amount: 23,
        unknown: 0,
    })))).await.unwrap().for_each(drop);

    let characters = entity_gateway.get_characters_by_user(&user1).await.unwrap();
    let char = characters[0].as_ref().unwrap();
    assert!(char.meseta == 999999);
    assert!(char.bank_meseta == 300);
}