use libpso::packet::ship::*;
use libpso::packet::messages::*;
use crate::entity::gateway::EntityGateway;
use crate::entity::item::ItemDetail;
use crate::entity::item::tool::ToolType;
use crate::common::serverstate::ClientId;
use crate::common::leveltable::CharacterLevelTable;
use crate::ship::ship::{SendShipPacket, ShipError, Rooms, Clients, ItemDropLocation};
use crate::ship::location::{ClientLocation, ClientLocationError};
use crate::ship::map::{MapArea};
use crate::ship::items::{ItemManager, ClientItemId};
use crate::ship::packet::builder;

pub async fn request_exp<EG: EntityGateway>(id: ClientId,
                                            request_exp: &RequestExp,
                                            entity_gateway: &mut EG,
                                            client_location: &ClientLocation,
                                            clients: &mut Clients,
                                            rooms: &mut Rooms,
                                            level_table: &CharacterLevelTable)
                                            -> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError> {
    let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;
    let area_client = client_location.get_local_client(id).map_err(|err| -> ClientLocationError { err.into() })?;
    let room_id = client_location.get_room(id).map_err(|err| -> ClientLocationError { err.into() })?;
    let room = rooms.get_mut(room_id.0)
        .ok_or_else(|| ShipError::InvalidRoom(room_id.0 as u32))?
        .as_mut()
        .ok_or_else(|| ShipError::InvalidRoom(room_id.0 as u32))?;

    let monster = room.maps.enemy_by_id(request_exp.enemy_id as usize)?;
    let monster_stats = room.monster_stats.get(&monster.monster).unwrap();

    let exp_gain = if request_exp.last_hitter == 1 {
        monster_stats.exp
    }
    else {
        ((monster_stats.exp as f32) * 0.8) as u32
    };

    let clients_in_area = client_location.get_clients_in_room(room_id).map_err(|err| -> ClientLocationError { err.into() })?;
    let gain_exp_pkt = builder::message::character_gained_exp(area_client, exp_gain);
    let mut exp_pkts: Box<dyn Iterator<Item = _> + Send> = Box::new(clients_in_area.clone().into_iter()
        .map(move |c| {
            (c.client, SendShipPacket::Message(Message::new(GameMessage::GiveCharacterExp(gain_exp_pkt.clone()))))
        }));

    let before_level = level_table.get_level_from_exp(client.character.char_class, client.character.exp);
    let after_level = level_table.get_level_from_exp(client.character.char_class, client.character.exp + exp_gain);
    let level_up = before_level != after_level;

    if level_up {
        let (_, before_stats) = level_table.get_stats_from_exp(client.character.char_class, client.character.exp);
        let (after_level, after_stats) = level_table.get_stats_from_exp(client.character.char_class, client.character.exp + exp_gain);

        let level_up_pkt = builder::message::character_leveled_up(area_client, after_level, before_stats, after_stats);
        exp_pkts = Box::new(exp_pkts.chain(clients_in_area.into_iter()
            .map(move |c| {
                (c.client, SendShipPacket::Message(Message::new(GameMessage::PlayerLevelUp(level_up_pkt.clone()))))
            })))
    }

    client.character.exp += exp_gain;
    entity_gateway.save_character(&client.character).await;

    Ok(exp_pkts)
}

pub async fn player_drop_item<EG>(id: ClientId,
                            player_drop_item: &PlayerDropItem,
                            entity_gateway: &mut EG,
                            client_location: &ClientLocation,
                            clients: &mut Clients,
                            rooms: &mut Rooms,
                            item_manager: &mut ItemManager)
                            -> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError>
where
    EG: EntityGateway
{
    let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;
    let room_id = client_location.get_room(id).map_err(|err| -> ClientLocationError { err.into() })?;
    let room = rooms.get_mut(room_id.0)
        .ok_or_else(|| ShipError::InvalidRoom(room_id.0 as u32))?
        .as_mut()
        .ok_or_else(|| ShipError::InvalidRoom(room_id.0 as u32))?;
    let area = MapArea::from_value(&room.mode.episode(), player_drop_item.map_area)?;
    item_manager.player_drop_item_on_shared_floor(entity_gateway, &client.character, ClientItemId(player_drop_item.item_id), (area, player_drop_item.x, player_drop_item.y, player_drop_item.z)).await?;
    let clients_in_area = client_location.get_clients_in_room(room_id).map_err(|err| -> ClientLocationError { err.into() })?;
    let pdi = player_drop_item.clone();
    Ok(Box::new(clients_in_area.into_iter()
                .map(move |c| {
                    (c.client, SendShipPacket::Message(Message::new(GameMessage::PlayerDropItem(pdi.clone()))))
                })))
}

