package com.mvc.netty.dealconstant;

import com.mvc.forwarmana.PackUtils;
import com.mvc.utils.NumberUtil;
import com.mvc.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;

//远程配置引导符类
public enum TaskRemoteIdentifier {
    EE01("01", "设备SN", "[0-9]*"),
    EE02("02", "遥测站地址", "[0-9]*"),
    EE04("04", "iP1port1", "[0-9]*"),
    EE06("06", "iP2port2", "[0-9]*"),
    EE08("08", "iP3port3", "[0-9]*"),
    EE0A("0A", "iP4port4", "[0-9]*"),
    EE10("10", "水表类型", "[0-9]*"),
    EE11("11", "数字轮子个数", "[a-zA-Z0-9]*"),
    EE12("12", "水表最后一位", "[0-9]*"),
    EE13("13", "水表安装方向", "[0-9]*"),
    EE14("14", "采样时间间隔", "[0-9]*"),
    EE15("15", "上传服务器时间间隔", "[0-9]*"),
    EE16("16", "低温采样时间间隔", "[0-9]*"),
    EE17("17", "最低工作温度", "[0-9]*"),
    EE18("18", "安装年份", "[0-9]*"),
    EE19("19", "入网超时", "[0-9]*"),
    EE1A("1A", "电池报警电量", "[a-zA-Z0-9]*"),
    EE1B("1B", "强拉回概率值", "[a-zA-Z0-9]*"),
    EE1C("1C", "识别概率阀值", "[a-zA-Z0-9]*"),
    EE1D("1D", "位概率阀值", "[0-9]*"),
    EE1E("1E", "最高工作温度", "[0-9]*"),
    EE1F("1F", "电压报警值", "[0-9]*"),
    EEB1("B1", "CPU版本号", "[a-zA-Z0-9]*"),
    EEB2("B2", "通讯版本号", "[a-zA-Z0-9]*"),
    EEB3("B3", "天线类型", "[a-zA-Z0-9]*"),
    EEB4("B4", "背光频率", "[a-zA-Z0-9]*"),
    EEB5("B5", "占空比", "[0-9]*"),
    EEB6("B6", "灯光亮度范围", "[a-zA-Z0-9]*"),
    EEB7("B7", "通讯方式", "[0-9]*"),
    EEB8("B8", "电池次数清零", "[a-zA-Z0-9]*"),
    EEB9("B9", "硬件使用次数清零", "[0-9]*"),
    EEBA("BA", "当前水表读数", "[0-9]*"),
    EEBB("BB", "整图上传周规则", "[a-zA-Z0-9]*"),
    EEBC("BC", "紧急域名", "[a-zA-Z0-9]*"),
    EEBD("BD", "输出串口日志", "[a-zA-Z0-9]*"),
    EEBE("BE", "WDT计数清零", "[a-zA-Z0-9]*"),

    //下面是新增压力表的 2022-09-13 guozhao
    EEC1("C1", "量程下限", "[0-9]*"),
    EEC2("C2", "量程上限", "[a-zA-Z0-9]*"),
    EEC3("C3", "数据精度", "[0-9]*"),
    EEC4("C4", "校正参数", "[0-9]*"),
    EEC5("C5", "坐标点赋值", "[a-zA-Z0-9]*"),
    EEC6("C6", "图片是否描点", "[a-zA-Z0-9]*"),
    EEC7("C7", "目标区域历史数据初始化", "[a-zA-Z0-9]*"),
    EEC8("C8", "压力表人工配置坐标点的优先级", "[a-zA-Z0-9]*"),//2022-10-18 guozhao
    //
    EED1("D1", "手动触发大图开关", "[a-zA-Z0-9]*"),
    EED2("D2", "月初月底发送大图开关", "[a-zA-Z0-9]*"),
    EED3("D3", "指定日期发送大图", "[a-zA-Z0-9]*"),
    EEBF("BF", "日志上传规则", "[a-zA-Z0-9]*"),
    EED4("D4", "上传上一次MCU通讯错误的日志", "[a-zA-Z0-9]*"),
    EED5("D5", "手动触发发送本次工作日志", "[a-zA-Z0-9]*"),

