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;

    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;

    let mut ship = 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;

    for _ in 0..3 {
        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;
    }

    let mut ship = 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;

    for _ in 0..3 {
        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;
    }

    let mut ship = 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;

    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;
    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;
    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;

    let mut ship = 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;

    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,
                slot: 0,
                equipped: false,
            }
        }).await;
    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,
                slot: 1,
                equipped: false,
            }
        }).await;

    let mut ship = 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 items = entity_gateway.get_items_by_character(&char1).await;
    let bank_item_ids = items.iter()
        .filter_map(|item| {
            if let item::ItemLocation::Bank {..} = item.location {
                Some(item.id)
            }
            else {
                None
            }
        })
        .collect::<Vec<_>>();
    assert!(bank_item_ids == vec![item::ItemEntityId(2)]);
}

#[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;

    for _ in 0..3 {
        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,
                    slot: 0,
                    equipped: false,
                }
            }).await;
    }

    let mut ship = 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 items = entity_gateway.get_items_by_character(&char1).await;
    let bank_item_ids = items.iter()
        .filter_map(|item| {
            if let item::ItemLocation::Bank {..} = item.location {
                Some(item.id)
            }
            else {
                None
            }
        })
        .collect::<Vec<_>>();
    assert!(bank_item_ids == vec![item::ItemEntityId(1), item::ItemEntityId(2), item::ItemEntityId(3)]);
}

#[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;

    for _ in 0..3 {
        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,
                    slot: 0,
                    equipped: false,
                }
            }).await;
    }

    let mut ship = 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 items = entity_gateway.get_items_by_character(&char1).await;
    let bank_item_ids = items.iter()
        .filter_map(|item| {
            if let item::ItemLocation::Bank {..} = item.location {
                Some(item.id)
            }
            else {
                None
            }
        })
        .collect::<Vec<_>>();
    assert!(bank_item_ids == vec![item::ItemEntityId(1), item::ItemEntityId(2)]);

    let inventory_item_ids = items.iter()
        .filter_map(|item| {
            if let item::ItemLocation::Inventory {..} = item.location {
                Some(item.id)
            }
            else {
                None
            }
        })
        .collect::<Vec<_>>();
    assert!(inventory_item_ids == vec![item::ItemEntityId(3)]);
}


#[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;

    for _ in 0..2 {
        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,
                    slot: 0,
                    equipped: false,
                }
            }).await;

        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;
    }

    let mut ship = 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 items = entity_gateway.get_items_by_character(&char1).await;
    let bank_item_ids = items.iter()
        .filter_map(|item| {
            if let item::ItemLocation::Bank {..} = item.location {
                Some(item.id)
            }
            else {
                None
            }
        })
        .collect::<Vec<_>>();
    assert!(bank_item_ids == vec![item::ItemEntityId(1), item::ItemEntityId(2), item::ItemEntityId(3), item::ItemEntityId(4)]);
}

#[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;

    for _ in 0..2 {
        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,
                    slot: 0,
                    equipped: false,
                }
            }).await;
    }

    for _ in 0..10 {
        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;
    }

    let mut ship = 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 items = entity_gateway.get_items_by_character(&char1).await;
    let bank_item_ids = items.iter()
        .filter_map(|item| {
            if let item::ItemLocation::Bank {..} = item.location {
                Some(item.id)
            }
            else {
                None
            }
        })
        .collect::<Vec<_>>();
    assert!(bank_item_ids.len() == 10);

    let inventory_item_ids = items.iter()
        .filter_map(|item| {
            if let item::ItemLocation::Inventory {..} = item.location {
                Some(item.id)
            }
            else {
                None
            }
        })
        .collect::<Vec<_>>();
    assert!(inventory_item_ids == vec![item::ItemEntityId(1), item::ItemEntityId(2)]);
}

#[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;

    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,
                slot: 0,
                equipped: false,
            }
        }).await;

    for _ in 0..200 {
        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;
    }

    let mut ship = 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 items = entity_gateway.get_items_by_character(&char1).await;
    let bank_item_ids = items.iter()
        .filter_map(|item| {
            if let item::ItemLocation::Bank {..} = item.location {
                Some(item.id)
            }
            else {
                None
            }
        })
        .collect::<Vec<_>>();
    assert!(bank_item_ids.len() == 200);

    let inventory_item_ids = items.iter()
        .filter_map(|item| {
            if let item::ItemLocation::Inventory {..} = item.location {
                Some(item.id)
            }
            else {
                None
            }
        })
        .collect::<Vec<_>>();
    assert!(inventory_item_ids == vec![item::ItemEntityId(1)]);
}

