package org.ts.ddcs.itemdatatemplate.szy;


import org.ts.ddcs.metadata.szy.SzyFlowRateMetadataEnum;
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.*;

/**
 * 0x03
 * 自报 流量(水量)参数
 */
public class DataItemTemplateSzyFlowRate 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, 5);
        count += 5;
        if (!BytesHelp.byte2HexStr(this.valueBuff).equals("FFFFFFFFFF") && !BytesHelp.byte2HexStr(this.valueBuff).equals("AAAAAAAAAA")) {
            byte tmp = this.valueBuff[0];
            this.valueBuff[0] = this.valueBuff[4];
            this.valueBuff[4] = tmp;
            tmp = this.valueBuff[1];
            this.valueBuff[1] = this.valueBuff[3];
            this.valueBuff[3] = tmp;
            String value = BytesHelp.byte2HexStr(this.valueBuff);
            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);
            String v8 = value.substring(8, 9);
            String v9 = value.substring(9, 10);
            value = "";
            if (flag.equals("F")) {
                value += "-";
            }
            value += StringUtil.getPrettyNumber(v1 + v2 + v3 + v4 + v5 + v6 + "." + v7 + v8 + v9);
            this.varcharValue = value;
        }

        return count;
    }

    @Override
    public int stream(byte[] data, int index, int batchSize) {
        this.macth = true;
        this.dataList = new ArrayList<>();
        int count = 0;
        if (20 == batchSize) {
            //瞬时流量
            byte[] byteBuff = BytesHelp.subBytes(data, index + count, 5);
            if (!BytesHelp.byte2HexStr(byteBuff).equals("FFFFFFFFFF") && !BytesHelp.byte2HexStr(byteBuff).equals("AAAAAAAAAA")) {
                Map<String, Object> datas = new HashMap<>();
                byte tmp = byteBuff[0];
                byteBuff[0] = byteBuff[4];
                byteBuff[4] = tmp;
                tmp = byteBuff[1];
                byteBuff[1] = byteBuff[3];
                byteBuff[3] = tmp;
                String value = BytesHelp.byte2HexStr(byteBuff);
                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);
                String v8 = value.substring(8, 9);
                String v9 = value.substring(9, 10);

                value = "";
                if ("F".equals(flag)) {
                    value += "FF";
                }
                value += v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9;
                datas.put(SzyFlowRateMetadataEnum.METADATA_CODE_Q.getName(), BytesHelp.toDecimal(value, 3));
                this.dataList.add(datas);
            }
            count += 5;

            //累计流量
            byteBuff = BytesHelp.subBytes(data, index + count, 5);
            if (!BytesHelp.byte2HexStr(byteBuff).equals("FFFFFFFFFF") && !BytesHelp.byte2HexStr(byteBuff).equals("AAAAAAAAAA")) {
                Map<String, Object> datas = new HashMap<>();
                byte tmp = byteBuff[0];
                byteBuff[0] = byteBuff[4];
                byteBuff[4] = tmp;
                tmp = byteBuff[1];
                byteBuff[1] = byteBuff[3];
                byteBuff[3] = tmp;
                String value = BytesHelp.byte2HexStr(byteBuff);
                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);
                String v8 = value.substring(8, 9);
                String v9 = value.substring(9, 10);
                value = v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9;
                datas.put(SzyFlowRateMetadataEnum.METADATA_CODE_ACC_Q.getName(), BytesHelp.toLong(value));
                this.dataList.add(datas);
            }
            count += 5;
            //1小时用水量
            byteBuff = BytesHelp.subBytes(data, index + count, 5);
            if (!BytesHelp.byte2HexStr(byteBuff).equals("FFFFFFFFFF") && !BytesHelp.byte2HexStr(byteBuff).equals("AAAAAAAAAA")) {
                Map<String, Object> datas = new HashMap<>();
                byte tmp = byteBuff[0];
                byteBuff[0] = byteBuff[4];
                byteBuff[4] = tmp;
                tmp = byteBuff[1];
                byteBuff[1] = byteBuff[3];
                byteBuff[3] = tmp;
                String value = BytesHelp.byte2HexStr(byteBuff);
                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);
                String v8 = value.substring(8, 9);
                String v9 = value.substring(9, 10);
                value = v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9;
                datas.put(SzyFlowRateMetadataEnum.METADATA_CODE_HOUR_Q.getName(), BytesHelp.toLong(value));
                this.dataList.add(datas);
            }
            count += 5;
            //1小时累计流量
            byteBuff = BytesHelp.subBytes(data, index + count, 5);
            if (!BytesHelp.byte2HexStr(byteBuff).equals("FFFFFFFFFF") && !BytesHelp.byte2HexStr(byteBuff).equals("AAAAAAAAAA")) {
                Map<String, Object> datas = new HashMap<>();
                byte tmp = byteBuff[0];
                byteBuff[0] = byteBuff[4];
                byteBuff[4] = tmp;
                tmp = byteBuff[1];
                byteBuff[1] = byteBuff[3];
                byteBuff[3] = tmp;
                String value = BytesHelp.byte2HexStr(byteBuff);
                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);
                String v8 = value.substring(8, 9);
                String v9 = value.substring(9, 10);
                value = v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9;
                datas.put(SzyFlowRateMetadataEnum.METADATA_CODE_HOUR_ACC_Q.getName(), BytesHelp.toLong(value));
                this.dataList.add(datas);
            }
            count += 5;
        } else if (10 == batchSize) {

            //瞬时流量
            byte[] byteBuff = BytesHelp.subBytes(data, index + count, 5);
            if (!BytesHelp.byte2HexStr(byteBuff).equals("FFFFFFFFFF") && !BytesHelp.byte2HexStr(byteBuff).equals("AAAAAAAAAA")) {
                Map<String, Object> datas = new HashMap<>();
                byte tmp = byteBuff[0];
                byteBuff[0] = byteBuff[4];
                byteBuff[4] = tmp;
                tmp = byteBuff[1];
                byteBuff[1] = byteBuff[3];
                byteBuff[3] = tmp;
                String value = BytesHelp.byte2HexStr(byteBuff);
                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);
                String v8 = value.substring(8, 9);
                String v9 = value.substring(9, 10);
                value = "";
                if ("F".equals(flag)) {
                    value += "FF";
                }
                value += v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9;
                datas.put(SzyFlowRateMetadataEnum.METADATA_CODE_Q.getName(), BytesHelp.toDecimal(value, 3));
                this.dataList.add(datas);
            }
            count += 5;

            //累计流量
            byteBuff = BytesHelp.subBytes(data, index + count, 5);
            if (!BytesHelp.byte2HexStr(byteBuff).equals("FFFFFFFFFF") && !BytesHelp.byte2HexStr(byteBuff).equals("AAAAAAAAAA")) {
                Map<String, Object> datas = new HashMap<>();
                byte tmp = byteBuff[0];
                byteBuff[0] = byteBuff[4];
                byteBuff[4] = tmp;
                tmp = byteBuff[1];
                byteBuff[1] = byteBuff[3];
                byteBuff[3] = tmp;
                String value = BytesHelp.byte2HexStr(byteBuff);
                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);
                String v8 = value.substring(8, 9);
                String v9 = value.substring(9, 10);
                value = v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9;
                datas.put(SzyFlowRateMetadataEnum.METADATA_CODE_ACC_Q.getName(), BytesHelp.toLong(value));
                this.dataList.add(datas);
            }
            count += 5;

        } else {
            int size = batchSize / 5;
            for (int i = 0; i < size; i++) {
                byte[] byteBuff = BytesHelp.subBytes(data, index + count, 5);
                if (!BytesHelp.byte2HexStr(byteBuff).equals("FFFFFFFFFF") && !BytesHelp.byte2HexStr(byteBuff).equals("AAAAAAAAAA")) {
                    Map<String, Object> datas = new HashMap<>();
                    byte tmp = byteBuff[0];
                    byteBuff[0] = byteBuff[4];
                    byteBuff[4] = tmp;
                    tmp = byteBuff[1];
                    byteBuff[1] = byteBuff[3];
                    byteBuff[3] = tmp;
                    String value = BytesHelp.byte2HexStr(byteBuff);
                    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);
                    String v8 = value.substring(8, 9);
                    String v9 = value.substring(9, 10);
                    value = "";
                    if ("F".equals(flag)) {
                        value += "FF";
                    }
                    value += v1 + v2 + v3 + v4 + v5 + v6 + v7 + v8 + v9;
                    datas.put(SzyFlowRateMetadataEnum.METADATA_CODE_Q.getName(), BytesHelp.toDecimal(value, 3));
                    this.dataList.add(datas);
                }
                count += 5;
            }
        }
        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<>();
        int size = this.dataList.size();
        for (int i = 0; i < size; i++) {
            Map<String, Object> datas = this.dataList.get(i);
            Set<String> keys = datas.keySet();
            for (String key : keys) {
                if (SzyFlowRateMetadataEnum.METADATA_CODE_Q.getName().equals(key)) {
                    BigDecimal elementValue = (BigDecimal) datas.get(key);
                    values.add(ElementJsonPackHelp.toJson(this.feture, key, String.valueOf(i + 1), elementValue.toPlainString(), "立方米每秒", "瞬时流量"));
                } else if (SzyFlowRateMetadataEnum.METADATA_CODE_ACC_Q.getName().equals(key)) {
                    Long elementValue = (Long) datas.get(key);
                    values.add(ElementJsonPackHelp.toJson(this.feture, key, String.valueOf(i + 1), elementValue.toString(), "立方米", "累计流量"));
                } else if (SzyFlowRateMetadataEnum.METADATA_CODE_HOUR_Q.getName().equals(key)) {
                    Long elementValue = (Long) datas.get(key);
                    values.add(ElementJsonPackHelp.toJson(this.feture, key, String.valueOf(i + 1), elementValue.toString(), "立方米", "1小时用水量"));
                } else if (SzyFlowRateMetadataEnum.METADATA_CODE_HOUR_ACC_Q.getName().equals(key)) {
                    Long elementValue = (Long) datas.get(key);
                    values.add(ElementJsonPackHelp.toJson(this.feture, key, String.valueOf(i + 1), elementValue.toString(), "立方米", "1小时累计流量"));
                }
            }
        }
        keyStore.put(this.feture, values);
    }

}
