package org.ts.ddcs.itemdatatemplate.szy;

import com.alibaba.fastjson.JSONObject;
import org.ts.ddcs.common.metadata.ElementDataMetadataEnum;
import org.ts.ddcs.common.util.BytesHelp;
import org.ts.ddcs.itemdatatemplate.DataItemTemplate;

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

/**
 * 427
 * 0xFFF0
 * 由 7 字节组成 格式 ssmmHHDD 秒分时日月年
 * 观测时间
 */
public class DataItemTemplateSzyTT427 extends DataItemTemplate {

    @Override
    public int stream(byte[] data, int index) {
        this.macth = true;
        int count = 0;
        byte[] timeBuff = BytesHelp.subBytes(data, index + count, 6);
        byte[] tmpBuff = new byte[6];
        tmpBuff[0] = timeBuff[5];
        tmpBuff[1] = timeBuff[4];
        tmpBuff[2] = timeBuff[3];
        tmpBuff[3] = timeBuff[2];
        tmpBuff[4] = timeBuff[1];
        tmpBuff[5] = timeBuff[0];
        this.varcharValue = BytesHelp.byte2HexStr(tmpBuff);
        count += 7;
        return count;
    }

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


    @Override
    public String getStringValue() {
        return this.varcharValue;
    }

    @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(), 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(), 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(), this.varcharValue);
            values.add(value);
            keyStore.put(this.feture, values);
        }
    }

}
