package com.xzzn.common.enums;

import org.apache.xmlbeans.impl.common.NameUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * device-设备类别
 * 
 * @author xzzn
 */
public enum DeviceCategory
{
    PCS("PCS", "PCS设备", null),
    BRANCH("BRANCH", "PCS分支设备", PCS),
    STACK("STACK", "电池堆", null),
    CLUSTER("CLUSTER", "电池簇", STACK),
    BATTERY("BATTERY", "单体电池", CLUSTER),
    AMMETER("AMMETER", "电表", null),
    COOLING("COOLING", "冷却", null),
    DH("DH", "动环", null),
    XF("XF", "消防", null),
    BATTERY_GROUP("BATTERY_GROUP", "电池组", null),
    EMS("EMS", "EMS设备", null),;

    private final String code;
    private final String info;
    private final DeviceCategory parentCategory;

    DeviceCategory(String code, String info, DeviceCategory parentCategory)
    {
        this.code = code;
        this.info = info;
        this.parentCategory = parentCategory;
    }

    public String getCode()
    {
        return code;
    }

    public String getInfo()
    {
        return info;
    }

    public DeviceCategory getParentCategory()
    {
        return parentCategory;
    }
    // 缓存info与code的映射（优化查询效率）
    private static final Map<String, String> INFO_CODE_MAP = new HashMap<>();

    // 静态块初始化缓存
    static {
        for (DeviceCategory category : DeviceCategory.values()) {
            INFO_CODE_MAP.put(category.info, category.code);
        }
    }

    // 通过info获取code的方法
    public static String getCodeByInfo(String info) {
        return INFO_CODE_MAP.get(info); // 从缓存中直接获取，效率高
    }

    // 通过code获取父类code
    // 根据字符串编码查找对应的枚举
    public static DeviceCategory fromCode(String code) {
        for (DeviceCategory category : values()) {
            if (category.code.equalsIgnoreCase(code)) { // 忽略大小写，增强兼容性
                return category;
            }
        }
        return null;
    }

    public static String getInfoByCode(String code) {
        for (DeviceCategory category : DeviceCategory.values()) {
            if (category.code.equals(code)) {
                return category.info;
            }
        }
        return null;
    }
}
