use elseware::common::serverstate::{ClientId, ServerState};
use elseware::entity::gateway::{EntityGateway, InMemoryGateway};
use elseware::common::leveltable::CharacterLevelTable;
use elseware::ship::ship::{ShipServerState, SendShipPacket, RecvShipPacket};
use elseware::ship::monster::MonsterType;
use elseware::ship::location::RoomId;
use elseware::ship::map::variant::{MapVariant, MapVariantMode};
use elseware::ship::map::maps::Maps;
use elseware::ship::map::area::MapArea;
use elseware::ship::map::enemy::MapEnemy;

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

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

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

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

    let mut ship = Box::new(ShipServerState::builder()
                            .gateway(entity_gateway.clone())
                            .map_builder(Box::new(|_room_mode, _event| {
                                Maps::new(
                                    vec![MapVariant::new(MapArea::Forest2, MapVariantMode::Online)],
                                    vec![Some(MapEnemy::new(MonsterType::Hildebear, MapArea::Forest2))],
                                    Vec::new(),
                                )
                            }))
                            .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::RequestExp(RequestExp {
        client: 0,
        target: 16,
        enemy_id: 0,
        client_id: 0,
        unused: 0,
        last_hitter: 1,
    })))).await.unwrap();

    ship.clients.with(ClientId(1), |client| Box::pin(async move {
        assert!(13 == client.character.exp);
    })).await.unwrap();
}

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

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

    let mut ship = Box::new(ShipServerState::builder()
                            .gateway(entity_gateway.clone())
                            .map_builder(Box::new(|_room_mode, _event| {
                                Maps::new(
                                    vec![MapVariant::new(MapArea::Forest2, MapVariantMode::Online)],
                                    vec![Some(MapEnemy::new(MonsterType::Hildebear, MapArea::Forest2))],
                                    Vec::new(),
                                )
                            }))
                            .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 levelup_pkt = ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::RequestExp(RequestExp {
        client: 0 as u8,
        target: 16,
        enemy_id: 0 as u16,
        client_id: 0,
        unused: 0,
        last_hitter: 1,
    })))).await.unwrap();

    assert!(matches!(levelup_pkt[1].1, SendShipPacket::Message(Message {msg: GameMessage::PlayerLevelUp(PlayerLevelUp {lvl: 1, ..})})));

    let leveltable = CharacterLevelTable::default();
    ship.clients.with(ClientId(1), |client| Box::pin(async move {
        assert!(leveltable.get_level_from_exp(client.character.char_class, client.character.exp) == 2)
    })).await.unwrap();
}

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

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

    let mut ship = Box::new(ShipServerState::builder()
                            .gateway(entity_gateway.clone())
                            .map_builder(Box::new(|_room_mode, _event| {
                                Maps::new(
                                    vec![MapVariant::new(MapArea::DarkFalz, MapVariantMode::Online)],
                                    vec![Some(MapEnemy::new(MonsterType::DarkFalz2, MapArea::DarkFalz))],
                                    Vec::new(),
                                )
                            }))
                            .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 levelup_pkt = ship.handle(ClientId(1), RecvShipPacket::Message(Message::new(GameMessage::RequestExp(RequestExp {
        client: 0 as u8,
        target: 16,
        enemy_id: 0 as u16,
        client_id: 0,
        unused: 0,
        last_hitter: 1,
    })))).await.unwrap();

    assert!(matches!(levelup_pkt[1].1, SendShipPacket::Message(Message {msg: GameMessage::PlayerLevelUp(PlayerLevelUp {lvl: 7, ..})})));

    ship.clients.with(ClientId(1), |client| Box::pin(async move {
        assert!(3000 == client.character.exp);
    })).await.unwrap();
}

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

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

    let mut ship = Box::new(ShipServerState::builder()
                            .gateway(entity_gateway.clone())
                            .map_builder(Box::new(|_room_mode, _event| {
                                Maps::new(
                                    vec![MapVariant::new(MapArea::Forest2, MapVariantMode::Online)],
                                    vec![Some(MapEnemy::new(MonsterType::Hildebear, MapArea::Forest2))],
                                    Vec::new(),
                                )
                            }))
                            .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::RequestExp(RequestExp {
        client: 0,
        target: 16,
        enemy_id: 0,
        client_id: 0,
        unused: 0,
        last_hitter: 1,
    })))).await.unwrap();

    ship.handle(ClientId(2), RecvShipPacket::Message(Message::new(GameMessage::RequestExp(RequestExp {
        client: 0,
        target: 16,
        enemy_id: 0,
        client_id: 0,
        unused: 0,
        last_hitter: 0,
    })))).await.unwrap();

    ship.clients.with(ClientId(1), |client| Box::pin(async move {
        assert_eq!(client.character.exp, 13);
    })).await.unwrap();
    ship.clients.with(ClientId(2), |client| Box::pin(async move {
        assert_eq!(client.character.exp, 10);
    })).await.unwrap();
}