package com.zt.plat.module.qms.iot.service;

import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.module.qms.iot.tcpserver.cluster.TioServerMessageConfig;
import com.zt.plat.module.qms.iot.tcpserver.cluster.TioServerMessagePublisher;
import com.zt.plat.module.qms.iot.tcpserver.cluster.TioServerMessageVo;
import com.zt.plat.module.qms.iot.tcpserver.core.IotDeviceSessionContext;
import com.zt.plat.module.qms.iot.tcpserver.device.RedisSessionComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestParam;
import tech.zzjc.tio.core.ChannelContext;
import tech.zzjc.tio.core.Tio;
import tech.zzjc.tio.starter.TioServerBootstrap;

import static java.lang.Thread.sleep;

@Service
public class IotConnectManagerStatsService {

    @Autowired
    private TioServerBootstrap tioServerBootstrap;
    @Autowired public RedisSessionComponent redisSessionComponent;
    @Autowired private TioServerMessagePublisher tioServerMessagePublisher;

    public CommonResult<?> close(String id, String fromSubscribe) throws InterruptedException {
        ChannelContext channelContext = Tio.getByChannelContextId(tioServerBootstrap.getServerTioConfig(), id);
        if(channelContext == null){
            if("1".equals(fromSubscribe))
                return CommonResult.success("ok");
            //广播消息处理
            TioServerMessageVo message = new TioServerMessageVo();
            message.setServerClusterMsgType(TioServerMessageConfig.MESSAGE_TYPE_CLOSE_MANUAL);
            message.setChannelId(id);
            tioServerMessagePublisher.publish(message);
            //延迟0.5秒返回
            sleep(500);
            return CommonResult.success("ok");
        }
        IotDeviceSessionContext sessionContext = (IotDeviceSessionContext) channelContext.get(IotDeviceSessionContext.DEFAULT_DEVICE_SESSION_CONTEXT_KEY);
        String deviceId = sessionContext.getDeviceId();
        if(!ObjectUtils.isEmpty(deviceId))
            redisSessionComponent.deleteByDeviceId(deviceId);
        Tio.IpBlacklist.clear();//清空全局黑名单
        Tio.close(channelContext, "控制端主动关闭连接");
        return CommonResult.success("ok");
    }

    public CommonResult<?> clearDeviceControl(String id, String fromSubscribe) throws InterruptedException {
        ChannelContext channelContext = Tio.getByChannelContextId(tioServerBootstrap.getServerTioConfig(), id);
        Tio.IpBlacklist.clear();//清空全局黑名单
        if(channelContext == null){
            if("1".equals(fromSubscribe))
                return CommonResult.success("ok");
            //广播消息处理
            TioServerMessageVo message = new TioServerMessageVo();
            message.setServerClusterMsgType(TioServerMessageConfig.MESSAGE_TYPE_CLEAR_MANUAL);
            message.setChannelId(id);
            tioServerMessagePublisher.publish(message);
            //延迟0.5秒返回
            sleep(500);
            return CommonResult.success("ok");
        }
        IotDeviceSessionContext deviceSessionContext = (IotDeviceSessionContext) channelContext.get(IotDeviceSessionContext.DEFAULT_DEVICE_SESSION_CONTEXT_KEY);
        String deviceId = deviceSessionContext.getDeviceId();
        if(!ObjectUtils.isEmpty(deviceId)){
            redisSessionComponent.clearControlByDeviceId(deviceId, channelContext);
        }
//        if(channelContext != null) {
        //设置控制点的channelContext
//            deviceSessionContext.setControlChannelContext(null);
//            deviceSessionContext.setControlRealName("");
//            channelContext.set(ChannelContextConstant.CONTROL_DEVICE_ID, "");
//        }
        return CommonResult.success("ok");
    }

}
