package org.ts.ddcs.itemdatatemplate.sw;


import org.ts.ddcs.common.metadata.ElementDataMetadataEnum;
import org.ts.ddcs.common.util.BytesHelp;
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;

/**
 * 0xF2
 * 人工置数
 */
public class DataItemTemplateRGZS extends DataItemTemplate {


    @Override
    public int stream(byte[] data, int index) throws UnsupportedEncodingException {
        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, data.length - index - count);
            count += this.valueBuff.length;
            //this.valueLen = valueLen;
            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);
            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_VALUE.getName(), BytesHelp.bytes2String(this.valueBuff, 0, this.valueBuff.length));
            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_VALUE.getName(), BytesHelp.bytes2String(this.valueBuff, 0, this.valueBuff.length));
            values.add(value);
            keyStore.put(this.feture, values);
        }
    }
}
