use libpso::packet::ship::*;
use libpso::packet::messages::*;
use crate::common::serverstate::ClientId;
use crate::common::leveltable::CharacterLevelTable;
use crate::ship::ship::{SendShipPacket, ShipError, Rooms, Clients};
use crate::ship::location::{ClientLocation, RoomId, RoomLobby, ClientLocationError};
use crate::ship::packet::builder;
use crate::ship::room;
use crate::ship::items::state::ItemState;
use std::convert::{TryFrom};

pub fn create_room(id: ClientId,
                   create_room: &CreateRoom,
                   client_location: &mut ClientLocation,
                   clients: &mut Clients,
                   item_state: &mut ItemState,
                   level_table: &CharacterLevelTable,
                   rooms: &mut Rooms)
                   -> Result<Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send>, anyhow::Error> {
    let client = clients.get(&id).ok_or(ShipError::ClientNotFound(id))?;
    let level = level_table.get_level_from_exp(client.character.char_class, client.character.exp);
    match room::Difficulty::try_from(create_room.difficulty)? {
        room::Difficulty::Ultimate => {
            if level < 80 {
                return Ok(Box::new(vec![(id, SendShipPacket::SmallDialog(SmallDialog::new("You must be at least level 80 \nto create Ultimate rooms.".into())))].into_iter()))
            }
        },
        room::Difficulty::VeryHard => {
            if level < 40 {
                return Ok(Box::new(vec![(id, SendShipPacket::SmallDialog(SmallDialog::new("You must be at least level 40 \nto create Very Hard rooms.".into())))].into_iter()))
            }
        },
        room::Difficulty::Hard => {
            if level < 20 {
                return Ok(Box::new(vec![(id, SendShipPacket::SmallDialog(SmallDialog::new("You must be at least level 20 \nto create Hard rooms.".into())))].into_iter()))
            }
        },
        room::Difficulty::Normal => {},
    };

    let area = client_location.get_area(id).unwrap();
    let area_client = client_location.get_local_client(id).unwrap();
    let lobby_neighbors = client_location.get_client_neighbors(id).unwrap();
    let room_id = client_location.create_new_room(id).unwrap();
    let mut room = room::RoomState::from_create_room(create_room, client.character.section_id).unwrap();
    room.bursting = true;

    item_state.add_character_to_room(room_id, &client.character, area_client);

    let join_room = builder::room::join_room(id, clients, client_location, room_id, &room)?;
    rooms[room_id.0] = Some(room);

    let mut result: Box<dyn Iterator<Item=(ClientId, SendShipPacket)> + Send> = Box::new(
        vec![(id, SendShipPacket::JoinRoom(join_room))].into_iter()
    );
    if let Ok(leader) = client_location.get_area_leader(area) {
        let leave_lobby = SendShipPacket::LeaveLobby(LeaveLobby::new(area_client.local_client.id(), leader.local_client.id()));
        result = Box::new(result.chain(lobby_neighbors
                                       .into_iter()
                                       .map(move |c| {
                                           (c.client, leave_lobby.clone())
                                       })));
    }

    Ok(result)
}

pub fn room_name_request(id: ClientId,
                         client_location: &ClientLocation,
                         rooms: &Rooms)
                         -> Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send> {
    let area = client_location.get_area(id).unwrap();
    match area {
        RoomLobby::Room(room) => Box::new(vec![(id, SendShipPacket::RoomNameResponse(RoomNameResponse {name: rooms[room.0].as_ref().unwrap().name.clone()}))].into_iter()),
        RoomLobby::Lobby(_) => panic!()
    }
}

