package org.ts.ddcs.itemdatatemplate.sw;


import lombok.extern.slf4j.Slf4j;
import org.ts.ddcs.common.metadata.ElementDataMetadataEnum;
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.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;


/**
 * funcode 0x58
 * 管道水压 N(5,2)
 */
@Slf4j
public class DataItemTemplate58 extends DataItemTemplate {

    @Override
    public int stream(byte[] data, int index) {
        log.info("item template name{}", this.getClass().getName());
        this.macth = false;
        this.valueList.clear();
        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;
            Map<String, Object> item = new HashMap<>();
            item.put(ElementDataMetadataEnum.ELEMENT_METADATA_ERROR.getName(), false);
            BigDecimal value = this.readBigDecimalValue(data, index + count, valueLen);
            if (null != value) {
                String valueText = value.setScale(2, BigDecimal.ROUND_DOWN).toPlainString();
                item.put(ElementDataMetadataEnum.ELEMENT_METADATA_VALUE.getName(), valueText);
            }
            this.valueList.add(item);
            count += valueLen.getByteLen();
            this.macth = true;
        }
        return count;
    }

    @Override
    public int stream(byte[] data, int index, int batchSize) {
        this.macth = false;
        this.valueList.clear();
        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;
            for (int i = 0; i < batchSize; i++) {
                Map<String, Object> item = new HashMap<>();
                item.put(ElementDataMetadataEnum.ELEMENT_METADATA_ERROR.getName(), false);
                BigDecimal value = this.readBigDecimalValue(data, index + count, valueLen);
                if (null != value) {
                    String valueText = value.setScale(2, BigDecimal.ROUND_DOWN).toPlainString();
                    item.put(ElementDataMetadataEnum.ELEMENT_METADATA_VALUE.getName(), valueText);
                }
                this.valueList.add(item);
                count += valueLen.getByteLen();
            }
            this.macth = true;
        }
        return count;
    }

    @Override
    public String getStringValue() {
        StringBuilder builder = new StringBuilder();
        for (Map<String, Object> item : this.valueList) {
            if (builder.length() > 0) {
                builder.append(",");
            }
            builder.append(item.get(ElementDataMetadataEnum.ELEMENT_METADATA_VALUE.getName()));
        }
        return builder.toString();
    }

    @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);
            for (Map<String, Object> item : this.valueList) {
                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(), item.get(ElementDataMetadataEnum.ELEMENT_METADATA_VALUE.getName()));
                values.add(value);
            }
        } else {
            List<Map<String, Object>> values = new LinkedList<>();
            for (Map<String, Object> item : this.valueList) {
                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(), item.get(ElementDataMetadataEnum.ELEMENT_METADATA_VALUE.getName()));
                values.add(value);
            }
            keyStore.put(this.feture, values);
        }
    }
}
