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

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

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

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

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

    let mag = entity_gateway.create_item(
        item::NewItemEntity {
            item: item::ItemDetail::Mag(
                item::mag::Mag::baby_mag(0)
            ),
            location: item::ItemLocation::Inventory {
                character_id: char1.id,
                //equipped: true,
            }
        }).await.unwrap();

    let mut monomates = Vec::new();
    for _ in 0..7usize {
        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 equipped = item::EquippedEntity {
        weapon: None,
        armor: None,
        shield: None,
        unit: [None; 4],
        mag: Some(mag.id),
    };
    entity_gateway.set_character_equips(&char1.id, &equipped).await.unwrap();

    let mut inventory = Vec::new();
    inventory.push(mag.into());
    inventory.push(item::InventoryItemEntity::Stacked(monomates));
    entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(inventory)).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;

    for _ in 0..7usize {
        ship.handle(ClientId(1), &RecvShipPacket::Message(Message::new(GameMessage::PlayerFeedMag(PlayerFeedMag {
            client: 0,
            target: 0,
            mag_id: 0x10000,
            item_id: 0x10001,
        })))).await.unwrap().for_each(drop);
    }

    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| {
        match &item.item {
            item::ItemDetail::Mag(mag) => {
                println!("mag! {:?}", mag);
                assert!(mag.level() == 7);
                assert!(mag.def() == 5);
                assert!(mag.pow() == 2);
                assert!(mag.dex() == 0);
                assert!(mag.mind() == 0);
            }
            _ => panic!()
        }
    }).unwrap();
}

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

    let (_user1, mut char1) = new_user_character(&mut entity_gateway, "a1", "a").await;
    let (_user2, mut char2) = new_user_character(&mut entity_gateway, "a2", "a").await;
    char1.char_class = CharacterClass::RAmarl;
    char1.section_id = SectionID::Redria;
    entity_gateway.save_character(&char1).await.unwrap();
    char2.char_class = CharacterClass::FOmarl;
    char2.section_id = SectionID::Whitill;
    entity_gateway.save_character(&char2).await.unwrap();

    let mag = entity_gateway.create_item(
        item::NewItemEntity {
            item: item::ItemDetail::Mag(
                item::mag::Mag::baby_mag(0)
            ),
            location: item::ItemLocation::Inventory {
                character_id: char1.id,
            }
        }).await.unwrap();

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

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

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

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

    let inventory_items = entity_gateway.get_character_inventory(&char2.id).await.unwrap();
    assert_eq!(inventory_items.items.len(), 1);
    inventory_items.items[0].with_individual(|item| {
        match &item.item {
            item::ItemDetail::Mag(mag) => {
                assert!(mag.class == CharacterClass::FOmarl);
                assert!(mag.id == SectionID::Whitill);
            }
            _ => panic!()
        }
    }).unwrap();
}


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

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

    let mag = entity_gateway.create_item(
        item::NewItemEntity {
            item: item::ItemDetail::Mag(
                item::mag::Mag::baby_mag(0)
            ),
            location: item::ItemLocation::Inventory {
                character_id: char1.id,
            }
        }).await.unwrap();

    for _ in 0..1000usize {
        let fed_tool = entity_gateway.create_item(
            item::NewItemEntity {
                item: item::ItemDetail::Tool (
                    item::tool::Tool {
                        tool: item::tool::ToolType::Monomate,
                    }
                ),
                location: item::ItemLocation::FedToMag {
                    mag: mag.id,
                }
            }).await.unwrap();
        entity_gateway.feed_mag(&mag.id, &fed_tool.id).await.unwrap();
    }
    let mag_cell = entity_gateway.create_item(
        item::NewItemEntity {
            item: item::ItemDetail::Tool(
                item::tool::Tool {
                    tool: item::tool::ToolType::CellOfMag502,
                }
            ),
            location: item::ItemLocation::Inventory {
                character_id: char1.id,
            }
        }).await.unwrap();

    let equipped = item::EquippedEntity {
        weapon: None,
        armor: None,
        shield: None,
        unit: [None; 4],
        mag: Some(mag.id),
    };
    entity_gateway.set_character_equips(&char1.id, &equipped).await.unwrap();
    entity_gateway.set_character_inventory(&char1.id, &item::InventoryEntity::new(vec![mag, mag_cell])).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::PlayerUseItem(PlayerUseItem {
        client: 0,
        target: 0,
        item_id: 0x10001,
    })))).await.unwrap().for_each(drop);

    let inventory_items = entity_gateway.get_character_inventory(&char1.id).await.unwrap();
    inventory_items.items[0].with_individual(|item| {
        match &item.item {
            item::ItemDetail::Mag(mag) => {
                assert_eq!(mag.mag, item::mag::MagType::Soniti);
            }
            _ => panic!()
        }
    }).unwrap();
}