package org.ts.ddcs.itemdatatemplate.szy;

import org.ts.ddcs.common.metadata.ElementDataMetadataEnum;
import org.ts.ddcs.metadata.szy.SzyWaterLevelMetadataEnum;
import org.ts.ddcs.common.util.BytesHelp;
import org.ts.ddcs.common.util.StringUtil;
import org.ts.ddcs.itemdatatemplate.DataItemTemplate;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 0x02
 * 自报 水位参数
 */
public class DataItemTemplateSzyWaterLevel extends DataItemTemplate {


    @Override
    public int stream(byte[] data, int index) throws UnsupportedEncodingException {
        this.macth = true;
        int count = 0;
        this.valueBuff = BytesHelp.subBytes(data, index+count, 4);
        count += 4;
        byte tmp = this.valueBuff[0];
        this.valueBuff[0] = this.valueBuff[3];
        this.valueBuff[3] = tmp;
        tmp = this.valueBuff[1];
        this.valueBuff[1] = this.valueBuff[2];
        this.valueBuff[2] = tmp;
        String value = BytesHelp.byte2HexStr(this.valueBuff);
        if (value.equals("FFFFFFFF")) {
            this.varcharValue = "FFFFFFFF";
        } else {
            String flag = value.substring(0, 1);
            String v1 = value.substring(1, 2);
            String v2 = value.substring(2, 3);
            String v3 = value.substring(3, 4);
            String v4 = value.substring(4, 5);
            String v5 = value.substring(5, 6);
            String v6 = value.substring(6, 7);
            String v7 = value.substring(7, 8);
            value = "";
            if (flag.equals("F")) {
                value += "-";
            }
            value += StringUtil.getPrettyNumber(v1 + v2 + v3 + v4 + "." + v5 + v6 + v7);
            this.varcharValue = value;
        }


        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);
            Map<String, Object> value = new HashMap<>();
            value.put(ElementDataMetadataEnum.ELEMENT_METADATA_CODE.getName(), this.feture);
            value.put(ElementDataMetadataEnum.ELEMENT_METADATA_NAME.getName(), SzyWaterLevelMetadataEnum.METADATA_CODE_LEVEL.getName());
            value.put(ElementDataMetadataEnum.ELEMENT_METADATA_UNIT.getName(), this.unit);
            value.put(ElementDataMetadataEnum.ELEMENT_METADATA_DESC.getName(), this.desc);
            if (null != this.varcharValue) {
                value.put(ElementDataMetadataEnum.ELEMENT_METADATA_VALUE.getName(), this.varcharValue);
            }
            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(),  SzyWaterLevelMetadataEnum.METADATA_CODE_LEVEL.getName());
            value.put(ElementDataMetadataEnum.ELEMENT_METADATA_UNIT.getName(), this.unit);
            value.put(ElementDataMetadataEnum.ELEMENT_METADATA_DESC.getName(), this.desc);
            if (null != this.varcharValue) {
                value.put(ElementDataMetadataEnum.ELEMENT_METADATA_VALUE.getName(), this.varcharValue);
            }
            values.add(value);
            keyStore.put(this.feture, values);
        }

    }
}
