/*
 * ClientState.java
 * 
 * This file is part of the Sorcix Java IRC Library (sIRC).
 * 
 * Copyright (C) 2008-2010 Vic Demuzere http://sorcix.com
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.sorcix.sirc;

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

/**
 * Contains information about an {@link IrcConnection}.
 * 
 * @author Sorcix
 * @since 1.1.0
 */
public final class ClientState {

	// TODO: Allow changing the username (sIRC@..)
	/** The list of channels. */
	private final Map<String, Channel> channels;
	/** The local user. */
	private User client;
	
	/**
	 * Creates a new ClientState.
	 */
	protected ClientState() {
		this.channels = new HashMap<String, Channel>();
	}
	
	/**
	 * Adds a channel to the channel list.
	 * 
	 * @param channel The channel to add.
	 */
	protected void addChannel(final Channel channel) {
		if (!this.channels.containsKey(channel.getName())) {
			this.channels.put(channel.getName(), channel);
		}
	}
	
	/**
	 * Retrieves a shared channel object from the channel list.
	 * 
	 * @param channel A channel object representing this channel.
	 * @return The channel, or null if this channel doesn't exist.
	 *         (The local user is not in that channel)
	 * @see #getChannel(String)
	 */
	protected Channel getChannel(final Channel channel) {
		return this.getChannel(channel.getName());
	}
	
	/**
	 * Retrieves a shared channel object from the channel list.
	 * 
	 * @param channel The channel name.
	 * @return The channel, or null if this channel doesn't exist.
	 *         (The local user is not in that channel)
	 */
	protected Channel getChannel(final String channel) {
		if (this.channels.containsKey(channel)) {
			return this.channels.get(channel);
		}
		return null;
	}
	
	/**
	 * Creates an iterator through all Channels.
	 * 
	 * @return an iterator through all Channels.
	 */
	public Iterator<Channel> getChannels() {
		return this.channels.values().iterator();
	}
	
	/**
	 * Retrieves the local {@link User}.
	 * 
	 * @return The local {@code User}.
	 */
	public User getClient() {
		return this.client;
	}
	
	/**
	 * Checks if given channel is in the channel list.
	 * 
	 * @param name The name of this channel.
	 * @return True if the channel is in the list, false otherwise.
	 */
	protected boolean hasChannel(final String name) {
		return this.channels.containsKey(name);
	}
	
	/**
	 * Remove all channels from the channel list.
	 */
	protected void removeAll() {
		this.channels.clear();
	}
	
	/**
	 * Removes a channel from the channel list.
	 * 
	 * @param channel The channel name.
	 */
	protected void removeChannel(final String channel) {
		if (this.channels.containsKey(channel)) {
			this.channels.remove(channel);
		}
	}
	
	/**
	 * Set the local {@link User}.
	 * 
	 * @param user The local {@code User}.
	 */
	protected void setClient(final User user) {
		this.client = user;
	}
}
@@ -2002,16 +2002,30 @@
 etName()
+.toLowerCase()
 , channe
@@ -2713,53 +2713,8 @@
 ) %7B%0A
-%09%09if (this.channels.containsKey(channel)) %7B%0A%09
 %09%09re
@@ -2747,28 +2747,23 @@
 nnel
-);%0A%09%09%7D%0A%09%09return null
+.toLowerCase())
 ;%0A%09%7D
@@ -3891,8 +3891,9 @@
 er;%0A%09%7D%0A%7D
+%0A

ClientState.getChannel should be case-insensitive