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

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

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

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

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

    let mut p1_inv = Vec::new();
    p1_inv.push(entity_gateway.create_item(
        item::NewItemEntity {
            item: item::ItemDetail::Armor(
                item::armor::Armor{
                    armor: item::armor::ArmorType::Frame,
                    dfp: 0,
                    evp: 0,
                    slots: 4,
                }),
        }).await.unwrap());

    p1_inv.push(entity_gateway.create_item(
        item::NewItemEntity {
            item: item::ItemDetail::Unit(
                item::unit::Unit{
                    unit: item::unit::UnitType::KnightPower,
                    modifier: None,
                }),
        }).await.unwrap());

    p1_inv.push(entity_gateway.create_item(
        item::NewItemEntity {
            item: item::ItemDetail::Unit(
                item::unit::Unit{
                    unit: item::unit::UnitType::KnightPower,
                    modifier: Some(item::unit::UnitModifier::Plus),
                }),
        }).await.unwrap());

    let equipped = item::EquippedEntity {
        weapon: None,
        armor: Some(p1_inv[0].id),
        shield: None,
        unit: [None; 4],
        mag: None,
    };
    entity_gateway.set_character_equips(&char1.id, &equipped).await.unwrap();
    entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap();

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

    ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::PlayerEquipItem(PlayerEquipItem {
        client: 0,
        target: 0,
        item_id: 0x10001,
        sub_menu: 9,
        unknown1: 0,
    })))).await.unwrap();

    // case when someone tries to send invalid submenu? submenu is 9-12 in normal gameplay
    ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::PlayerEquipItem(PlayerEquipItem {
        client: 0,
        target: 0,
        item_id: 0x10002,
        sub_menu: 14,
        unknown1: 0,
    })))).await.unwrap();

    let equips = entity_gateway.get_character_equips(&char1.id).await.unwrap();
    assert_eq!(equips.unit[0].unwrap(), item::ItemEntityId(2));
    assert_eq!(equips.unit[1].unwrap(), item::ItemEntityId(3));
    assert!(equips.unit[2].is_none());
    assert!(equips.unit[3].is_none());
}

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

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

    let mut p1_inv = Vec::new();
    p1_inv.push(entity_gateway.create_item(
        item::NewItemEntity {
            item: item::ItemDetail::Armor(
                item::armor::Armor{
                    armor: item::armor::ArmorType::Frame,
                    dfp: 0,
                    evp: 0,
                    slots: 4,
                }),
        }).await.unwrap());

    p1_inv.push(entity_gateway.create_item(
        item::NewItemEntity {
            item: item::ItemDetail::Unit(
                item::unit::Unit{
                    unit: item::unit::UnitType::KnightPower,
                    modifier: None,
                }),
        }).await.unwrap());

    p1_inv.push(entity_gateway.create_item(
        item::NewItemEntity {
            item: item::ItemDetail::Unit(
                item::unit::Unit{
                    unit: item::unit::UnitType::KnightPower,
                    modifier: Some(item::unit::UnitModifier::Plus),
                }),
        }).await.unwrap());

    let equipped = item::EquippedEntity {
        weapon: None,
        armor: Some(p1_inv[0].id),
        shield: None,
        unit: [Some(p1_inv[1].id), Some(p1_inv[2].id), None, None],
        mag: None,
    };
    entity_gateway.set_character_equips(&char1.id, &equipped).await.unwrap();
    entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(p1_inv)).await.unwrap();

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

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

    let equips = entity_gateway.get_character_equips(&char1.id).await.unwrap();
    assert!(equips.armor.is_none());
    assert!(equips.unit[0].is_none());
    assert!(equips.unit[1].is_none());
    assert!(equips.unit[2].is_none());
    assert!(equips.unit[3].is_none());
}

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

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

    let mut p1_inv = Vec::new();
    p1_inv.push(entity_gateway.create_item(
        item::NewItemEntity {
            item: item::ItemDetail::Armor(
                item::armor::Armor{
                    armor: item::armor::ArmorType::Frame,
                    dfp: 0,
                    evp: 0,
                    slots: 4,
                }),
        }).await.unwrap());

    p1_inv.push(entity_gateway.create_item(
        item::NewItemEntity {
            item: item::ItemDetail::Unit(
                item::unit::Unit{
                    unit: item::unit::UnitType::KnightPower,
                    modifier: None,
                }),
        }).await.unwrap());

    p1_inv.push(entity_gateway.create_item(
        item::NewItemEntity {
            item: item::ItemDetail::Unit(
                item::unit::Unit{
                    unit: item::unit::UnitType::KnightPower,
                    modifier: Some(item::unit::UnitModifier::Plus),
                }),
        }).await.unwrap());

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

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

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


    ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::SortItems(SortItems {
        client: 255,
        target: 255,
        item_ids: [0x10001u32, 0x10002, 0x10000, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 
        0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 
        0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF],
    })))).await.unwrap();

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