pub fn join_room(id: ClientId,
                 pkt: &MenuSelect,
                 client_location: &mut ClientLocation,
                 clients: &mut Clients,
                 item_state: &mut ItemState,
                 level_table: &CharacterLevelTable,
                 rooms: &mut Rooms)
                 -> Result<Box<dyn Iterator<Item=(ClientId, SendShipPacket)> + Send>, ShipError> {
    let client = clients.get(&id).ok_or(ShipError::ClientNotFound(id))?;
    let level = level_table.get_level_from_exp(client.character.char_class, client.character.exp);
    // let room = rooms.get(pkt.item as usize).ok_or(ShipError::InvalidRoom(pkt.item))?.as_ref().unwrap(); // clippy look what you made me do
    if let Some(room) = rooms.get(pkt.item as usize).ok_or(ShipError::InvalidRoom(pkt.item))? {

        match room.mode.difficulty() {
            room::Difficulty::Ultimate => {
                if level < 80 {
                    return Ok(Box::new(vec![(id, SendShipPacket::SmallDialog(SmallDialog::new("You must be at least level 80 \nto join Ultimate rooms.".into())))].into_iter()))
                }
            },
            room::Difficulty::VeryHard => {
                if level < 40 {
                    return Ok(Box::new(vec![(id, SendShipPacket::SmallDialog(SmallDialog::new("You must be at least level 40 \nto join Very Hard rooms.".into())))].into_iter()))
                }
            },
            room::Difficulty::Hard => {
                if level < 20 {
                    return Ok(Box::new(vec![(id, SendShipPacket::SmallDialog(SmallDialog::new("You must be at least level 20 \nto join Hard rooms.".into())))].into_iter()))
                }
            },
            _ => {},
        };

        let original_area = client_location.get_area(id).unwrap();
        let original_neighbors = client_location.get_client_neighbors(id).unwrap();
        if room.bursting {
            return Ok(Box::new(vec![(id, SendShipPacket::SmallDialog(SmallDialog::new("player is bursting\nplease wait".into())))].into_iter()))
        }
        let room_id = RoomId(pkt.item as usize);
        let original_room_clients = client_location.get_clients_in_room(room_id).map_err(|err| -> ClientLocationError { err.into() })?;
        client_location.add_client_to_room(id, room_id).unwrap(); // TODO: show room full error or whatever

        let client = clients.get(&id).ok_or(ShipError::ClientNotFound(id))?;
        let area_client = client_location.get_local_client(id).map_err(|err| -> ClientLocationError { err.into() })?;

        item_state.add_character_to_room(room_id, &client.character, area_client);

        let leader = client_location.get_room_leader(room_id).map_err(|err| -> ClientLocationError { err.into() })?;
        let join_room = builder::room::join_room(id, clients, client_location, room_id, room)?;
        let add_to = builder::room::add_to_room(id, client, &area_client, &leader, item_state, level_table, room_id)?;

        let room = rooms.get_mut(room_id.0).unwrap().as_mut().unwrap();
        room.bursting = true;

        let mut result: Box<dyn Iterator<Item=(ClientId, SendShipPacket)> + Send> = Box::new(
            vec![(id, SendShipPacket::JoinRoom(join_room))]
                .into_iter()
                .chain(original_room_clients.into_iter()
                    .map(move |c| (c.client, SendShipPacket::AddToRoom(add_to.clone())))
                ));

        if let Ok(leader) = client_location.get_area_leader(original_area) {
            let leave_lobby = SendShipPacket::LeaveLobby(LeaveLobby::new(area_client.local_client.id(), leader.local_client.id()));
            result = Box::new(result.chain(original_neighbors.into_iter()
                                        .map(move |c| (c.client, leave_lobby.clone()))))
        }

        Ok(result)
    } else {
        Ok(Box::new(vec![(id, SendShipPacket::SmallDialog(SmallDialog::new("Game is no longer active".into())))].into_iter()))
    }
}

pub fn done_bursting(id: ClientId,
                     client_location: &ClientLocation,
                     rooms: &mut Rooms)
                     -> Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send> {
    let area = client_location.get_area(id).unwrap();
    let mut rare_monster_list: Option<Vec<u16>> = None;
    if let RoomLobby::Room(room_id) = area {
        if let Some(room) = rooms.get_mut(room_id.0).unwrap().as_mut() {
            room.bursting = false;
            rare_monster_list = Some(room.maps.get_rare_monster_list());
        };
    }
    let area_client = client_location.get_local_client(id).unwrap(); // TODO: unwrap
    let mut result: Box<dyn Iterator<Item=(ClientId, SendShipPacket)> + Send> = Box::new(
        client_location.get_client_neighbors(id).unwrap().into_iter() // TODO: unwrap
             .flat_map(move |client| {
                 vec![
                     (client.client, SendShipPacket::Message(Message::new(GameMessage::BurstDone(BurstDone {
                         client: area_client.local_client.id(),
                         target: 0
                     })))),
                 ]
             })
    );

    // TODO: check how often `done_bursting` is called. ie: make sure it's only used when joining a room and not each time a player warps in a pipe
    if let Some(rare_list) = rare_monster_list {
        let rare_monster_packet = SendShipPacket::RareMonsterList(builder::room::build_rare_monster_list(rare_list));
        result = Box::new(result.chain(vec![(id, rare_monster_packet)])); // TODO: make sure we arent clobbering `result` here
    }

    result
}

pub fn request_room_list(id: ClientId,
                         client_location: &ClientLocation,
                         rooms: &Rooms)
                         -> Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send> {
    let active_room_list = rooms.iter()
        .enumerate()
        .filter_map(|(i, r)| {
            r.as_ref().map(|room| {
                RoomList {
                    menu_id: ROOM_MENU_ID,
                    item_id: i as u32,
                    difficulty: room.get_difficulty_for_room_list(),
                    players: client_location.get_clients_in_room(RoomId(i)).unwrap().len() as u8,
                    name: libpso::utf8_to_utf16_array!(room.name, 16),
                    episode: room.get_episode_for_room_list(),
                    flags: room.get_flags_for_room_list(),
                }
            })
        });
    let baseroom: RoomList = RoomList {
        menu_id: ROOM_MENU_ID,
        item_id: ROOM_MENU_ID,
        difficulty: 0x00,
        players: 0x00,
        name: libpso::utf8_to_utf16_array!("Room list menu", 16),
        episode: 0,
        flags: 0,
    };

    Box::new(vec![(id, SendShipPacket::RoomListResponse(RoomListResponse {
        baseroom,
        rooms: active_room_list.collect()
    }))].into_iter())
}

pub fn cool_62(id: ClientId,
               cool_62: &Like62ButCooler,
               client_location: &ClientLocation)
               -> Box<dyn Iterator<Item = (ClientId, SendShipPacket)> + Send> {
    let target = cool_62.flag as u8;
    let cool_62 = cool_62.clone();
    Box::new(client_location.get_client_neighbors(id).unwrap().into_iter()
             .filter(move |client| client.local_client.id() == target)
             .map(move |client| {
                 (client.client, SendShipPacket::Like62ButCooler(cool_62.clone()))
             }))
}