package chat.servlet;

import javax.servlet.http.HttpServlet;
import javax.servlet.ServletException;
import java.util.Properties;
import chat.*;

/** Allows users to add new rooms.
* At server startup this servlet is initialised.
* @author Sukhwinder Singh
*/
public class ManageChatServlet extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	ChatRoomList rooms = new ChatRoomList();
	Properties props = null;
	/** Creates an object of ChatRoomList and stores it in ServletContext
	*/
	public void init() throws ServletException {
		String mainRoom = getServletContext().getInitParameter("leagueName");
		addMainRoom(mainRoom, "");
		getServletContext().setAttribute("chatroomlist", rooms);
	}

	/**
	 * Adds main room
	 */
	public void addMainRoom(String roomName, String roomDescr) {
		String s = getServletContext().getInitParameter("maxNoOfMessages");
		int maxMessages = 25;
		if (s != null) {
			try	{
				maxMessages = Integer.parseInt(s);
			} catch (NumberFormatException nfe)	{
				
			}
		}
		ChatRoom room = new ChatRoom(roomName, roomDescr);
		room.setMaximumNoOfMessages(maxMessages);
		room.setMainRoom(true);
		rooms.addRoom(room);
		rooms.setMainRoom(roomName);
	}

	/** Called when servlet is being destroyed */

	public void destroy() {
		System.err.println("Destroying all rooms");
	}
}