package com.zt.plat.module.qms.iot.tcpserver.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zt.plat.module.qms.iot.tcpserver.IotPacket;
import com.zt.plat.module.qms.iot.tcpserver.caaclient.IotCaaClientControlDeviceHander;
import com.zt.plat.module.qms.iot.tcpserver.caaclient.IotCaaClientRegisterHander;
import com.zt.plat.module.qms.iot.tcpserver.consoleclient.*;
import com.zt.plat.module.qms.iot.tcpserver.consoleclient.IotConsoleClientCheckControlHander;
import com.zt.plat.module.qms.iot.tcpserver.consoleclient.IotConsoleClientRegisterHander;
import com.zt.plat.module.qms.iot.tcpserver.core.ChannelContextConstant;
import com.zt.plat.module.qms.iot.tcpserver.core.Command;
import com.zt.plat.module.qms.iot.tcpserver.core.IotClientSessionForRedisContext;
import com.zt.plat.module.qms.iot.tcpserver.device.RedisSessionComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.zzjc.tio.core.ChannelContext;

/**
 * <b>IotClientHandler</b>
 * <p>更新历史：</p>
 * <pre> 版本         更新时间            更新者        更新内容
 * V1.0      2021-6-28          王兴荣         Add</pre>
 * <b>Copyright (C)  云南志者竟成科技有限公司</b> 
 * @author 王兴荣 &lt;xingrong_wang@will-way.cn&gt;
 * @version V1.0 
 * @since 2021-6-28
 */
@Slf4j
@Component
public class IotClientHandler implements IotHander {
    

    
    @Autowired
    private IotConsoleClientRegisterHander iotConsoleClientRegisterHander;

    @Autowired
    private IotConsoleClientCheckControlHander iotConsoleClientCheckControlHander;

    @Autowired
    private IotCaaClientRegisterHander iotCaaClientRegisterHander;
    
    @Autowired
    private IotCaaClientControlDeviceHander iotCaaClientControlDeviceHander;
    @Autowired public RedisSessionComponent redisSessionComponent;
    
    public IotClientHandler() {
    }

    @Override
    public Object hander(IotPacket iotPacket, ChannelContext channelContext) throws Exception {
        //获取客户端连接信息
        String realClientIp = channelContext.get("realClientIp") == null ? channelContext.getClientNode().getIp() : channelContext.get("realClientIp").toString();
        String realClientPort = channelContext.get("realClientPort") == null ? channelContext.getClientNode().getPort() + "" : channelContext.get("realClientPort").toString();
        String realClient = realClientIp + ":" + realClientPort;
        String text = new String(iotPacket.getBody(), IotPacket.CHARSET);
        JSONObject jsonObject = JSON.parseObject(text);
        String msgId = jsonObject.getString("msgId");
        String clientType = jsonObject.getString("clientType");
        String cmd = jsonObject.getString("cmd");
        Command command = Command.getByName(cmd);
        IotDataHander iotDataHander = null;
        String channelId = channelContext.getId();
        IotClientSessionForRedisContext iotClientSessionForRedisContext = redisSessionComponent.getByClientChannelId(channelId);
        if ("consoleClient".equals(clientType)) {
            channelContext.set(ChannelContextConstant.CONNECT_TYPE, ChannelContextConstant.CONNECT_CONSOLE_CLIENT);
            switch (command) {
                case REGISTER:
                    iotDataHander = iotConsoleClientRegisterHander;
                    break;

                case CHECK_CONTROL:
                    iotDataHander = iotConsoleClientCheckControlHander;
                    break;
                default:
                    break;
            }
        } else if ("caaClient".equals(clientType)) {
            channelContext.set(ChannelContextConstant.CONNECT_TYPE, ChannelContextConstant.CONNECT_CAA_CLIENT);
            switch (command) {
                case REGISTER:
                    iotDataHander = iotCaaClientRegisterHander;
                    break;
                case CONTROL_DEVICE:
                    iotDataHander = iotCaaClientControlDeviceHander;
                    break;
                default:
                    break;
            }
        }
        if (iotDataHander == null) {
            log.error("{}, 找不到处理类， 客户端消息ID：{}，客户端类型：{}，命令：{}", realClient, msgId, clientType, cmd);
            return null;
        }

        if(iotClientSessionForRedisContext == null){
            redisSessionComponent.regClient(channelContext);
        }else{
            redisSessionComponent.updateClient(iotClientSessionForRedisContext, channelContext);
        }

        iotDataHander.hander(msgId, jsonObject.getString("data"), channelContext);
        return null;
    }

}
