/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pw.yumc.Residence.chat;

import java.util.HashMap;
import java.util.Map;

import pw.yumc.Residence.ResidenceMain;

/**
 *
 * @author Administrator
 */
public class ChatManager {

    protected Map<String, ChatChannel> channelmap;
    protected final ResidenceMain plugin;

    public ChatManager(final ResidenceMain plugin) {
        this.plugin = plugin;
        channelmap = new HashMap<String, ChatChannel>();
    }

    public ChatChannel getChannel(final String channel) {
        return channelmap.get(channel);
    }

    public ChatChannel getPlayerChannel(final String player) {
        for (final ChatChannel chan : channelmap.values())
            if (chan.hasMember(player))
                return chan;
        return null;
    }

    public void removeFromChannel(final String player) {
        for (final ChatChannel chan : channelmap.values())
            if (chan.hasMember(player))
                chan.leave(player);
    }

    public void setChannel(final String player, final String channel) {
        this.removeFromChannel(player);
        if (!channelmap.containsKey(channel))
            channelmap.put(channel, new ChatChannel(plugin, channel));
        channelmap.get(channel).join(player);
    }

}
