package com.angkorteam.mbaas.server.xmpp;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;

import java.io.File;
import java.util.UUID;

/**
 * Created by socheat on 5/8/16.
 */
public class ServerHandler extends SimpleChannelInboundHandler<String> {

    public static final char SEPARATOR = ' ';

    public static final String COMMAND_CHAT = "CHAT";
    public static final String COMMAND_AUTHENTICATE_REQUEST = "AUTHENTICATE_REQUEST";
    public static final String COMMAND_AUTHENTICATE_RESPONSE = "CHAT";

    public static final String COMMAND_GROUP_INITIATE = "CHAT";
    public static final String COMMAND_GROUP_INVITE = "CHAT";
    public static final String COMMAND_GROUP_JOIN = "CHAT";
    public static final String COMMAND_GROUP_LEAVE = "CHAT";

    public static final String COMMAND_FRIEND_REQUEST = "FRIQ";
    public static final String COMMAND_FRIEND_REMOVE = "FRIR";
    public static final String COMMAND_FRIEND_REJECT = "FRIR";
    public static final String COMMAND_FRIEND_BLOCK = "FRIR";

    public static ChannelGroup CLIENTS = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);

    private boolean authenticated;

    private String login;

    private String userId;

    private String mobileId;

    public ServerHandler() {
    }

    @Override
    public void handlerAdded(ChannelHandlerContext context) throws Exception {
        Channel channel = context.channel();
        CLIENTS.add(channel);
        context.writeAndFlush(COMMAND_AUTHENTICATE_REQUEST);
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext context) throws Exception {
    }

    @Override
    protected void channelRead0(ChannelHandlerContext context, String msg) throws Exception {
        if (!authenticated) {

        }
    }

    private String[] parseCommand(String msg) {
        return null;
    }

    private ServerHandler getServerHandler(Channel channel) {
        return (ServerHandler) channel.pipeline().get("handler");
    }
}

@@ -571,32 +571,33 @@
 _CHAT = %22CHAT%22;%0A
+%0A
     public stati
@@ -726,28 +726,45 @@
 RESPONSE = %22
-CHAT
+AUTHENTICATE_RESPONSE
 %22;%0A%0A    publ
@@ -808,28 +808,38 @@
 INITIATE = %22
-CH
+GROUP_INITI
 AT
+E
 %22;%0A    publi
@@ -880,28 +880,36 @@
 P_INVITE = %22
-CHAT
+GROUP_INVITE
 %22;%0A    publi
@@ -952,20 +952,26 @@
 JOIN = %22
-CHAT
+GROUP_JOIN
 %22;%0A    p
@@ -1015,28 +1015,35 @@
 UP_LEAVE = %22
-CHAT
+GROUP_LEAVE
 %22;%0A%0A    publ
@@ -1094,17 +1094,27 @@
 T = %22FRI
-Q
+END_REQUEST
 %22;%0A    p
@@ -1163,25 +1163,34 @@
 EMOVE = %22FRI
-R
+END_REMOVE
 %22;%0A    publi
@@ -1239,17 +1239,26 @@
 T = %22FRI
-R
+END_REJECT
 %22;%0A    p
@@ -1310,17 +1310,25 @@
 K = %22FRI
-R
+END_BLOCK
 %22;%0A%0A    

test simple nio communication api