use libpso::packet::ship::*;
use crate::common::serverstate::ClientId;
use crate::common::leveltable::CharacterLevelTable;
use crate::ship::ship::{SendShipPacket, ShipError, Clients, Rooms};
use crate::ship::character::{FullCharacterBytesBuilder};
use crate::ship::location::{ClientLocation, LobbyId, RoomLobby, ClientLocationError, RoomId};
use crate::ship::packet;
use crate::ship::items::state::ItemState;
use crate::entity::gateway::EntityGateway;
use crate::ship::map::MapArea;

// this function needs a better home
pub fn block_selected(id: ClientId,
                      pkt: &MenuSelect,
                      clients: &mut Clients,
                      item_state: &ItemState,
                      level_table: &CharacterLevelTable)
                      -> Result<Vec<(ClientId, SendShipPacket)>, anyhow::Error> {
    let client = clients.get_mut(&id).ok_or(ShipError::ClientNotFound(id))?;
    client.block = pkt.item as usize - 1;

    let (level, stats) = level_table.get_stats_from_exp(client.character.char_class, client.character.exp);

    let inventory = item_state.get_character_inventory(&client.character).unwrap();
    let bank = item_state.get_character_bank(&client.character).unwrap();

    let fc = FullCharacterBytesBuilder::default()
        .character(&client.character)
        .stats(&stats)
        .level(level)
        .meseta(inventory.meseta)
        .inventory(inventory)
        .bank(bank)
        .keyboard_config(&client.character.keyboard_config.as_bytes())
        .gamepad_config(&client.character.gamepad_config.as_bytes())
        .symbol_chat(&client.settings.settings.symbol_chats)
        .tech_menu(&client.character.tech_menu.as_bytes())
        .option_flags(client.character.option_flags)
        .build();

    Ok(vec![
        (id, SendShipPacket::FullCharacter(Box::new(FullCharacter {
            character: fc,
        }))),
        (id, SendShipPacket::CharDataRequest(CharDataRequest {})),
        (id, SendShipPacket::LobbyList(LobbyList::new())),
    ])
}

pub fn send_player_to_lobby(id: ClientId,
                            _pkt: &CharData,
                            client_location: &mut ClientLocation,
                            clients: &Clients,
                            item_state: &ItemState,
                            level_table: &CharacterLevelTable)
                            -> Result<Vec<(ClientId, SendShipPacket)>, anyhow::Error> {
    let lobby = client_location.add_client_to_next_available_lobby(id, LobbyId(0)).map_err(|_| ShipError::TooManyClients)?;
    let join_lobby = packet::builder::lobby::join_lobby(id, lobby, client_location, clients, item_state, level_table)?;
    let addto = packet::builder::lobby::add_to_lobby(id, lobby, client_location, clients, item_state, level_table)?;
    let neighbors = client_location.get_client_neighbors(id).unwrap();
    Ok(vec![(id, SendShipPacket::JoinLobby(join_lobby))]
       .into_iter()
       .chain(neighbors.into_iter()
              .map(|c| (c.client, SendShipPacket::AddToLobby(addto.clone())))).collect())
}