    /** 2022-11-08 添加开始 */
    EEDA("DA", "图片类型", "[a-zA-Z0-9]*"),
    EEDB("DB", "图片像素外扩", "[a-zA-Z0-9]*"),
    EEDC("DC", "指定数字区域角度及坐标", "[a-zA-Z0-9]*"),
    EEDD("DD", "上传模型输出结果", "[a-zA-Z0-9]*"),
    EEDE("DE", "NMS阈值", "[a-zA-Z0-9]*"),
    /** 2022-11-08 添加结束 */

    /** 2022-11-15 guozhao 添加开始 */
    EEDF("DF", "检测的大框排除野数字", "[a-zA-Z0-9]*"),
    /** 2022-11-15 guozhao 添加结束 */

    /** 2023-01-18 guozhao 服务侧识别水表专用配置项 添加开始 */
    EE71("71", "指定坐标区域上报的图片类型", "[a-zA-Z0-9]*"),
    EE72("72", "上报的JPG的压缩类型", "[a-zA-Z0-9]*"),
    /** 2023-01-18 guozhao 添加结束 */

    /** 2024-04-12 guozhao 添加开始 */
    EE80("80", "RS485使能标志", "[0-1]*"),
    EE81("81", "RS485波特率", "[0-9]*"),
    EE82("82", "模块ID号(站地址)", "[a-fA-F0-9]*"),
    EE83("83", "模块名称", "[a-fA-F0-9]*"),
    EE84("84", "校验类型", "[0-9]*"),
    EE85("85", "停止位", "[0-9]*"),
    EEA0("A0", "AI识别需要的图片亮度设置值", "[0-9]*"),
    EEA1("A1", "图像合成方式", "[0-9]*"),
    /** 2024-04-12 guozhao 添加结束 */

    /** 2024-04-22 guozhao 根据IMEI配置SN相关 添加开始 */
    FF38("FF38", "设备IMEI号", "[0-9]*"),
    FF42("FF42", "子设备的BDA", "[0-9]*"),
    FF08("FF08", "设备SN", "[0-9]*"),
    /** 2024-04-22 guozhao 根据IMEI配置SN相关 添加结束 */

    /** 2024-04-24 guozhao 会议讨论,添加11项 添加开始 */
    EEA2("A2", "日首次采样时间(", "[0-9]*"),
    EEA3("A3", "上传图片的大小(单位K)", "[0-9]*"),
    EEA4("A4", "AI输出测试(标注)图开关", "[0-9]*"),
    EEA5("A5", "是否使用人工配置的参数用于AI识别过程", "[0-9]*"),
    EEA6("A6", "禁用AI识别", "[0-9]*"),
    EEA7("A7", "最大离散时长范围", "[0-9]*"),
    EEA8("A8", "启用离散标志(", "[0-9]*"),
    EEA9("A9", "启用智慧院平台", "[0-9]*"),
    EEAA("AA", "启用北京自来水平台", "[0-9]*"),
    EEF0("F0", "水表地址", "[a-zA-Z0-9]*"),
    EEF1("F1", "启用AES加密", "[0-9]*"),
    /** 024-04-24 guozhao 添加结束 */

    EEFA("FA", "数据恢复", "[0-3]*"),
    EEAB("AB", "是否上传红色指针部分", "[0-3]*"), //(针对2合一版本)2024-06-04 guozhao
    EEF2("F2", "自来水平台密钥类型", "[0-3]*"),

    EEAC("AC", "人工配置角度", "[0-9]*"),
    EEAD("AD", "数字位置", "[0-9]*"),
    EEAE("AE", "数字表放大倍率", "[0-9]*"),
    EEAF("AF", "指针表放大倍率", "[0-9]*"),

