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

import com.alibaba.fastjson.JSONObject;
import com.cm.central.control.client.socket.config.properties.SocketClientProperties;
import com.cm.central.control.client.socket.manager.SocketClientManager;
import com.cm.central.control.client.socket.service.socket.BaseSocketService;
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 com.cm.socket.service.ISocketService;
import io.netty.channel.ChannelHandlerContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

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

    @Autowired
    private SocketClientProperties socketClientProperties;
    @Resource(name = "pingServiceImpl")
    private ISocketService pingServiceImpl;
    @Resource(name = "clientInfoServiceImpl")
    private ISocketService clientInfoServiceImpl;


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

    @Override
    protected void readMessageService(ChannelHandlerContext channelHandlerContext, Message readMessage) throws Exception {
        SocketResult<String> socketResult = JSONObject.parseObject(readMessage.getContent(), SocketResult.class);
        SocketClientManager socketClientManager = SocketClientManager.getInstance();
        // 初始化重连
        socketClientManager.setCurrentReconnectCount(1);
        socketClientManager.setCurrentReconnectTime(1);
        socketClientManager.setToken(socketResult.getData());
        socketClientManager.setChannel(channelHandlerContext.channel());
        LOG.debug("登录成功，开启 ping-pong 心跳，发送服务器信息");
        pingServiceImpl.autoReply(channelHandlerContext);
        clientInfoServiceImpl.autoReply(channelHandlerContext);
    }

    @Override
    public void autoReply(ChannelHandlerContext channelHandlerContext) throws Exception {
        String clientId = socketClientProperties.getClientId();
        String clientSecret = socketClientProperties.getClientSecret();
        Message message = new Message();
        message.setStart(SocketTypeMessageEnum.MESSAGE_TYPE_START.getType());
        message.setType(SocketTypeMessageEnum.MESSAGE_TYPE_CLIENT_LOGIN.getType());
        JSONObject loginObj = new JSONObject();
        loginObj.put(ISocketConst.CLIENT_ID, clientId);
        loginObj.put(ISocketConst.CLIENT_SECRET, clientSecret);
        message.setContent(getEncodeContent(loginObj.toJSONString()));
        channelHandlerContext.channel().writeAndFlush(message);
    }
}
