package chat;

import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.Iterator;

/**
* This class is used to manipulate and store ChatRoom objects.
* It provides methods to store and retrieve ChatRoom objects
* in this <code>ChatRoomList</code>.
*@author Sukhwinder Singh(ssruprai@hotmail.com)
*/
public class ChatRoomList
{
	/**
	* Stores all the ChatRoom objects
	*/
	private Map<String, ChatRoom> roomList;
	private String mainRoomName;

	/**
	*/
	public ChatRoomList() {
		roomList = new HashMap<String, ChatRoom>();
	}
	/**
	* adds new chat room object to a list of Rooms.
	* @param room ChatRoom object
	* @return void
	*/
	public synchronized void addRoom(ChatRoom room)	{
		roomList.put(room.getName(), room);
	}
	
	/**
	* Used to remove a ChatRoom object from the
	* list of ChatRooms.
	* @param name is a String object is the name of the
	* room to be removed from the list of rooms.
	* @return void
	*/
	public synchronized void removeRoom(String name)
	{
		roomList.remove(name);
	}
	
	/** Returns a ChatRoom object
	* @param name is the name of the ChatRoom object to be returned.
	* @return ChatRoom object.
	*/
	public ChatRoom getRoom(String name) {
		return roomList.get(name);
	}

	public ChatRoom getMainRoom() {
		return roomList.get(mainRoomName);
	}

	public void setMainRoom(String name) {
		mainRoomName = name;
	}

	/** Finds the room of chatter having this name.
	* @param name is the name of the Chatter object.
	* @return ChatRoom object.
	*/
	public ChatRoom getRoomOfChatter(String name) {
		ChatRoom[] rooms = this.getRoomListArray();
		for (int i = 0; i < rooms.length; i++) {
			boolean chatterexists = rooms[i].chatterExists(name);
			if (chatterexists) {
				return rooms[i];
			}
		}
		return null;
	}
	
	/** Returns a Set containing all the ChatRoom objects
	* @return Set
	*/
	
	public Set<Map.Entry<String, ChatRoom>> getRoomList() {
		return roomList.entrySet();
	}
	
	/** returns an array containing all ChatRoom objects
	* @return chat.ChatRoom[]
	*/
	public ChatRoom[] getRoomListArray() {
		ChatRoom[] roomListArray = new ChatRoom[roomList.size()];
		Set<Map.Entry<String, ChatRoom>> roomlist = getRoomList();
		Iterator<Map.Entry<String, ChatRoom>> roomlistit = roomlist.iterator();
		int i = 0;
		while(roomlistit.hasNext())	{
			Map.Entry<String, ChatRoom> me = roomlistit.next();
			me.getKey();
			roomListArray[i] = (ChatRoom)me.getValue();
			i++;
		}
		return roomListArray;
	}
	
	/**
	* searches each ChatRoom for existance of a chatter.
	* @param nickname Name of the chatter to find.
	* @return boolean
	*/
	public boolean chatterExists(String nickname) {
		boolean chatterexists = false;
		ChatRoom[] rooms = this.getRoomListArray();
		for (int i = 0; i < rooms.length; i++) {
			chatterexists = rooms[i].chatterExists(nickname);
			if (chatterexists) {
				break;
			}
		}
		return chatterexists;
	}
}