package com.njcn.gather.detection.util.socket;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.njcn.gather.detection.pojo.enums.SourceOperateCodeEnum;
import com.njcn.gather.detection.pojo.param.PreDetectionParam;
import com.njcn.gather.detection.pojo.vo.SocketMsg;
import com.njcn.gather.detection.util.socket.websocket.WebServiceManager;

/**
 * @Author: cdf
 * @CreateTime: 2025-01-02
 * @Description: 工具类
 */
public class CnSocketUtil {

    public final static String DEV_TAG = "_Dev";

    public final static String CONTRAST_DEV_TAG = "_Contrast_Dev";

    public final static String SOURCE_TAG = "_Source";

    public final static String START_TAG = "_Start";

    public final static String END_TAG = "_End";

    public final static String STEP_TAG = "&&";

    public final static String SPLIT_TAG = "_";

    /**
     * 退出检测
     */
    public static void quitSend(PreDetectionParam param) {
        SocketMsg<String> socketMsg = new SocketMsg<>();
        socketMsg.setRequestId(SourceOperateCodeEnum.QUITE.getValue());
        socketMsg.setOperateCode(SourceOperateCodeEnum.QUIT_INIT_03.getValue());
        SocketManager.sendMsg(param.getUserPageId() + DEV_TAG, JSON.toJSONString(socketMsg));
        WebServiceManager.removePreDetectionParam(param.getUserPageId());
    }

    /**
     * 关闭源连接
     */
    public static void quitSendSource(PreDetectionParam param) {
        SocketMsg<String> socketMsg = new SocketMsg<>();
        socketMsg.setRequestId(SourceOperateCodeEnum.QUITE_SOURCE.getValue());
        socketMsg.setOperateCode(SourceOperateCodeEnum.CLOSE_GATHER.getValue());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("sourceId", param.getSourceName());
        socketMsg.setData(jsonObject.toJSONString());
        String sourceKey = param.getUserPageId() + SOURCE_TAG;
        if (SocketManager.isChannelActive(sourceKey)) {
            SocketManager.markActiveClose(sourceKey);
        }
        SocketManager.sendMsg(sourceKey, JSON.toJSONString(socketMsg));
        WebServiceManager.removePreDetectionParam(param.getUserPageId());
    }


    /**
     * 比对式-退出检测
     */
    public static void contrastSendquit(String loginName, SourceOperateCodeEnum operateCode, boolean isRemoveSocket) {
        System.out.println("比对式-发送" + operateCode.getMsg() + "指令。。。。。。。。");
        SocketMsg<String> socketMsg = new SocketMsg<>();
        socketMsg.setRequestId(SourceOperateCodeEnum.QUITE.getValue());
        socketMsg.setOperateCode(operateCode.getValue());
        SocketManager.sendMsg(loginName + CONTRAST_DEV_TAG, JSON.toJSONString(socketMsg));
//        WebServiceManager.removePreDetectionParam();
        FormalTestManager.isRemoveSocket = isRemoveSocket;
        FormalTestManager.currentStep = SourceOperateCodeEnum.QUITE;
    }
}
