package apz.pirichat.server;
import java.util.Vector;

public class Room
{
    private String mName;
    private String mTopic;
    private String mFounder;
    private String mPassword;
    private Vector<String> mParticipants;

    public Room(String pName, String pTopic, String pFounder)
    {
        if (pName.length() == 0)
            throw new PiriZeroLengthException("pName");

        // Check founder

        addParticipant(pFounder);
    }

    public String getName()
    {
        return mName;
    }

    public String getTopic()
    {
        return mTopic;
    }

    public String setTopic(String pTopic)
    {
        if (pTopic == null)
            throw new PiriNullPointerException("pTopic");
        mTopic = pTopic;
    }

    public String getFounder()
    {
        return mFounder;
    }

    public boolean isUserInRoom(String pUserName)
    {

    }

    public String[] getParticipants()
    {
        ChatUser[] tempArray = new ChatUser[chatUser.size()];
        for(int i=0; i<chatUser.size(); i++)
            tempArray[i] = (ChatUser)chatUser.elementAt(i);
        return tempArray;
    }

    public void addParticipant(String pUserName) throws ServerException
    {
        if (getChatUserIndex(pUser.getUserName()) != (-1))
            throw new ServerException("User already in room");
        chatUser.add(pUser);
    }

    public void removeParticipant(String pUserName) throws ServerException
    {
        int tempIndex = getChatUserIndex(pUserName);
        if (tempIndex == (-1)) throw new ServerException("User not in room");
        chatUser.remove(tempIndex);
    }


    private int getChatUserIndex(String pUserName)
    {
        for(int i=0; i<chatUser.size(); i++)
            if (((ChatUser)chatUser.elementAt(i)).getUserName().equals(pUserName)) return i;
        return (-1);
    }
}
