package com.mogo.telematic.server.netty;

import static com.mogo.telematic.MogoProtocolMsg.IDENTITY_REGIST;

import android.text.TextUtils;
import android.util.Log;

import com.mogo.telematic.MogoProtocolMsg;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

@ChannelHandler.Sharable
public class MogoProtocolServerHandler extends SimpleChannelInboundHandler<MogoProtocolMsg> {
    private static final String TAG = "MogoServerHandler";
    private NettyServerListener mListener;

    public MogoProtocolServerHandler(NettyServerListener listener) {
        this.mListener = listener;
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {}

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx,
                                Throwable cause) {
        Log.e(TAG, "ProtocolServerHandler$exception msg is:" + cause.getMessage());
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MogoProtocolMsg msg) throws Exception {
        if (msg.getProtocolType() == MogoProtocolMsg.IDENTITY_REGIST) {
            String sn = new String(msg.getBody());
            NettyTcpServer.getInstance().putChannelSN(ctx.channel().id(), sn);
            if (mListener != null) {
                mListener.onReceiveClientSign(ctx.channel(), sn);
            }
        } else {
            if (mListener != null) {
                mListener.onMessageResponseServer(msg, ctx.channel());
            }
        }
    }

    /**
     * 连接成功
     *
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        Log.d(TAG, "channelActive");
        NettyTcpServer.getInstance().addChannel(ctx.channel());
        if (mListener != null) {
            mListener.onChannelConnect(ctx.channel());
        }
        String sn = NettyTcpServer.sSERVER_SN;
        if (!TextUtils.isEmpty(sn)) {
            byte[] signByteArr = sn.getBytes();
            MogoProtocolMsg msg = new MogoProtocolMsg(IDENTITY_REGIST, signByteArr.length, signByteArr);
            NettyTcpServer.getInstance().sendMsgToSpecifiedClient(msg, ctx.channel(), future -> {
                if (future.isSuccess()) {
                    Log.d(TAG, "Netty Server端注册身份成功！");
                } else {
                    Log.d(TAG, "Netty Server端注册身份不成功！");
                }
            });
        }
//        NettyTcpServer.getInstance().setConnectStatus(true);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        Log.w(TAG, "channelInactive");
//        NettyTcpServer.getInstance().setConnectStatus(false);
        NettyTcpServer.getInstance().removeChannelSN(ctx.channel().id());
        NettyTcpServer.getInstance().removeChannel(ctx.channel());
        if (mListener != null) {
            mListener.onChannelDisConnect(ctx.channel());
        }
    }
}
