package com.cm.central.control.handler;

import com.alibaba.fastjson.JSONObject;
import com.cm.central.control.manager.SocketServerManager;
import com.cm.socket.enums.SocketCodeEnum;
import com.cm.socket.enums.SocketTypeMessageEnum;
import com.cm.socket.pojo.Message;
import com.cm.socket.pojo.SocketResult;
import com.cm.socket.service.ISocketService;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: MessageServerHandler
 * @Description: 消息服务处理器
 * @Author: WangGeng
 * @Date: 2020/7/4 17:11
 * @Version: 1.0
 **/
public class SocketServerHandler extends SimpleChannelInboundHandler<Message> {

    private static final Logger LOG = LoggerFactory.getLogger(SocketServerHandler.class);
    private ISocketService socketClientPongServiceImpl;
    private ISocketService socketClientExceptionServiceImpl;
    private ISocketService socketClientLoginServiceImpl;
    private ISocketService socketClientInfoServiceImpl;

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        LOG.debug("有客户端建立连接");
        ctx.fireChannelActive();
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, Message message) throws Exception {
        if (message.getType() == SocketTypeMessageEnum.MESSAGE_TYPE_PING.getType()) {
            socketClientPongServiceImpl.readMessage(channelHandlerContext, message);
        } else if (message.getType() == SocketTypeMessageEnum.MESSAGE_TYPE_CLIENT_LOGIN.getType()) {
            socketClientLoginServiceImpl.readMessage(channelHandlerContext, message);
        } else if (message.getType() == SocketTypeMessageEnum.MESSAGE_TYPE_EXCEPTION.getType()) {
            socketClientExceptionServiceImpl.readMessage(channelHandlerContext, message);
        } else if (message.getType() == SocketTypeMessageEnum.MESSAGE_TYPE_CLIENT_INFO.getType()) {
            socketClientInfoServiceImpl.readMessage(channelHandlerContext, message);
        } else {
            SocketResult<String> socketResult = new SocketResult<>();
            socketResult.setCode(SocketCodeEnum.ERROR.getCode());
            socketResult.setErrorMsg("消息类型暂不支持");
            Message resultMessage = new Message();
            resultMessage.setStart(SocketTypeMessageEnum.MESSAGE_TYPE_START.getType());
            resultMessage.setType(SocketTypeMessageEnum.MESSAGE_TYPE_ERROR.getType());
            resultMessage.setContent(JSONObject.toJSONString(socketResult));
            channelHandlerContext.channel().writeAndFlush(message);
        }
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        LOG.debug("客户端断开连接，移除登录状态");
        SocketServerManager.getInstance().removeOnlineClient(ctx.channel().id().asShortText());
        ctx.fireChannelInactive();
    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }

    public void setSocketClientPongServiceImpl(ISocketService socketClientPongServiceImpl) {
        this.socketClientPongServiceImpl = socketClientPongServiceImpl;
    }

    public void setSocketClientExceptionServiceImpl(ISocketService socketClientExceptionServiceImpl) {
        this.socketClientExceptionServiceImpl = socketClientExceptionServiceImpl;
    }

    public void setSocketClientLoginServiceImpl(ISocketService socketClientLoginServiceImpl) {
        this.socketClientLoginServiceImpl = socketClientLoginServiceImpl;
    }

    public void setSocketClientInfoServiceImpl(ISocketService socketClientInfoServiceImpl) {
        this.socketClientInfoServiceImpl = socketClientInfoServiceImpl;
    }
}