#[allow(clippy::too_many_arguments)]
pub async fn change_lobby<EG: EntityGateway>(id: ClientId,
                                             requested_lobby: u32,
                                             client_location: &mut ClientLocation,
                                             clients: &Clients,
                                             item_state: &mut ItemState,
                                             level_table: &CharacterLevelTable,
                                             ship_rooms: &mut Rooms,
                                             entity_gateway: &mut EG)
                                             -> Result<Vec<(ClientId, SendShipPacket)>, anyhow::Error> {
    let client = clients.get(&id).ok_or(ShipError::ClientNotFound(id))?;
    let prev_area = client_location.get_area(id).map_err(|err| -> ClientLocationError {err.into()})?;
    match prev_area {
        RoomLobby::Lobby(old_lobby) => {
            if old_lobby.0 == requested_lobby as usize {
                return Ok(vec![(id, SendShipPacket::SmallDialog(SmallDialog::new("You are already in this Lobby!".into())))])
            }
        },
        RoomLobby::Room(old_room) => {
            if client_location.get_client_neighbors(id)?.is_empty() {
                ship_rooms[old_room.0] = None;
            }
            item_state.remove_character_from_room(&client.character);
        },
    }
    let leave_lobby = packet::builder::lobby::remove_from_lobby(id, client_location)?;
    let old_neighbors = client_location.get_client_neighbors(id).unwrap();
    let mut lobby = LobbyId(requested_lobby as usize);
    if client_location.add_client_to_lobby(id, lobby).is_err() {
        match prev_area {
            RoomLobby::Lobby(_lobby) => {
                let dialog = SmallDialog::new(String::from("Lobby is full."));
                return Ok(vec![(id, SendShipPacket::SmallDialog(dialog))])
            }
            RoomLobby::Room(_room) => {
                lobby = client_location.add_client_to_next_available_lobby(id, lobby).map_err(|_| ShipError::TooManyClients)?;
            }
        }
    }
    item_state.load_character(entity_gateway, &client.character).await?;
    let join_lobby = packet::builder::lobby::join_lobby(id, lobby, client_location, clients, item_state, level_table)?;
    let addto = packet::builder::lobby::add_to_lobby(id, lobby, client_location, clients, item_state, level_table)?;
    let neighbors = client_location.get_client_neighbors(id).unwrap();
    Ok(vec![(id, SendShipPacket::JoinLobby(join_lobby))]
        .into_iter()
        .chain(neighbors.into_iter()
            .map(|c| (c.client, SendShipPacket::AddToLobby(addto.clone()))))
        .chain(old_neighbors.into_iter()
            .map(|c| (c.client, SendShipPacket::LeaveLobby(leave_lobby.clone()))))
        .collect())
}

pub fn remove_from_lobby(id: ClientId,
                         client_location: &mut ClientLocation)
                         -> Result<Vec<(ClientId, SendShipPacket)>, anyhow::Error> {
    let area_client = client_location.get_local_client(id)?;
    let neighbors = client_location.get_client_neighbors(id)?;
    let leader = client_location.get_leader_by_client(id)?;
    let leave_lobby_pkt = SendShipPacket::LeaveLobby(LeaveLobby::new(area_client.local_client.id(), leader.local_client.id()));

    client_location.remove_client_from_area(id)?;
    Ok(neighbors.into_iter().map(|n| {
        (n.client, leave_lobby_pkt.clone())
    }).collect())
}

pub fn get_room_tab_info(id: ClientId,
                                pkt: &MenuDetail,
                                client_location: &mut ClientLocation,
                                clients: &Clients,
                                rooms: &mut Rooms)
                                -> Result<Vec<(ClientId, SendShipPacket)>, anyhow::Error> {
    let room_id = RoomId(pkt.item as usize);
    if let Some(_room) = rooms.get(pkt.item as usize).ok_or(ShipError::InvalidRoom(pkt.item))? {
        let mut room_info = String::new();
        let clients_in_room = client_location.get_clients_in_room(room_id).map_err(|err| -> ClientLocationError { err.into() })?;
        for client in clients_in_room {
            let cs = clients.get(&client.client).ok_or(ShipError::ClientNotFound(client.client))?;
            let gc = cs.user.guildcard;
            let name = &cs.character.name;
            let cc = cs.character.char_class;
            let leveltable = CharacterLevelTable::default();
            let lv = leveltable.get_level_from_exp(cc, cs.character.exp);
            let floor = cs.area.unwrap_or(MapArea::Pioneer2Ep1);

            room_info += format!("{} Lv{} {}\n{} {}\n", gc,lv,name,cc,floor).as_str();
        }
        Ok(vec![(id, SendShipPacket::SmallLeftDialog(SmallLeftDialog::new(room_info)))])
    } else  {
        Ok(vec![(id, SendShipPacket::SmallLeftDialog(SmallLeftDialog::new("Game is no longer active".into())))])
    }
}