package com.fuyuanshen.global.mqtt.base;

import cn.hutool.core.lang.Dict;

import java.util.Map;
import java.util.LinkedHashMap;
import java.util.Collections;
import org.springframework.stereotype.Component;


@Component
public final class MqttXinghanCommandType {
    private MqttXinghanCommandType() {}

    public enum XinghanCommandTypeEnum {
        /**
         * 星汉设备主动上报数据
         */
        GRADE_INFO(101),
        /**
         * 星汉开机LOGO
         */
        PIC_TRANS(102),
        /**
         * 星汉设备发送消息 (XingHan send msg)
         */
        TEX_TRANS(103),
        BREAK_NEWS(104),
        UNKNOWN(0);

        private final int value;
        XinghanCommandTypeEnum(int value) { this.value = value; }
        public int getValue() { return value; }
    }

    private static final Map<String, XinghanCommandTypeEnum> KEY_TO_TYPE;
    static {
        LinkedHashMap<String, XinghanCommandTypeEnum> map = new LinkedHashMap<>();
        map.put("sta_DetectGrade", XinghanCommandTypeEnum.GRADE_INFO);
        map.put("sta_PowerTime",   XinghanCommandTypeEnum.GRADE_INFO);
        map.put("sta_longitude",   XinghanCommandTypeEnum.GRADE_INFO);
        map.put("sta_latitude",    XinghanCommandTypeEnum.GRADE_INFO);
        map.put("sta_PicTrans",    XinghanCommandTypeEnum.PIC_TRANS);
        map.put("sta_TexTrans",    XinghanCommandTypeEnum.TEX_TRANS);
        map.put("sta_BreakNews",    XinghanCommandTypeEnum.BREAK_NEWS);
        KEY_TO_TYPE = Collections.unmodifiableMap(map);
    }

    public static int computeVirtualCommandType(Dict payloadDict) {
        if (payloadDict == null) {
            return XinghanCommandTypeEnum.UNKNOWN.getValue();
        }
        try {
            for (String key : KEY_TO_TYPE.keySet()) {
                if (payloadDict.containsKey(key)) {
                    return KEY_TO_TYPE.get(key).getValue();
                }
            }
        } catch (Exception ex) {
            return XinghanCommandTypeEnum.UNKNOWN.getValue();
        }

        return XinghanCommandTypeEnum.UNKNOWN.getValue();
    }
}
