package org.ts.ddcs.itemdatatemplate.sw;


import org.ts.ddcs.common.metadata.ElementDataMetadataEnum;
import org.ts.ddcs.common.util.BytesHelp;
import org.ts.ddcs.common.util.Sw2014DatagramTemplateHelp;
import org.ts.ddcs.common.util.ValueLen;
import org.ts.ddcs.itemdatatemplate.DataItemTemplate;

import java.math.BigDecimal;
import java.util.*;


/**
 * 0xFF10
 * 扩展闸门开度 ,长度包括2个字节闸门编号和高度，闸门编号N（3），高度数据组 N(5,2)
 */
public class DataItemTemplateEXT_FF10 extends DataItemTemplate {

//    @Override
//    public byte[] Stream(byte[] data) {
//        byte[] remain = data;
//        int count = 0;
//        String flag = BytesHelp.byte2HexStr(remain, count, 2);
//        count += 2;
//        if (flag.equals(this.feture)) {
//            byte[] len = BytesHelp.subBytes(remain, count, 1);
//            count += 1;
//            int dataTotalLen = len[0] >> 3;
//            int decimalLen = len[0] & 7;
//            if (dataTotalLen == 5 && remain.length >= count + 5) {
//                //闸门编号
//                byte[] buff = BytesHelp.subBytes(remain, count, 2);
//                String hexStr = BytesHelp.byte2HexStr(buff);
//                StringBuffer dataStr = new StringBuffer(hexStr);
//                String gateCode = StringUtil.getPrettyNumber(dataStr.toString());
//                count += 2;
//                //闸门开度
//                buff = BytesHelp.subBytes(remain, count, 3);
//                hexStr = BytesHelp.byte2HexStr(buff);
//                if (hexStr.equals("FFFFFF")) {
//                    this.datas.put("" + gateCode, hexStr);
//                } else {
//                    dataStr = new StringBuffer(hexStr);
//                    dataStr.insert(dataStr.length() - 2, ".");
//                    String value = StringUtil.getPrettyNumber(dataStr.toString());
//                    this.datas.put("" + gateCode, value);
//                }
//                count += 3;
//            }
//            remain = BytesHelp.subBytes(remain, count, remain.length - count);
//            this.macth = true;
//        }
//        return remain;
//    }

//    @Override
//    public String toString() {
//        if (null != datas && datas.size() > 0) {
//            String text = "";
//            List<Integer> textList = new LinkedList<>();
//            Set<String> keys = this.datas.keySet();
//            for (String key : keys) {
//                textList.add(Integer.parseInt(key));
//            }
//            textList.sort(new Comparator<Integer>() {
//                @Override
//                public int compare(Integer o1, Integer o2) {
//                    if (o1 > o2) {
//                        return 1;
//                    } else if (o1 < o2) {
//                        return -1;
//                    }
//                    return 0;
//                }
//            });
//
//            for (Integer i : textList) {
//                text += "" + i + "号闸门：" + this.datas.get("" + i) + "（米） ";
//            }
//            return text;
//        }
//        if (null != bigDecimalValue) {
//            return bigDecimalValue.toPlainString();
//        }
//        if (null != varcharValue) {
//            return varcharValue;
//        }
//        return "";
//    }

    @Override
    public int stream(byte[] data, int index) {
        this.macth = false;
        this.datas.clear();
        int count = 0;
        String flag = BytesHelp.byte2HexStr(data, index + count, 2);
        if (flag.toUpperCase().equals(this.feture)) {
            count += 2;
            ValueLen valueLen = Sw2014DatagramTemplateHelp.getValueLen(data, index + count);
            count += 1;
            //闸门编号
            byte[] buff = BytesHelp.subBytes(data, index + count, 2);
            String gateCode = BytesHelp.byte2HexStr(buff);
            this.datas.put("gateCode", gateCode);
            count += 2;
            //闸门开度
            buff = BytesHelp.subBytes(data, index + count, 3);
            String gateValue = BytesHelp.byte2HexStr(buff);
            this.datas.put("value", gateValue);

//            if (hexStr.equals("FFFFFF")) {
//                this.datas.put("" + gateCode, hexStr);
//            } else {
//                dataStr = new StringBuffer(hexStr);
//                dataStr.insert(dataStr.length() - 2, ".");
//                String value = StringUtil.getPrettyNumber(dataStr.toString());
//                this.datas.put("" + gateCode, value);
//            }
            count += 3;
            //count += valueLen.getByteLen();
            this.macth = true;
            this.valueLen = valueLen;
        }
        return count;
    }

    @Override
    public int stream(byte[] data, int index, int batchSize) {
        return 0;
    }

    @Override
    public String getStringValue() {
        return null;
    }

    @Override
    public void saveValue(Map<String, List<Map<String,Object>>> keyStore) {
        String gateValue = (String) this.datas.get("value");
        if (!gateValue.equals("FFFFFF")) {
            if (keyStore.containsKey(this.feture)) {
                List<Map<String,Object>> values = keyStore.get(this.feture);
                Map<String,Object> value = new HashMap<>();
                value.put(ElementDataMetadataEnum.ELEMENT_METADATA_CODE.getName(), this.feture);
                value.put(ElementDataMetadataEnum.ELEMENT_METADATA_NAME.getName(), this.name);
                value.put(ElementDataMetadataEnum.ELEMENT_METADATA_UNIT.getName(), this.unit);
                value.put(ElementDataMetadataEnum.ELEMENT_METADATA_DESC.getName(), this.desc);
                value.put(ElementDataMetadataEnum.ELEMENT_METADATA_CODE.getName(), (String) this.datas.get("gateCode"));
                StringBuffer dataStr = new StringBuffer(gateValue);
                dataStr.insert(dataStr.length() - 2, ".");
                BigDecimal decimal = BytesHelp.toDecimal(dataStr.toString(), 2);
                value.put(ElementDataMetadataEnum.ELEMENT_METADATA_VALUE.getName(), decimal.setScale(2,BigDecimal.ROUND_DOWN).toPlainString());
                values.add(value);
            } else {
                List<Map<String,Object>> values = new LinkedList<>();
                Map<String,Object> value = new HashMap<>();
                value.put(ElementDataMetadataEnum.ELEMENT_METADATA_CODE.getName(), this.feture);
                value.put(ElementDataMetadataEnum.ELEMENT_METADATA_NAME.getName(), this.name);
                value.put(ElementDataMetadataEnum.ELEMENT_METADATA_UNIT.getName(), this.unit);
                value.put(ElementDataMetadataEnum.ELEMENT_METADATA_DESC.getName(), this.desc);
                value.put(ElementDataMetadataEnum.ELEMENT_METADATA_CODE.getName(), (String) this.datas.get("gateCode"));
                StringBuffer dataStr = new StringBuffer(gateValue);
                dataStr.insert(dataStr.length() - 2, ".");
                BigDecimal decimal = BytesHelp.toDecimal(dataStr.toString(), 2);
                value.put(ElementDataMetadataEnum.ELEMENT_METADATA_VALUE.getName(), decimal.setScale(2,BigDecimal.ROUND_DOWN).toPlainString());
                values.add(value);
                keyStore.put(this.feture, values);
            }
        }
    }
}
