package org.ts.ddcs.itemdatatemplate.szy;


import org.ts.ddcs.metadata.szy.SzyFlowRateMetadataEnum;
import org.ts.ddcs.metadata.szy.SzyFlowSpeedMetadataEnum;
import org.ts.ddcs.common.util.BytesHelp;
import org.ts.ddcs.common.util.ElementJsonPackHelp;
import org.ts.ddcs.common.util.StringUtil;
import org.ts.ddcs.itemdatatemplate.DataItemTemplate;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * 0x04
 * 自报 流速参数
 */
public class DataItemTemplateSzyFlowSpeed 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, 3);
        count += 3;
        byte tmp = this.valueBuff[0];
        this.valueBuff[0] = this.valueBuff[2];
        this.valueBuff[2] = tmp;
        String value = BytesHelp.byte2HexStr(this.valueBuff);
        if ("FFFFFF".equals(value) || "AAAAAA".equals(value)) {

        } 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);
            value = "";
            if (flag.equals("F")) {
                value += "-";
            }
            value += StringUtil.getPrettyNumber(v1 + v2 + "." + v3 + v4 + v5);
            this.bigDecimalValues.add(new BigDecimal(value));
        }
        return count;
    }

    @Override
    public int stream(byte[] data, int index, int batchSize) {
        this.macth = true;
        int count = 0;
        int size = batchSize / 3;
        for (int i = 0; i < size; i++) {
            this.valueBuff = BytesHelp.subBytes(data, index + count, 3);
            count += 3;
            byte tmp = this.valueBuff[0];
            this.valueBuff[0] = this.valueBuff[2];
            this.valueBuff[2] = tmp;
            String value = BytesHelp.byte2HexStr(this.valueBuff);
            if ("FFFFFF".equals(value) || "AAAAAA".equals(value)) {

            } 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);
                value = "";
                if (flag.equals("F")) {
                    value += "-";
                }
                value += StringUtil.getPrettyNumber(v1 + v2 + "." + v3 + v4 + v5);
                this.bigDecimalValues.add(new BigDecimal(value));
            }
        }
        return count;
    }

    @Override
    public String getStringValue() {
        return null;
    }

    @Override
    public void saveValue(Map<String, List<Map<String, Object>>> keyStore) {
        List<Map<String, Object>> values = new LinkedList<>();
        for (int i = 0; i < this.bigDecimalValues.size(); i++) {
            BigDecimal v = this.bigDecimalValues.get(i);
            values.add(ElementJsonPackHelp.toJson(this.feture, SzyFlowSpeedMetadataEnum.METADATA_CODE_FLOWSPEED.getName(), String.valueOf(i + 1), v.toPlainString(), "", "流速参数"));
        }
        keyStore.put(this.feture, values);
    }
}
