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.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * F6
 * 0.01 米 24 字节 HEX
 * 1小时内5分钟间隔相对水位2;河道水位、闸坝（泵）站下水位
 */
public class DataItemTemplateDRZ2 extends DataItemTemplate {


    @Override
    public int stream(byte[] data, int index) {
        this.macth = false;
        int count = 0;
        String flag = BytesHelp.byte2HexStr(data, index + count, 1);
        if (flag.toUpperCase().equals(this.feture)) {
            count += 1;
            ValueLen valueLen = Sw2014DatagramTemplateHelp.getValueLen(data, index + count);
            count += 1;
            this.valueBuff = BytesHelp.subBytes(data, index + count, 24);
            count += 24;
            this.macth = true;
        }
        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) {
        if (keyStore.containsKey(this.feture)) {
            List<Map<String,Object>> values = keyStore.get(this.feture);
            byte[] drz = new byte[2];
            for (int i = 0; i < 12; i++) {
                drz[0] = this.valueBuff[i * 2];
                drz[1] = this.valueBuff[i * 2 + 1];
                Map<String,Object> value = new HashMap<>();
                value.put(ElementDataMetadataEnum.ELEMENT_METADATA_CODE.getName(), Integer.valueOf(i + 1).toString());
                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);
                if ("FFFF".equals(BytesHelp.byte2HexStr(drz))) {
                    value.put(ElementDataMetadataEnum.ELEMENT_METADATA_VALUE.getName(), "");
                } else {
                    short intValue = BytesHelp.bytes2short(drz);
                    BigDecimal v = BigDecimal.valueOf((double) intValue * (double) 0.01);
                    value.put(ElementDataMetadataEnum.ELEMENT_METADATA_VALUE.getName(), v.setScale(2, BigDecimal.ROUND_DOWN).toPlainString());
                }
                values.add(value);
            }
        } else {
            List<Map<String,Object>> values = new LinkedList<>();
            byte[] drz = new byte[2];
            for (int i = 0; i < 12; i++) {
                drz[0] = this.valueBuff[i * 2];
                drz[1] = this.valueBuff[i * 2 + 1];
                Map<String,Object> value = new HashMap<>();
                value.put(ElementDataMetadataEnum.ELEMENT_METADATA_CODE.getName(), Integer.valueOf(i + 1).toString());
                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);
                if ("FFFF".equals(BytesHelp.byte2HexStr(drz))) {
                    value.put(ElementDataMetadataEnum.ELEMENT_METADATA_VALUE.getName(), "");
                } else {
                    short intValue = BytesHelp.bytes2short(drz);
                    BigDecimal v = BigDecimal.valueOf((double) intValue * (double) 0.01);
                    value.put(ElementDataMetadataEnum.ELEMENT_METADATA_VALUE.getName(), v.setScale(2, BigDecimal.ROUND_DOWN).toPlainString());
                }
                values.add(value);
            }
            keyStore.put(this.feture, values);
        }
    }

//    @Override
//    public String toString() {
//        String viewInfo = "";
//        if (null != this.datas && this.datas.size() == 12) {
//            BigDecimal v = (BigDecimal) this.datas.get("drz2_1");
//            v =v.setScale(2, RoundingMode.HALF_UP);//保留两位小数
//            viewInfo += "0分=" + v.toPlainString()+"米";
//            v =v.setScale(2, RoundingMode.HALF_UP);//保留两位小数
//            v = (BigDecimal) this.datas.get("drz2_2");
//            v =v.setScale(2, RoundingMode.HALF_UP);//保留两位小数
//            viewInfo += " 5分=" + v.toPlainString()+"米";
//            v = (BigDecimal) this.datas.get("drz2_3");
//            v =v.setScale(2, RoundingMode.HALF_UP);//保留两位小数
//            viewInfo += " 10分=" + v.toPlainString()+"米";
//            v = (BigDecimal) this.datas.get("drz2_4");
//            v =v.setScale(2, RoundingMode.HALF_UP);//保留两位小数
//            viewInfo += " 15分=" + v.toPlainString()+"米";
//            v = (BigDecimal) this.datas.get("drz2_5");
//            v =v.setScale(2, RoundingMode.HALF_UP);//保留两位小数
//            viewInfo += " 20分=" + v.toPlainString()+"米";
//            v = (BigDecimal) this.datas.get("drz2_6");
//            v =v.setScale(2, RoundingMode.HALF_UP);//保留两位小数
//            viewInfo += " 25分=" + v.toPlainString()+"米";
//            v = (BigDecimal) this.datas.get("drz2_7");
//            v =v.setScale(2, RoundingMode.HALF_UP);//保留两位小数
//            viewInfo += " 30分=" + v.toPlainString()+"米";
//            v = (BigDecimal) this.datas.get("drz2_8");
//            v =v.setScale(2, RoundingMode.HALF_UP);//保留两位小数
//            viewInfo += " 35分=" + v.toPlainString()+"米";
//            v = (BigDecimal) this.datas.get("drz2_9");
//            v =v.setScale(2, RoundingMode.HALF_UP);//保留两位小数
//            viewInfo += " 40分=" + v.toPlainString()+"米";
//            v = (BigDecimal) this.datas.get("drz2_10");
//            v =v.setScale(2, RoundingMode.HALF_UP);//保留两位小数
//            viewInfo += " 45分=" + v.toPlainString()+"米";
//            v = (BigDecimal) this.datas.get("drz2_11");
//            v =v.setScale(2, RoundingMode.HALF_UP);//保留两位小数
//            viewInfo += " 50分=" + v.toPlainString()+"米";
//            v = (BigDecimal) this.datas.get("drz2_12");
//            v =v.setScale(2, RoundingMode.HALF_UP);//保留两位小数
//            viewInfo += " 55分=" + v.toPlainString()+"米";
//        }
//        return viewInfo;
//    }
}
