/*
 * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
 *
 * 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.parser.irc;

import com.dmdirc.parser.common.ParserError;
import com.dmdirc.parser.common.QueuePriority;
import com.dmdirc.parser.interfaces.ChannelClientInfo;
import com.dmdirc.parser.interfaces.ChannelInfo;
import com.dmdirc.parser.interfaces.callbacks.ChannelJoinListener;
import com.dmdirc.parser.interfaces.callbacks.ChannelSelfJoinListener;

/**
 * Process a channel join.
 */
public class ProcessJoin extends IRCProcessor {

    /**
     * Process a channel join.
     *
     * @param sParam Type of line to process ("JOIN")
     * @param token IRCTokenised line to process
     */
    @Override
    public void process(final String sParam, final String[] token) {
        if (sParam.equals("329")) {
            if (token.length < 5) { return; }
            final IRCChannelInfo iChannel = myParser.getChannel(token[3]);
            if (iChannel != null) {
                try {
                    iChannel.setCreateTime(Integer.parseInt(token[4]));
                } catch (NumberFormatException nfe) {
                    /* Oh well, not a normal ircd I guess */
                }
            }
        } else {
            // :nick!ident@host JOIN (:)#Channel
            if (token.length < 3) { return; }
            IRCClientInfo iClient;
            IRCChannelInfo iChannel;
            IRCChannelClientInfo iChannelClient;

            iClient = getClientInfo(token[0]);
            iChannel = myParser.getChannel(token[token.length - 1]);

            if (iClient == null) {
                iClient = new IRCClientInfo(myParser, token[0]);
                myParser.addClient(iClient);
            }
            // Check to see if we know the host/ident for this client to facilitate dmdirc Formatter
            if (iClient.getHostname().isEmpty()) { iClient.setUserBits(token[0], false); }
            if (iChannel != null) {
                if (iClient == myParser.getLocalClient()) {
                    try {
                        if (iChannel.getChannelClient(iClient) == null) {
                            // Otherwise we have a channel known, that we are not in?
                            myParser.callErrorInfo(new ParserError(ParserError.ERROR_FATAL, "Joined known channel that we wern't already on..", myParser.getLastLine()));
                        } else {
                            // If we are joining a channel we are already on, fake a part from
                            // the channel internally, and rejoin.
                            myParser.getProcessingManager().process("PART", token);
                        }
                    } catch (ProcessorNotFoundException e) { }
                } else if (iChannel.getChannelClient(iClient) != null) {
                    // Client joined channel that we already know of.
                    return;
                } else {
                    // This is only done if we are already the channel, and it isn't us that
                    // joined.
                    iChannelClient = iChannel.addClient(iClient);
                    callChannelJoin(iChannel, iChannelClient);
                    return;
                }
            }
            //if (iClient != myParser.getMyself()) {
                // callErrorInfo(new ParserError(ParserError.ERROR_WARNING, "Got join for channel ("+token[token.length-1]+") that I am not on. [Me: "+myParser.getMyself()+"]", myParser.getLastLine()));
            //}
            iChannel = new IRCChannelInfo(myParser, token[token.length - 1]);
            // Add ourself to the channel, this will be overridden by the NAMES reply
            iChannel.addClient(iClient);
            myParser.addChannel(iChannel);
            sendString("MODE " + iChannel.getName(), QueuePriority.LOW);

            callChannelSelfJoin(iChannel);
        }
    }


    /**
     * Callback to all objects implementing the ChannelJoin Callback.
     *
     * @see IChannelJoin
     * @param cChannel Channel Object
     * @param cChannelClient ChannelClient object for new person
     * @return true if a method was called, false otherwise
     */
    protected boolean callChannelJoin(final ChannelInfo cChannel, final ChannelClientInfo cChannelClient) {
        return getCallbackManager().getCallbackType(ChannelJoinListener.class).call(cChannel, cChannelClient);
    }

    /**
     * Callback to all objects implementing the ChannelSelfJoin Callback.
     *
     * @see IChannelSelfJoin
     * @param cChannel Channel Object
     * @return true if a method was called, false otherwise
     */
    protected boolean callChannelSelfJoin(final ChannelInfo cChannel) {
        return getCallbackManager().getCallbackType(ChannelSelfJoinListener.class).call(cChannel);
    }

    /**
     * What does this IRCProcessor handle.
     *
     * @return String[] with the names of the tokens we handle.
     */
    @Override
    public String[] handles() {
        return new String[]{"JOIN", "329"};
    }

    /**
     * Create a new instance of the IRCProcessor Object.
     *
     * @param parser IRCParser That owns this IRCProcessor
     * @param manager ProcessingManager that is in charge of this IRCProcessor
     */
    protected ProcessJoin(final IRCParser parser, final ProcessingManager manager) {
        super(parser, manager);
    }

}