pub fn drop_coordinates(id: ClientId,
                            drop_coordinates: &DropCoordinates,
                            client_location: &ClientLocation,
                            clients: &mut Clients,
                            rooms: &Rooms)
                            -> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError>
{
    let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;
    let room_id = client_location.get_room(id).map_err(|err| -> ClientLocationError { err.into() })?;
    let room = rooms.get(room_id.0)
        .ok_or_else(|| ShipError::InvalidRoom(room_id.0 as u32))?
        .as_ref()
        .ok_or_else(|| ShipError::InvalidRoom(room_id.0 as u32))?;

    client.item_drop_location = Some(ItemDropLocation {
        map_area: MapArea::from_value(&room.mode.episode(), drop_coordinates.map_area)?,
        x: drop_coordinates.x,
        z: drop_coordinates.z,
        item_id: ClientItemId(drop_coordinates.item_id),
    });

    Ok(Box::new(None.into_iter()))
}

pub async fn split_item_stack<EG>(id: ClientId,
                                  no_longer_has_item: &PlayerNoLongerHasItem,
                                  entity_gateway: &mut EG,
                                  client_location: &ClientLocation,
                                  clients: &mut Clients,
                                  item_manager: &mut ItemManager)
                                  -> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError>
where
    EG: EntityGateway
{
    let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;
    let area_client = client_location.get_local_client(id).map_err(|err| -> ClientLocationError { err.into() })?;
    let room_id = client_location.get_room(id).map_err(|err| -> ClientLocationError { err.into() })?;
    let drop_location = client.item_drop_location.ok_or(ShipError::ItemDropLocationNotSet)?;

    if drop_location.item_id.0 != no_longer_has_item.item_id {
        return Err(ShipError::DropInvalidItemId(no_longer_has_item.item_id));
    }

    if no_longer_has_item.item_id == 0xFFFFFFFF {
        let dropped_meseta = item_manager.player_drops_meseta_on_shared_floor(entity_gateway, &mut client.character, drop_location, no_longer_has_item.amount as u32).await?;

        let dropped_meseta_pkt = builder::message::drop_split_meseta_stack(area_client, &dropped_meseta)?;
        client.item_drop_location = None;

        let clients_in_area = client_location.get_clients_in_room(room_id).map_err(|err| -> ClientLocationError { err.into() })?;
        Ok(Box::new(clients_in_area.into_iter()
                    .map(move |c| {
                        (c.client, SendShipPacket::Message(Message::new(GameMessage::DropSplitStack(dropped_meseta_pkt.clone()))))
                    })))
    }
    else {
        let dropped_item = item_manager.player_drops_partial_stack_on_shared_floor(entity_gateway, &client.character, drop_location.item_id, drop_location, no_longer_has_item.amount as usize).await?;

        let dropped_item_pkt = builder::message::drop_split_stack(area_client, dropped_item)?;
        client.item_drop_location = None;

        let clients_in_area = client_location.get_clients_in_room(room_id).map_err(|err| -> ClientLocationError { err.into() })?;
        Ok(Box::new(clients_in_area.into_iter()
                    .map(move |c| {
                        (c.client, SendShipPacket::Message(Message::new(GameMessage::DropSplitStack(dropped_item_pkt.clone()))))
                    })))
    }
}

