/**
 * This file is part of Pulse, licensed under the MIT License (MIT).
 *
 * Copyright (c) 2014 InspireNXE <http://inspirenxe.org/>
 *
 * 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 org.inspirenxe.server.network;

import java.net.InetSocketAddress;
import java.util.EnumMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;

import com.flowpowered.commons.ticking.TickingElement;
import com.flowpowered.networking.session.BasicSession;
import com.flowpowered.networking.session.Session;
import com.flowpowered.networking.util.AnnotatedMessageHandler;
import com.flowpowered.networking.util.AnnotatedMessageHandler.Handle;
import io.netty.channel.ChannelOption;
import org.inspirenxe.server.Game;
import org.inspirenxe.server.network.ChannelMessage.Channel;
import org.inspirenxe.server.network.message.handshake.HandshakeMessage;
import org.inspirenxe.server.network.message.login.LoginStartMessage;
import org.inspirenxe.server.network.protocol.LoginProtocol;

public class Network extends TickingElement {
    private static final int TPS = 20;
    private final Game game;
    private final GameNetworkServer server;
    private final AnnotatedMessageHandler handler = new AnnotatedMessageHandler(this);
    private final Map<Channel, ConcurrentLinkedQueue<ChannelMessage>> messageQueue = new EnumMap<>(Channel.class);

    public Network(Game game) {
        super("network", TPS);
        this.game = game;
        server = new GameNetworkServer(game);
        messageQueue.put(Channel.NETWORK, new ConcurrentLinkedQueue<ChannelMessage>());
    }

    @Override
    public void onStart() {
        game.getLogger().info("Starting network");
        final InetSocketAddress address = new InetSocketAddress(game.getConfiguration().getAddress(), game.getConfiguration().getPort());
        server.bind(address);
        game.getLogger().info("Listening on " + address);
    }

    @Override
    public void onTick(long l) {
        final Iterator<ChannelMessage> messages = getChannel(Channel.NETWORK);
        while (messages.hasNext()) {
            handler.handle(messages.next());
            messages.remove();
        }
    }

    @Override
    public void onStop() {
        game.getLogger().info("Stopping network");
        server.shutdown();
    }

    /**
     * Gets the {@link java.util.Iterator} storing the messages for the {@link ChannelMessage.Channel}
     *
     * @param c See {@link ChannelMessage}
     * @return The iterator
     */
    public Iterator<ChannelMessage> getChannel(ChannelMessage.Channel c) {
        return messageQueue.get(c).iterator();
    }

    /**
     * Offers a {@link ChannelMessage} to a queue mapped to {@link ChannelMessage.Channel}
     *
     * @param c See {@link ChannelMessage.Channel}
     * @param m See {@link ChannelMessage}
     */
    public void offer(ChannelMessage.Channel c, ChannelMessage m) {
        if (c == Channel.NETWORK) {
            handler.handle(m);
        } else {
            messageQueue.get(c).offer(m);
        }
    }

    @Handle
    private void handleHandshake(HandshakeMessage message) {
        switch (message.getState()) {
            case STATUS:
                //TODO Implement status protocol
                break;
            case LOGIN:
                game.getLogger().info("Handshake is LOGIN state, switching protocol");
                message.getSession().setProtocol(new LoginProtocol(game));
                game.getLogger().info("Protocol is now LOGIN state");
        }
    }

    @Handle
    private void handleLoginStart(LoginStartMessage message) {
        game.getLogger().info("Handling LoginStart");
    }
}


@@ -1352,24 +1352,47 @@
 a.util.Map;%0A
+import java.util.UUID;%0A
 import java.
@@ -1490,115 +1490,8 @@
 nt;%0A
-import com.flowpowered.networking.session.BasicSession;%0Aimport com.flowpowered.networking.session.Session;%0A
 impo
@@ -1625,47 +1625,8 @@
 le;%0A
-import io.netty.channel.ChannelOption;%0A
 impo
@@ -1901,22 +1901,154 @@
 ork.
-protocol.Login
+message.login.LoginSuccessMessage;%0Aimport org.inspirenxe.server.network.protocol.LoginProtocol;%0Aimport org.inspirenxe.server.network.protocol.Play
 Prot
@@ -2554,96 +2554,8 @@
 e);%0A
-        messageQueue.put(Channel.NETWORK, new ConcurrentLinkedQueue%3CChannelMessage%3E());%0A
     
@@ -2934,210 +2934,8 @@
 ) %7B%0A
-        final Iterator%3CChannelMessage%3E messages = getChannel(Channel.NETWORK);%0A        while (messages.hasNext()) %7B%0A            handler.handle(messages.next());%0A            messages.remove();%0A        %7D%0A
     
@@ -4376,51 +4376,274 @@
     
-game.getLogger().info(%22Handling LoginStart%22
+message.getSession().setUUID(new UUID(0, message.getUsername().hashCode()).toString());%0A        message.getSession().send(new LoginSuccessMessage(message.getSession().getUUID(), message.getUsername()));%0A        message.getSession().setProtocol(new PlayProtocol(game)
 );%0A 

Send client login success