    //针对全配置模式添加 2025-03-07 guozhao
    EE90("90", "投运类型", "[0-9]*"),
    EE91("91", "水表类型", "[0-9]*"),
    EE92("92", "表盘区域坐标", "[0-9]*"),
    EE93("93", "数字区域坐标", "[0-9]*"),
    EE94("94", "首尾指针坐标", "[0-9]*"),
    EE95("95", "表底读数+相应时间", "[0-9]*")
    ;

    private String key;
    private String desc;
    private String rule;
    private String value;
    private String len;            //字节长度(调试使用) 2022-06-30 guozhao

    TaskRemoteIdentifier(String key, String desc, String rule) {
        this.key = key;
        this.desc = desc;
        this.rule = rule;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getLen() {
        return len;
    }

    public void setLen(String len) {
        this.len = len;
    }

    /**
     * 获取key集合
     *
     * @return
     */
    public static List<String> keys() {
        List<String> keyList = new ArrayList<>();
        for (TaskRemoteIdentifier msg : TaskRemoteIdentifier.values()) {
            keyList.add(msg.key);
        }
        return keyList;
    }

    /**
     * 解析报文
     *
     * @param msg
     * @return
     */
    public String parse(String msg) {
        String result = msg;
        StringBuilder stringBuilder = new StringBuilder();
        String key = this.key;
        result = handleIdentifier(result);
        String deinfer = result.substring(0, 2);
        result = result.substring(2);
        String binaryStr = NumberUtil.hexStringToBinaryString(deinfer);
        //确定小数位
        String redixStr = binaryStr.substring(binaryStr.length() - 3);
        int radix = NumberUtil.binaryToDecimal(redixStr);
        // 确定数字位数
        String numStr = binaryStr.substring(0, binaryStr.length() - 3);
        int num = NumberUtil.binaryToDecimal(numStr);
        String dataStr = result.substring(0, num * 2);

        this.len = "整数:" + num + ",小数:" + radix;

        //取得实践数字字符串
        dataStr = getFactData(dataStr);
        result = result.substring(dataStr.length());
        stringBuilder.append(dataStr);
        if (radix != 0) {
            stringBuilder.insert(dataStr.length() - radix, '.');
        }
        this.value = stringBuilder.toString();
        return result;

    }

    /**
     * 取得实践数字字符串
     *
     * @param data
     * @return
     */
    private String getFactData(String data) {
        String result = data;
        if (!StringUtil.regularCheck(result, this.rule)) {
            result = data.substring(0, data.length() - 2);
            result = getFactData(result);
        }
        return result;
    }


    /**
     * 处理表示符
     *
     * @param msg
     * @return
     */
    public String handleIdentifier(String msg) {
        String result = msg;
        result = result.substring(this.key.length());
        return result;
    }

    /**
     * 获取成员
     *
     * @param key
     * @return
     */
    public static TaskRemoteIdentifier getElement(String key) {
        for (TaskRemoteIdentifier msg : TaskRemoteIdentifier.values()) {
            if (msg.key.equals(key)) {
                return msg;
            }
        }
        return null;
    }

    public String getValue() {
        return value;
    }

    /**
     * 获取对应标识符报文
     *
     * @param identifier
     * @param value      原始值
     * @return
     */
    public static String getIdentifierMsg(TaskRemoteIdentifier identifier, String value) {
        StringBuilder sb = new StringBuilder();
        int rdxLen = 0;
        int index = value.indexOf(".");
        if (index > -1) {//小数
            rdxLen = value.length() - index - 1;
            value = value.replace(".", "");
        }
        if (value.length() % 2 != 0) {
            value = StringUtils.leftPad(value, value.length() + 1, '0');
        }
        sb.append(identifier.key);//引导符
        sb.append(PackUtils.getItemLenIdentifier(value.length(), rdxLen));//长度标识符
        sb.append(value);//值

        return sb.toString();
    }
}
