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

import com.google.common.collect.HashBiMap;
import com.njcn.gather.detection.pojo.dto.WaveResultDTO;
import com.njcn.gather.detection.pojo.enums.SourceOperateCodeEnum;
import com.njcn.gather.detection.pojo.po.DevData;
import com.njcn.gather.detection.pojo.vo.DevLineTestResult;
import com.njcn.gather.device.pojo.enums.PatternEnum;
import com.njcn.gather.device.pojo.vo.PreDetection;
import com.njcn.gather.plan.pojo.enums.DataSourceEnum;
import com.njcn.gather.plan.pojo.po.AdPlan;
import com.njcn.gather.plan.pojo.po.AdPlanTestConfig;
import com.njcn.gather.script.pojo.po.SourceIssue;
import com.njcn.gather.system.dictionary.pojo.enums.DictDataEnum;

import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;

/**
 * @Author: cdf
 * @CreateTime: 2025-01-08
 * @Description: 正式（预）检测对象管理
 */
public class FormalTestManager {

    // 当前步骤
    public static SourceOperateCodeEnum currentStep;

    /**
     * key:设备ip,value:当前设备下面的监测点ID(ip_通道号)
     */
    public static Map<String, List<String>> devMapMonitorNum = new ConcurrentHashMap<>();

    /**
     * 所有参与检测的被检设备
     */
    public static List<PreDetection> devList = new ArrayList<>();

    /**
     * 所有参与检测的标准设备
     */
    public static List<PreDetection> standardDevList = new ArrayList<>();

    /**
     * 所有参与检测的监测点
     */
    public static List<String> monitorIdListComm = new ArrayList<>();

    /**
     * 所有参与检测的监测点。key:监测点（ip_通道号）,value:检测点实体
     */
    public static Map<String, PreDetection.MonitorListDTO> monitorMap = new HashMap<>();

    /**
     * 用于存储所有测点的实时数据
     */
    public static List<DevData> realDataXiList = new ArrayList<>();

    /**
     * key:设备ip,value:装置名称
     */
    public static Map<String, String> devNameMapComm = new HashMap<>();

    /**
     * key:设备ip,value:装置id
     */
    public static HashBiMap<String, String> devIdMapComm = HashBiMap.create();

    /**
     * 停止触发标识
     */
    public static Boolean stopFlag = false;

    /**
     * 已经暂停后的标识
     */
    public static Boolean hasStopFlag = false;

    /**
     * 暂停后的超时计时时间
     */
    public static Integer stopTime = 0;

    public enum PauseMode {
        NONE,
        WAIT_CURRENT_ITEM,
        IMMEDIATE
    }

    public static PauseMode pauseMode = PauseMode.NONE;

    /**
     * 当前正在检测的计划
     */
    public static AdPlan currentTestPlan;

    /**
     * 非录波数据源枚举
     */
    public static DataSourceEnum nonWaveDataSourceEnum;


    /**
     * 统计数据最大超时时间
     */
    public static Long maxTime;

    public static AdPlanTestConfig curretntTestPlanConfig;

    /**
     * 当前正在检测的模式
     */
    public static PatternEnum patternEnum;

    /**
     * 比对式检测-检测项。key为检测项code,value为检测项id
     */
    public static Map<String, String> testItemMap = new HashMap<>();

    /**
     * 数据处理原则
     */
    public static DictDataEnum dataRule;

    /**
     * 所有参与比对式检测的被检设备、标准设备配对关系。key:被检设备ip_通道号,value:标准设备ip_通道号
     */
    public static HashBiMap<String, String> pairsIpMap = HashBiMap.create();

    /**
     * 所有参与比对式检测的被检设备、标准设备配对关系。key:被检设备id_通道号,value:标准设备id_通道号
     */
    public static HashBiMap<String, String> pairsIdMap = HashBiMap.create();

    /**
     * 被检设备的数据。key:设备ip_通道号,value:DevData数据集合
     */
    public static Map<String, List<DevData>> devDataMap = new HashMap<>();

    /**
     * 标准设备的数据。key:设备ip_通道号,value:DevData数据集合
     */
    public static Map<String, List<DevData>> standardDevDataMap = new HashMap<>();

    /**
     * 是否要移除和通信模块的socket连接
     */
    public static Boolean isRemoveSocket;

    /**
     * 录波功能校验
     */
    public static Boolean waveCheckFlag;

    /**
     * 第几次监测 key为设备监测点id,value为第几次监测
     */
    public static Map<String, Integer> numMap = new HashMap<>();

    /**
     * 存放录波相关数据。key:设备ip_通道号,value:WaveResultDTO数据
     */
    public static Map<String, WaveResultDTO> waveResultDTOMap = new HashMap<>();

    /**
     * 录波组数
     */
    public static Integer waveNum;

    /**
     * 每次录波检测结果
     */
    public static List<DevLineTestResult> preNumTestResultList = new ArrayList<>();

    /**
     * 定时器
     */
    public static ScheduledExecutorService scheduler;

    /**
     * 定时器任务
     */
    public static ScheduledFuture<?> scheduledFuture;


    public static boolean isWaveCheck;

    public static List<String> pstDataType;

    public static boolean isPstData;

    /**
     * 是否在检测中
     */
    public static boolean isTesting;

    public static boolean realProtocol;

    public static boolean statisticsProtocol;

    public static boolean voltageProtocol;

    public static boolean unknownError;

    /**
     * 当前下发的脚本
     */
    public static SourceIssue currentIssue;

    /**
     * 是否进行相序校验
     */
    public static boolean isXu;

    /**
     * 过载信息，如果检测到电压过载，overload=1；如果检测到电流过载，overload=2；如果检测到电压&&电流过载，overload=3；反之，overload=4
     */
    public static int overload;

    /**
     * 检测开始时间
     */
    public static LocalDateTime checkStartTime;

    public static boolean resumeFormalPending;

    public static Integer resumeNextSort;

    public static Integer resumeBaseFormalCheckTime;

    public static LocalDateTime resumeFormalStartTime;


    /**
     * 数模式 检测类型"1"-"全部检测" , "2"-"不合格项复检"
     */
    public static String reCheckType;

    public static Integer longFlickerDataCount;

    public static void requestWaitPause() {
        stopFlag = true;
        hasStopFlag = false;
        stopTime = 0;
        pauseMode = PauseMode.WAIT_CURRENT_ITEM;
    }

    public static void requestImmediatePause() {
        stopFlag = true;
        hasStopFlag = true;
        stopTime = 0;
        pauseMode = PauseMode.IMMEDIATE;
    }

    public static void markPausedAfterCurrentItem() {
        stopFlag = true;
        hasStopFlag = true;
        stopTime = 0;
        pauseMode = PauseMode.WAIT_CURRENT_ITEM;
    }

    public static boolean isWaitCurrentItemPause() {
        return pauseMode == PauseMode.WAIT_CURRENT_ITEM;
    }

    public static boolean isImmediatePause() {
        return pauseMode == PauseMode.IMMEDIATE;
    }

    public static void clearPauseState() {
        stopFlag = false;
        hasStopFlag = false;
        stopTime = 0;
        pauseMode = PauseMode.NONE;
    }


    public static void clearFormalRuntime() {
        clearPauseState();
        resumeFormalPending = false;
        resumeNextSort = null;
        resumeBaseFormalCheckTime = null;
        resumeFormalStartTime = null;
        realDataXiList.clear();
        currentIssue = null;
    }
}
