/*
 * Copyright (c) 2006-2014 DMDirc Developers
 *
 * 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.dmdirc.addons.contactlist;

import com.dmdirc.DMDircMBassador;
import com.dmdirc.Query;
import com.dmdirc.events.ChannelUserAwayEvent;
import com.dmdirc.events.ChannelUserBackEvent;
import com.dmdirc.events.FrameClosingEvent;
import com.dmdirc.interfaces.GroupChat;
import com.dmdirc.interfaces.NicklistListener;
import com.dmdirc.parser.interfaces.ChannelClientInfo;

import java.util.Collection;

import net.engio.mbassy.listener.Handler;

/**
 * Listens for contact list related events.
 */
public class ContactListListener implements NicklistListener {

    /** The group chat this listener is for. */
    private final GroupChat groupChat;
    /** Event bus to register listeners with. */
    private final DMDircMBassador eventBus;

    /**
     * Creates a new ContactListListener for the specified group chat.
     *
     * @param groupChat The group chat to show a contact list for
     */
    public ContactListListener(final GroupChat groupChat) {
        this.groupChat = groupChat;
        this.eventBus = groupChat.getEventBus();
    }

    /**
     * Adds all necessary listeners for this contact list listener to function.
     */
    public void addListeners() {
        groupChat.addNicklistListener(this);
        eventBus.subscribe(this);
    }

    /**
     * Removes the listeners added by {@link #addListeners()}.
     */
    public void removeListeners() {
        groupChat.removeNicklistListener(this);
        eventBus.unsubscribe(this);
    }

    @Override
    public void clientListUpdated(final Collection<ChannelClientInfo> clients) {
        clients.forEach(this::clientAdded);
    }

    @Override
    public void clientListUpdated() {
        // Do nothing
    }

    @Override
    public void clientAdded(final ChannelClientInfo client) {
        final Query query = groupChat.getConnection().get()
                .getQuery(client.getClient().getNickname(), false);

        query.setIcon("query-" + client.getClient().getAwayState().name().toLowerCase());
    }

    @Override
    public void clientRemoved(final ChannelClientInfo client) {
        // Do nothing
    }

    @Handler
    public void handleUserAway(final ChannelUserAwayEvent event) {
        clientAdded(event.getUser());
    }

    @Handler
    public void handleUserBack(final ChannelUserBackEvent event) {
        clientAdded(event.getUser());
    }

    @Handler
    public void windowClosing(final FrameClosingEvent event) {
        removeListeners();
    }

}

@@ -1381,27 +1381,92 @@
 irc.
-interfaces.GroupCha
+events.NickListClientAddedEvent;%0Aimport com.dmdirc.events.NickListClientsChangedEven
 t;%0Ai
@@ -1493,32 +1493,25 @@
 erfaces.
-NicklistListener
+GroupChat
 ;%0Aimport
@@ -1564,38 +1564,8 @@
 o;%0A%0A
-import java.util.Collection;%0A%0A
 impo
@@ -1692,36 +1692,8 @@
 ner 
-implements NicklistListener 
 %7B%0A%0A 
@@ -2317,53 +2317,8 @@
 ) %7B%0A
-        groupChat.addNicklistListener(this);%0A
     
@@ -2473,56 +2473,8 @@
 ) %7B%0A
-        groupChat.removeNicklistListener(this);%0A
     
@@ -2513,32 +2513,31 @@
     %7D%0A%0A    @
-Override
+Handler
 %0A    public 
@@ -2537,34 +2537,37 @@
 public void 
-c
+handleC
 lient
-List
+s
 Updated(fina
@@ -2572,45 +2572,41 @@
 nal 
-Collection%3CChannelClientInfo%3E cli
+NickListClientsChangedEvent ev
 ent
-s
 ) %7B%0A
@@ -2617,15 +2617,24 @@
     
-clients
+event.getUsers()
 .for
@@ -2666,32 +2666,31 @@
     %7D%0A%0A    @
-Override
+Handler
 %0A    public 
@@ -2698,460 +2698,103 @@
 oid 
-clientListUpdated() %7B%0A        // Do nothing%0A    %7D%0A%0A    @Override%0A    public void clientAdded(final ChannelClientInfo client) %7B%0A        final Query query = groupChat.getConnection().get()%0A                .getQuery(client.getClient().getNickname(), false);%0A%0A        query.setIcon(%22query-%22 + client.getCli
+handleClientAdded(final NickListClientAddedEvent event) %7B%0A        clientAdded(ev
 ent
-()
 .get
-AwayState().name().toLowerCase());%0A    %7D%0A%0A    @Override%0A    public void clientRemoved(final ChannelClientInfo client) %7B%0A        // Do nothing
+User());
 %0A   
@@ -3157,10 +3157,299 @@
 %0A    %7D%0A%0A
+    private void clientAdded(final ChannelClientInfo client) %7B%0A        final Query query = groupChat.getConnection().get()%0A                .getQuery(client.getClient().getNickname(), false);%0A%0A        query.setIcon(%22query-%22 + client.getClient().getAwayState().name().toLowerCase());%0A    %7D%0A%0A
 %7D%0A

Convert ContactListListener to events.