package com.ruoyi.device.mqtt.enums;

import com.ruoyi.common.utils.StringUtils;

/**
 * 请求方法类型枚举
 *
 * @author lwm
 */
public enum CmdTypeEnum
{
    TSB_LOGIN("tsb:login", "tsb:login:up", "tsb:login:down", "调试宝登录"),
    TSB_PT("tsb:pt", "tsb:pt:up", "tsb:pt:down", "调试宝PT产测"),
    COMMON_TAX("common:tax", "common:tax:up", "common:tax:down", "报税口数据"),
    COMMON_OPW("common:opw", "common:opw:up", "common:opw:down", "液位仪主页"),
    COMMON_OPW_QUERY("common:opw:query", "common:opw:query:up", "common:opw:query:down", "液位仪查询"),
    COMMON_OPW_PASSTHROUGH("common:opw:passthrough", "common:opw:passthrough:up", "common:opw:passthrough:down", "液位仪透传"),
    ;

    private final String cmdType;
    private final String cmdUpType;
    private final String cmdDownType;
    private final String message;

    CmdTypeEnum(String cmdType, String cmdUpType, String cmdDownType, String message)
    {
        this.cmdType = cmdType;
        this.cmdUpType = cmdUpType;
        this.cmdDownType = cmdDownType;
        this.message = message;
    }

    public String getCmdUpType()
    {
        return cmdUpType;
    }

    public String getCmdDownType()
    {
        return cmdDownType;
    }

    public String getCmdType()
    {
        return cmdType;
    }

    public String getMessage()
    {
        return message;
    }

    /**
     * 解析设备上行 cmdType（兼容 {@code cmdUpType} 与 {@code cmdType}）
     */
    public static CmdTypeEnum resolveUplink(String raw)
    {
        if (StringUtils.isEmpty(raw))
        {
            return null;
        }
        String trimmed = raw.trim();
        for (CmdTypeEnum item : values())
        {
            if (trimmed.equals(item.cmdUpType) || trimmed.equals(item.cmdType))
            {
                return item;
            }
        }
        return null;
    }

    /**
     * 是否为下行应答 cmdType（设备误发 down 时识别）
     */
    public static boolean isDownlink(String raw)
    {
        if (StringUtils.isEmpty(raw))
        {
            return false;
        }
        String trimmed = raw.trim();
        for (CmdTypeEnum item : values())
        {
            if (trimmed.equals(item.cmdDownType))
            {
                return true;
            }
        }
        return false;
    }

    /**
     * 解析设备下行 cmdType（兼容 {@code cmdDownType} 与 {@code cmdType}）
     */
    public static CmdTypeEnum resolveDownlink(String raw)
    {
        if (StringUtils.isEmpty(raw))
        {
            return null;
        }
        String trimmed = raw.trim();
        for (CmdTypeEnum item : values())
        {
            if (trimmed.equals(item.cmdDownType) || trimmed.equals(item.cmdType))
            {
                return item;
            }
        }
        return null;
    }

    /**
     *  是否为上行请求 cmdType（设备误发 up 时识别）
     */
    public static boolean isUplink(String raw)
    {
        if (StringUtils.isEmpty(raw))
        {
            return false;
        }
        String trimmed = raw.trim();
        for (CmdTypeEnum item : values())
        {
            if (trimmed.equals(item.cmdUpType))
            {
                return true;
            }
        }
        return false;
    }
}
