//   Course Number: COSC 650 Data Comm/Networks
//    Program Name: JavaChat
//  Project Number: 2
//         Authors: Brian Hoffman
//                  Mike McKinney
//                  Javier Carrasco
//     Description:

// Java Core Packages
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;

// Jave Extension Packages 
import javax.swing.*;
import javax.swing.border.*;

public class ChatDialogMap
{
    private HashMap roomMap;

    /**
     * ------------------------------------------------------------------------
     * default constructor 
     * ------------------------------------------------------------------------
     */
    public ChatDialogMap()
    {
        roomMap = new HashMap();
    }
    
    /**
     * ------------------------------------------------------------------------
     * roomExists - returns true if a given room exists in the map and false 
     *              otherwise
     * ------------------------------------------------------------------------
     */
    public boolean roomExists(String name)
    {
        return roomMap.containsKey(name);
    }
    
    /**
     * ------------------------------------------------------------------------
     * isEmpty - retuns true if the map is empty and false otherwise
     * ------------------------------------------------------------------------
     */
    public boolean isEmpty()
    {
        return roomMap.isEmpty();
    }
    
    /**
     * ------------------------------------------------------------------------
     * getRoom - returns a reference to the ChatroomDialog with the given name
     * ------------------------------------------------------------------------
     */
    public ChatroomDialog getRoom(String name)
    {
        return (ChatroomDialog) roomMap.get(name);
    }

    /**
     * ------------------------------------------------------------------------
     * addRoom - adds a room with the given name and ChatroomDialog to the map.
     *           Returns true if successful and false if not.
     * ------------------------------------------------------------------------
     */
    public synchronized boolean addRoom(String name, ChatroomDialog newDialog)
    {
        if (roomMap.containsKey(name)) return false;
        roomMap.put(name, newDialog);
        return true;
    }
    
    /**
     * ------------------------------------------------------------------------
     * removeRoom - removes the room with the specified name from the map. If
     *              it fails to find the room it does nothing
     * ------------------------------------------------------------------------
     */
    public synchronized void removeRoom(String name)
    {
        roomMap.remove(name);
    }

    /**
     * ------------------------------------------------------------------------
     *  disposeDialogs - calls the dispose method of each of the ChatDialogs in 
     *                   the map causing them to close. It then removes them
     *                   from the map. 
     * ------------------------------------------------------------------------
     */
    public synchronized void disposeDialogs()
    {
        Object [] keyArray;

        if (roomMap.isEmpty()) return; 
        keyArray = roomMap.keySet().toArray();
        for (int i=0; i<keyArray.length; i++)
        {
            getRoom( (String) keyArray[i] ).dispose();
        }
        roomMap.clear();        
    }
    
    /**
     * ------------------------------------------------------------------------
     *  clearMap - clears the map of all ChatroomDialogs
     * ------------------------------------------------------------------------
     */
    public synchronized void clearMap()
    {
        roomMap.clear();
    }
}