#[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;

    for _ in 0..2 {
        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,
                    slot: 0,
                    equipped: false,
                }
            }).await;
    }

    for _ in 0..200 {
        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;
    }

    let mut ship = 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 items = entity_gateway.get_items_by_character(&char1).await;
    let bank_item_ids = items.iter()
        .filter_map(|item| {
            if let item::ItemLocation::Bank {..} = item.location {
                Some(item.id)
            }
            else {
                None
            }
        })
        .collect::<Vec<_>>();
    assert!(bank_item_ids.len() == 200);

    let inventory_item_ids = items.iter()
        .filter_map(|item| {
            if let item::ItemLocation::Inventory {..} = item.location {
                Some(item.id)
            }
            else {
                None
            }
        })
        .collect::<Vec<_>>();
    assert!(inventory_item_ids == vec![item::ItemEntityId(1), item::ItemEntityId(2)]);
}

#[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;

    for _ in 0..2 {
        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,
                    slot: 0,
                    equipped: false,
                }
            }).await;
    }

    for _ in 0..199 {
        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;
    }

    for _ in 0..2 {
        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;
    }

    let mut ship = 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 items = entity_gateway.get_items_by_character(&char1).await;
    let bank_item_ids = items.iter()
        .filter_map(|item| {
            if let item::ItemLocation::Bank {..} = item.location {
                Some(item.id)
            }
            else {
                None
            }
        })
        .collect::<Vec<_>>();
    assert!(bank_item_ids.len() == 203);

    let inventory_item_ids = items.iter()
        .filter_map(|item| {
            if let item::ItemLocation::Inventory {..} = item.location {
                Some(item.id)
            }
            else {
                None
            }
        })
        .collect::<Vec<_>>();
    assert!(inventory_item_ids.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;

    let mut ship = 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;
    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;

    let mut ship = 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;
    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;

    let mut ship = 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;
    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;

    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;

    let mut ship = 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 items = entity_gateway.get_items_by_character(&char1).await;
    let inventory_item_ids = items.iter()
        .filter_map(|item| {
            if let item::ItemLocation::Inventory{..} = item.location {
                Some(item.id)
            }
            else {
                None
            }
        })
        .collect::<Vec<_>>();
    assert!(inventory_item_ids == vec![item::ItemEntityId(1)]);
}

#[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;

    for _ in 0..3 {
        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;
    }

    let mut ship = 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 items = entity_gateway.get_items_by_character(&char1).await;
    let inventory_item_ids = items.iter()
        .filter_map(|item| {
            if let item::ItemLocation::Inventory {..} = item.location {
                Some(item.id)
            }
            else {
                None
            }
        })
        .collect::<Vec<_>>();
    assert!(inventory_item_ids == vec![item::ItemEntityId(1), item::ItemEntityId(2), item::ItemEntityId(3)]);
}

#[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;

    for _ in 0..3 {
        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;
    }

    let mut ship = 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 items = entity_gateway.get_items_by_character(&char1).await;
    let bank_item_ids = items.iter()
        .filter_map(|item| {
            if let item::ItemLocation::Bank {..} = item.location {
                Some(item.id)
            }
            else {
                None
            }
        })
        .collect::<Vec<_>>();
    assert!(bank_item_ids == vec![item::ItemEntityId(3)]);

    let inventory_item_ids = items.iter()
        .filter_map(|item| {
            if let item::ItemLocation::Inventory {..} = item.location {
                Some(item.id)
            }
            else {
                None
            }
        })
        .collect::<Vec<_>>();
    assert!(inventory_item_ids == vec![item::ItemEntityId(1), item::ItemEntityId(2)]);
}

