package net.northking.cctp.designer.ws;

import com.alibaba.fastjson2.JSONObject;
import net.northking.cctp.designer.ws.client.EngineWsClient;
import net.northking.cctp.designer.ws.client.RobotWsClient;
import net.northking.cctp.designer.ws.constants.PcuiDesignerConstant;
import net.northking.cctp.designer.ws.server.FrontDatasWebsocket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;

public class WsUtils {

    public static final String redisEnterKey = "UITEST-enterKey:";

    public static final String frontWsStateKey = "UITEST-frontWsStateKey:";

    private static final Logger logger = LoggerFactory.getLogger(WsUtils.class);

    public static final Map<String,WsManyWaysBo> sessionMap = new ConcurrentHashMap<>();

    //状态管理map
    public static final Map<String,RobotEngineConnetStatusBo> connectStatus = new ConcurrentHashMap<>();
    //默认启动一个10个线程的线程池
    public static final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);
    //用来存储启动任务线程的map
    public static final Map<String, ScheduledFuture<?>> scheduledFutures = new ConcurrentHashMap<>();

    public static void closeRobotAndEngineWs(String sessionId,String msg) {
        WsManyWaysBo wsManyWaysBo = sessionMap.get(sessionId);
        //如果为机器人ws则关闭引擎的ws
        if (null != wsManyWaysBo){
            EngineWsClient engineWsClient = wsManyWaysBo.getEngineWsClient();
            RobotWsClient robotWsClient = wsManyWaysBo.getRobotWsClient();
            //将引擎对象置位null
            wsManyWaysBo.setEngineWsClient(null);
            //将引擎对象置位null
            wsManyWaysBo.setRobotWsClient(null);
            if (null != engineWsClient && engineWsClient.isNormal()){
                engineWsClient.optClose(msg);
            }
            if (null != robotWsClient && robotWsClient.isNormal()){
                robotWsClient.optClose(msg);
            }
        }

    }

    public static void closeFrontWs(String sessionId) {
        WsManyWaysBo wsManyWaysBo = sessionMap.get(sessionId);
        //移除对象map
        sessionMap.remove(sessionId);
        //再移除状态map
        connectStatus.remove(sessionId);
        if (null != wsManyWaysBo && null != wsManyWaysBo.getEngineWsClient() && wsManyWaysBo.getEngineWsClient().isNormal()){
            wsManyWaysBo.getEngineWsClient().optClose(PcuiDesignerConstant.RP_CLOSE_WS_CHANNEL_MANUALLY);
        }
        if (null != wsManyWaysBo && null != wsManyWaysBo.getRobotWsClient() && wsManyWaysBo.getRobotWsClient().isNormal()){
            wsManyWaysBo.getRobotWsClient().optClose(PcuiDesignerConstant.RP_CLOSE_WS_CHANNEL_MANUALLY);
        }
    }


    public static void sendMsgEngine(FrontDatasWebsocket frontDatasWebsocket, String message) {
        sessionMap.values().forEach(wsManyWaysBo -> {
            if (wsManyWaysBo.getFrontDatasWebsocket() == frontDatasWebsocket){
                wsManyWaysBo.getEngineWsClient().sendMessage(message);
                return;
            }
        });
    }

    public static void sendMsgRobot(FrontDatasWebsocket frontDatasWebsocket, String message) {
        sessionMap.values().forEach(wsManyWaysBo -> {
            if (wsManyWaysBo.getFrontDatasWebsocket() == frontDatasWebsocket){
                wsManyWaysBo.getRobotWsClient().sendMessage(message);
                return;
            }
        });
    }

    public static void sendMsgFront(Object ws, String message) {
        sessionMap.values().forEach(wsManyWaysBo -> {
            if (wsManyWaysBo.getEngineWsClient() == ws || wsManyWaysBo.getRobotWsClient() == ws){
                wsManyWaysBo.getFrontDatasWebsocket().sendMessage(message);
                return;
            }
        });


    }
    public static void sendMsgFrontConnetStatus(WsManyWaysBo wsManyWaysBo,String status,String type) {
        //将连接状态告知前端
        JSONObject json = new JSONObject();
        json.put(InstructSetMsg.cmd, InstructSetMsg.device_status);
        json.put(InstructSetMsg.sessionId, wsManyWaysBo.getSessionId());
        JSONObject data = new JSONObject();
        data.put(InstructSetMsg.type,status);
        json.put(InstructSetMsg.data, data);
        json.put(InstructSetMsg.robot_engine,type);
        wsManyWaysBo.getFrontDatasWebsocket().sendMessage(json.toJSONString());
    }


}
