package com.usky.cdi.service.enums;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

/**
 *
 * @author fyc
 * @email yuchuan.fu@chinausky.com
 * @date 2026/2/3
 */
public final class MqttTopics {

    /* ========================== 1. base ========================== */
    @Getter
    @RequiredArgsConstructor
    public enum Base {
        ENGINEERING("base/engineering", "人防工程基础信息"),
        PROTECTIVE_UNIT("base/protectiveUnit", "防护单元基础信息"),
        FLOOR_PLANE("base/floorPlane", "楼层平面图信息"),
        SENSOR_INFO("base/sensorInfo", "智能监管物联设施数据"),
        VIDEO_DEVICE("base/videoDevice", "GB28181视频设备目录");

        private final String topic;
        private final String desc;
    }

    /* ========================== 2. iotInfo ========================== */
    @Getter
    @RequiredArgsConstructor
    public enum IotInfo {
        MONITORING_DATA("iotInfo", "物联监测数据"),
        CIVIL_DEFENSE_DOOR("iotInfo/civilDefenseDoor", "人防门姿态"),
        WATER_TANK_CAPACITY("iotInfo/waterTankCapacity", "战时水箱水量"),
        PIPELINE_VALVE("iotInfo/pipelineValve", "给排水管道阀门启闭状态"),
        VENTILATION_PIPELINE_VALVE("iotInfo/ventilationPipelineValve", "战时通风管道密闭阀启闭状态"),
        OVER_PRESSURE_VALVE("iotInfo/overPressureValve", "超压排气活门启闭状态"),
        MICRO_PRESSURE_DIFFERENCE("iotInfo/microPressureDifference", "微压差"),
        BLOWER_STARTUP_TIME("iotInfo/blowerStartupTime", "战时风机每日运行时长"),
        VENTILATION_RUN_STATUS("iotInfo/ventilationRunStatus", "战时通风系统运行工况"),
        ALTERNATOR_STARTUP_TIME("iotInfo/alternatorStartupTime", "战时柴油发电机每日启动时长"),
        ALTERNATOR_STATUS("iotInfo/alternatorStatus", "战时柴油发电机实时工况"),
        DIESEL_CAPACITY("iotInfo/dieselCapacity", "战时储油容量"),
        PERSON("iotInfo/person", "工程内掩蔽人数"),
        SIGNAL_STRENGTH("iotInfo/sig", "工程外移动通信信号强度"),
        MATERIAL("war/material", "工程战备物资储备情况"),
        SEWAGE_LEVEL("iotInfo/sewageLevel", "集水井水位"),
        H2S("iotInfo/h2S", "硫化氢浓度"),
        NH3("iotInfo/nh3", "氨浓度"),
        HCHO("iotInfo/hcho", "甲醛浓度"),
        C6H6("iotInfo/c6h6", "苯浓度"),
        RADIATION("iotInfo/radiation", "空气放射性物质含量"),
        RN("iotInfo/rn", "氡活度浓度"),
        CCL2F2("iotInfo/ccl2f2", "二氯二氟甲烷浓度"),
        TVOC("iotInfo/tvoc", "总挥发性有机化合物浓度"),
        PM("iotInfo/pm", "可吸入颗粒物浓度"),
        NO2("iotInfo/no2", "二氧化氮浓度"),
        SO2("iotInfo/so2", "二氧化硫浓度"),
        BACTERIAL("iotInfo/bacterial", "细菌总数"),
        TILT("iotInfo/tilt", "倾斜"),
        CRACK("iotInfo/crack", "裂缝"),
        DEVIATION("iotInfo/deviation", "位移"),
        // 水浸状态
        WATER_LEAK("iotInfo/flooded", "水浸状态"),
        // 温度
        TEMP("iotInfo/temp", "空气温度"),
        // 湿度
        HUMIDITY("iotInfo/rh", "空气湿度"),
        // 氧气
        OXYGEN("iotInfo/o2", "氧气浓度"),
        // 二氧化碳
        CO2("iotInfo/co2", "二氧化碳浓度"),
        // 一氧化碳
        CO("iotInfo/co", "一氧化碳浓度"),
        // 人员闯入情况
        PERSON_PRESENCE("iotInfo/personPresence", "人员闯入情况"),
        // 用电负荷
        ELECTRICITY_LOAD("iotInfo/electricityLoad", "人防用电负荷"),
        VIDEO_STREAM("iotInfo/videoStream", "GB28181视频流地址");

        private final String topic;
        private final String desc;
    }

    /* ========================== 3. alarm ========================== */
    @Getter
    @RequiredArgsConstructor
    public enum Alarm {
        // 通用告警事件（人防门、超压、滤尘器、油库、水浸、环境气体、电气、结构、人员、火险等全部走此 Topic）
        MESSAGE("alarm/message", "告警信息");

        private final String topic;
        private final String desc;
    }


    /* =============== 工具方法：根据字符串 topic 反查枚举 =============== */
    public static Base baseOf(String topic) {
        for (Base b : Base.values()) {
            if (b.topic.equals(topic)) return b;
        }
        return null;
    }

    public static IotInfo iotInfoOf(String topic) {
        for (IotInfo i : IotInfo.values()) {
            if (i.topic.equals(topic)) return i;
        }
        return null;
    }

    public static Alarm alarmOf(String topic) {
        for (Alarm a : Alarm.values()) {
            if (a.topic.equals(topic)) return a;
        }
        return null;
    }

    public static String getDescByTopic(String topic) {
        String desc = "";
        Base base = baseOf(topic);
        if (base != null) desc = base.getDesc();
        IotInfo iotInfo = iotInfoOf(topic);
        if (iotInfo != null) desc = iotInfo.getDesc();
        Alarm alarm = alarmOf(topic);
        if (alarm != null) desc = alarm.getDesc();
        return desc;
    }

    private MqttTopics() {
    }

    public static IotInfo getIotInfoByTypeCode(Integer typeCode) {
        switch (typeCode) {
            case 707:
                return IotInfo.TEMP;
            case 708:
                return IotInfo.HUMIDITY;
            case 709:
                return IotInfo.OXYGEN;
            case 710:
                return IotInfo.CO2;
            case 711:
                return IotInfo.CO;
            case 703:
                return IotInfo.PERSON_PRESENCE;
            case 704:
                return IotInfo.ELECTRICITY_LOAD;
            case 702:
                return IotInfo.WATER_LEAK;
            case 714:
                return IotInfo.DEVIATION;
            case 719:
                return IotInfo.PERSON;
            default:
                return IotInfo.MONITORING_DATA;
        }
    }
}