use std::collections::HashMap;
use libpso::packet::ship::*;
use crate::common::serverstate::ClientId;
use crate::common::leveltable::CharacterLevelTable;
use crate::ship::ship::{SendShipPacket, ShipError, ClientState, Clients, Rooms};
use crate::ship::character::{CharacterBytesBuilder, FullCharacterBytesBuilder};
use crate::ship::location::{ClientLocation, LobbyId, RoomId, RoomLobby, MAX_ROOMS, ClientLocationError};
use crate::ship::packet;
use libpso::character::character;
use crate::ship::location::ClientLocationError::GetAreaError;

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

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

    let fc = FullCharacterBytesBuilder::new()
        .character(&client.character)
        .stats(&stats)
        .level(level)
        .inventory(&client.inventory)
        .key_config(&client.settings.settings.key_config)
        .joystick_config(&client.settings.settings.joystick_config)
        .symbol_chat(&client.settings.settings.symbol_chats)
        .tech_menu(&client.character.tech_menu.as_bytes())
        .build();

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

pub fn send_player_to_lobby(id: ClientId,
                            _pkt: &CharData,
                            client_location: &mut ClientLocation,
                            clients: &Clients,
                            level_table: &CharacterLevelTable)
                            -> Result<Vec<(ClientId, SendShipPacket)>, ShipError> {
    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, level_table)?;
    let addto = packet::builder::lobby::add_to_lobby(id, lobby, client_location, clients, 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())
}

pub fn change_lobby(id: ClientId,
                    requested_lobby: u32,
                    client_location: &mut ClientLocation,
                    clients: &Clients,
                    level_table: &CharacterLevelTable,
                    ship_rooms: &mut Rooms)
                    -> Result<Vec<(ClientId, SendShipPacket)>, ShipError> {
    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).unwrap().len() == 0 {
                ship_rooms[old_room.0] = None;
            }
        },
    }
    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 let Err(_) = client_location.add_client_to_lobby(id, lobby) {
            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)?;
                }
            }
    }
    let join_lobby = packet::builder::lobby::join_lobby(id, lobby, client_location, clients, level_table)?;
    let addto = packet::builder::lobby::add_to_lobby(id, lobby, client_location, clients, 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())
}