package com.cm.central.control.service.socket.login;

import com.alibaba.fastjson.JSONObject;
import com.cm.central.control.manager.SocketServerManager;
import com.cm.central.control.pojo.bos.ClientBO;
import com.cm.central.control.pojo.bos.SocketClientBO;
import com.cm.central.control.pojo.dtos.client.ClientDTO;
import com.cm.central.control.service.client.IClientService;
import com.cm.central.control.service.socket.BaseSocketService;
import com.cm.common.utils.AesUtil;
import com.cm.socket.consts.ISocketConst;
import com.cm.socket.enums.SocketTypeMessageEnum;
import com.cm.socket.pojo.Message;
import com.cm.socket.pojo.SocketResult;
import io.netty.channel.ChannelHandlerContext;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: LoginService
 * @Description: 登录业务
 * @Author: WangGeng
 * @Date: 2020/7/28 3:52 下午
 * @Version: 1.0
 **/
@Service("socketLoginServiceImpl")
public class SocketLoginServiceImpl extends BaseSocketService {

    @Autowired
    private IClientService clientService;

    @Override
    public void readMessage(ChannelHandlerContext channelHandlerContext, Message readMessage) throws Exception {
        readMessageService(channelHandlerContext, readMessage);
    }

    @Override
    protected void readMessageService(ChannelHandlerContext channelHandlerContext, Message readMessage) throws Exception {
        JSONObject clientInfo = JSONObject.parseObject(getDecodeContent(readMessage.getContent()));
        String clientId = clientInfo.getString(ISocketConst.CLIENT_ID);
        String clientSecret = clientInfo.getString(ISocketConst.CLIENT_SECRET);
        if (StringUtils.isBlank(clientId)) {
            writeError(channelHandlerContext, ISocketConst.CLIENT_ID + "不能为空");
            return;
        }
        if (StringUtils.isBlank(clientSecret)) {
            writeError(channelHandlerContext, ISocketConst.CLIENT_SECRET + "不能为空");
            return;
        }
        if (SocketServerManager.getInstance().isClientOnline(clientId)) {
            writeError(channelHandlerContext, "客户端已登录");
            return;
        }
        ClientBO clientBO = clientService.getClientBOByClientId(clientId);
        if (clientBO == null) {
            writeError(channelHandlerContext, "客户端未注册");
            return;
        }
        try {
            clientSecret = AesUtil.aesCommonDecoder(IClientService.CLIENT_SECRET_ENCODE_KEY, clientSecret);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
            writeError(channelHandlerContext, "ClientSecret错误");
            return;
        }
        if (!StringUtils.equals(clientSecret, clientBO.getClientSecret())) {
            writeError(channelHandlerContext, "ClientId和ClientSecret不匹配");
            return;
        }
        SocketClientBO socketClientBO = new SocketClientBO();
        socketClientBO.setClientId(clientId);
        socketClientBO.setProjectLeaderId(clientBO.getProjectLeaderId());
        socketClientBO.setChannel(channelHandlerContext.channel());
        socketClientBO.setLastTime(System.currentTimeMillis());
        String token = channelHandlerContext.channel().id().asShortText();
        SocketServerManager.getInstance().addClient(token, socketClientBO);
        SocketResult<String> result = successResult(token);
        writeMessage(channelHandlerContext, SocketTypeMessageEnum.MESSAGE_TYPE_CLIENT_LOGIN, result);
    }

    @Override
    public void autoReply(ChannelHandlerContext channelHandlerContext) {

    }

}
