package com.mvc.netty.dealconstant;

import com.mvc.utils.NumberUtil;
import com.mvc.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;

//紧急域名联系报文引导符类
public enum DomainContactIdentifier {
    F1F1("F1F1", "遥测站地址", "[0-9]*"),
    FF48("48", "水表", "[0-9]*"),
    FF49("49", "压力表", "[0-9]*"),
    FF08("FF08", "设备SN", "[0-9]*"),
    FF31("FF31", "IP1及端口", "[0-9]*"),
    FF32("FF32", "IP2及端口", "[0-9]*"),
    FF33("FF33", "IP3及端口", "[0-9]*"),
    FF34("FF34", "IP4及端口", "[0-9]*"),
    FF35("FF35", "IP1域名及端口", "[0-9]*"),
    ;

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

    private DomainContactIdentifier(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 (DomainContactIdentifier msg : DomainContactIdentifier.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;

        if (this.key == F1F1.getKey()) {   //遥测站(固定长度)
            result = handleIdentifier(result);
            this.value = result.substring(0, 10);
            result = result.substring(10);
            this.len = "5";
            return result;
        } else if (key == FF48.getKey() || key == FF49.getKey()) {
            this.value = result.substring(0, 2);
            result = result.substring(2);
            this.len = "1";
            return result;
        }
        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;

        if (key == FF35.getKey()) {
            String domain = dataStr.substring(0, 56);
            String port = dataStr.substring(56);
            byte[] bytes = NumberUtil.hexStringToBytes(domain);
            String nameValue = StringUtil.getStringByBytes(bytes);
            Integer portValue = Integer.valueOf(port, 16);
            nameValue = nameValue.replace(" ","");
            this.value = nameValue + ":" + portValue;
            result = result.substring(dataStr.length());
            return result;
        }

        //取得实践数字字符串
        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 DomainContactIdentifier getElement(String key) {
        for (DomainContactIdentifier msg : DomainContactIdentifier.values()) {
            if (msg.key.equals(key)) {
                return msg;
            }
        }
        return null;
    }

    public String getValue() {
        return value;
    }
}
