package org.ts.ddcs.itemdatatemplate.sw;


import com.alibaba.fastjson.JSONObject;
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.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * FF28
 * 立方米/秒 N(9,3)
 * 取（排）水口流量1（累计值）
 */
public class DataItemTemplateEXT_28 extends DataItemTemplate {


    @Override
    public int stream(byte[] data, int index) {
        this.macth = false;
        this.bigDecimalValues = new ArrayList<>();
        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;
            this.readBigDecimalValue(data, index + count, valueLen);
            count += valueLen.getByteLen();
            this.macth = true;
        }
        return count;
    }

    @Override
    public int stream(byte[] data, int index, int batchSize) {
        this.macth = false;
        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;
            for (int i = 0; i < batchSize; i++) {
                this.readBigDecimalValue(data, index + count, valueLen);
                count += valueLen.getByteLen();
            }
            this.macth = true;
        }
        return count;
    }

    @Override
    public String getStringValue() {
        BigDecimal value = this.bigDecimalValues.get(0);
        return value.setScale(3, BigDecimal.ROUND_DOWN).toPlainString();
    }

    @Override
    public void saveValue(Map<String, List<Map<String,Object>>> keyStore) {
        this.saveBigDecimalValues(keyStore, 3);
    }
}