pub fn update_player_position(id: ClientId,
                            message: &Message,
                            clients: &mut Clients,
                            client_location: &ClientLocation,
                            rooms: &Rooms)
                            -> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError> {
    let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;
    if let Ok(room_id) = client_location.get_room(id).map_err(|err| -> ClientLocationError { err.into() }) {
        let room = rooms.get(room_id.0)
            .ok_or_else(|| ShipError::InvalidRoom(room_id.0 as u32))?
            .as_ref()
            .ok_or_else(|| ShipError::InvalidRoom(room_id.0 as u32))?;
        
        match &message.msg {
            GameMessage::PlayerChangedMap(p) => {client.x = p.x; client.y = p.y; client.z = p.z;},
            GameMessage::PlayerChangedMap2(p) => {client.area = MapArea::from_value(&room.mode.episode(), p.map_area).ok();},
            GameMessage::TellOtherPlayerMyLocation(p) => {client.x = p.x; client.y = p.y; client.z = p.z; client.area = MapArea::from_value(&room.mode.episode(), p.map_area).ok();},
            GameMessage::PlayerWarpingToFloor(p) => {client.area = MapArea::from_value(&room.mode.episode(), p.area as u16).ok();},
            GameMessage::PlayerTeleported(p) => {client.x = p.x; client.y = p.y; client.z = p.z;},
            GameMessage::PlayerStopped(p) => {client.x = p.x; client.y = p.y; client.z = p.z;},
            GameMessage::PlayerLoadedIn(p) => {client.x = p.x; client.y = p.y; client.z = p.z;},
            GameMessage::PlayerWalking(p) => {client.x = p.x; client.z = p.z;},
            GameMessage::PlayerRunning(p) => {client.x = p.x; client.z = p.z;},
            GameMessage::PlayerWarped(p) => {client.x = p.x; client.y = p.y;},
            // GameMessage::PlayerChangedFloor(p) => {client.area = MapArea::from_value(&room.mode.episode(), p.map).ok();},
            GameMessage::InitializeSpeechNpc(p) => {client.x = p.x; client.z = p.z;}
            _ => {},
        }
    } else {}
    let m = message.clone();
    Ok(Box::new(client_location.get_client_neighbors(id).unwrap().into_iter()
                    .map(move |client| {
                        (client.client, SendShipPacket::Message(m.clone()))
                    })))
}

pub async fn charge_attack<EG>(id: ClientId,
                        charge: &ChargeAttack,
                        clients: &mut Clients,
                        entity_gateway: &mut EG)
                        -> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError>
where
    EG: EntityGateway
{
    let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;
    if client.character.meseta >= charge.meseta {
        client.character.meseta -= charge.meseta;
        entity_gateway.save_character(&client.character).await;
        Ok(Box::new(None.into_iter()))
    } else {
        Err(ShipError::NotEnoughMeseta(id, client.character.meseta))
    }
}

pub async fn use_item<EG>(id: ClientId,
                          player_use_tool: &PlayerUseItem,
                          entity_gateway: &mut EG,
                          _client_location: &ClientLocation,
                          clients: &mut Clients,
                          item_manager: &mut ItemManager)
                          -> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, ShipError>
where
    EG: EntityGateway
{
    let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;

    let item_used_type = item_manager.player_consumes_tool(entity_gateway, &client.character, ClientItemId(player_use_tool.item_id), 1).await?;

    match item_used_type {
        ItemDetail::Weapon(_w) => {
            // something like when items are used to combine/transform them?
            //_ => {}
        },
        ItemDetail::Tool(t) => {
            match t.tool {
                ToolType::PowerMaterial => {
                    client.character.materials.power += 1;
                    entity_gateway.save_character(&client.character).await;
                },
                ToolType::MindMaterial => {
                    client.character.materials.mind += 1;
                    entity_gateway.save_character(&client.character).await;
                },
                ToolType::EvadeMaterial => {
                    client.character.materials.evade += 1;
                    entity_gateway.save_character(&client.character).await;
                },
                ToolType::DefMaterial => {
                    client.character.materials.def += 1;
                    entity_gateway.save_character(&client.character).await;
                },
                ToolType::LuckMaterial => {
                    client.character.materials.luck += 1;
                    entity_gateway.save_character(&client.character).await;
                },
                ToolType::HpMaterial => {
                    client.character.materials.hp += 1;
                    entity_gateway.save_character(&client.character).await;
                },
                ToolType::TpMaterial => {
                    client.character.materials.tp += 1;
                    entity_gateway.save_character(&client.character).await;
                },
                _ => {}
            }
        }
        _ => {}
    }

    Ok(Box::new(None.into_iter()))
}