#[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;

    for _ in 0..2 {
        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,
                    slot: 0,
                    equipped: false,
                }
            }).await;

        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;
    }

    let mut ship = 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 items = entity_gateway.get_items_by_character(&char1).await;
    let inventory_item_ids = items.iter()
        .filter_map(|item| {
            if let item::ItemLocation::Inventory {..} = item.location {
                Some(item.id)
            }
            else {
                None
            }
        })
        .collect::<Vec<_>>();
    assert!(inventory_item_ids == vec![item::ItemEntityId(1), item::ItemEntityId(2), item::ItemEntityId(3), item::ItemEntityId(4)]);
}

#[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;

    for _ in 0..2 {
        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;
    }

    for _ in 0..10 {
        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,
                    slot: 0,
                    equipped: false,
                }
            }).await;
    }

    let mut ship = 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 items = entity_gateway.get_items_by_character(&char1).await;
    let bank_item_ids = items.iter()
        .filter_map(|item| {
            if let item::ItemLocation::Bank {..} = item.location {
                Some(item.id)
            }
            else {
                None
            }
        })
        .collect::<Vec<_>>();
    assert!(bank_item_ids == vec![item::ItemEntityId(1), item::ItemEntityId(2)]);

    let inventory_item_ids = items.iter()
        .filter_map(|item| {
            if let item::ItemLocation::Inventory {..} = item.location {
                Some(item.id)
            }
            else {
                None
            }
        })
        .collect::<Vec<_>>();
    assert!(inventory_item_ids.len() == 10);
}

#[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;

    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;

    for i in 0..30 {
        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,
                    slot: i,
                    equipped: false,
                }
            }).await;
    }

    let mut ship = 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 items = entity_gateway.get_items_by_character(&char1).await;
    let bank_item_ids = items.iter()
        .filter_map(|item| {
            if let item::ItemLocation::Bank {..} = item.location {
                Some(item.id)
            }
            else {
                None
            }
        })
        .collect::<Vec<_>>();
    assert!(bank_item_ids == vec![item::ItemEntityId(1)]);

    let inventory_item_ids = items.iter()
        .filter_map(|item| {
            if let item::ItemLocation::Inventory {..} = item.location {
                Some(item.id)
            }
            else {
                None
            }
        })
        .collect::<Vec<_>>();
    assert!(inventory_item_ids.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;

    for _ in 0..2 {
        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;
    }

    for i in 0..30 {
        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,
                    slot: i,
                    equipped: false,
                }
            }).await;
    }

    let mut ship = 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 items = entity_gateway.get_items_by_character(&char1).await;
    let bank_item_ids = items.iter()
        .filter_map(|item| {
            if let item::ItemLocation::Bank {..} = item.location {
                Some(item.id)
            }
            else {
                None
            }
        })
        .collect::<Vec<_>>();
    assert!(bank_item_ids == vec![item::ItemEntityId(1), item::ItemEntityId(2)]);

    let inventory_item_ids = items.iter()
        .filter_map(|item| {
            if let item::ItemLocation::Inventory {..} = item.location {
                Some(item.id)
            }
            else {
                None
            }
        })
        .collect::<Vec<_>>();
    assert!(inventory_item_ids.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;

    for _ in 0..2 {
        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;
    }

    for i in 0..29 {
        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,
                    slot: i,
                    equipped: false,
                }
            }).await;
    }

    for _ in 0..2 {
        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,
                    slot: 29,
                    equipped: false,
                }
            }).await;
    }

    let mut ship = 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 items = entity_gateway.get_items_by_character(&char1).await;
    let bank_item_ids = items.iter()
        .filter_map(|item| {
            if let item::ItemLocation::Bank {..} = item.location {
                Some(item.id)
            }
            else {
                None
            }
        })
        .collect::<Vec<_>>();
    assert!(bank_item_ids.len() == 0);

    let inventory_item_ids = items.iter()
        .filter_map(|item| {
            if let item::ItemLocation::Inventory {..} = item.location {
                Some(item.id)
            }
            else {
                None
            }
        })
        .collect::<Vec<_>>();
    assert!(inventory_item_ids.len() == 33);
}

#[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;

    let mut ship = 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;
    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;

    let mut ship = 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;
    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;

    let mut ship = 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;
    let char = characters[0].as_ref().unwrap();
    assert!(char.meseta == 999999);
    assert!(char.bank_meseta == 300);
}