package org.ts.ddcs.itemdatatemplate.sw;


import com.alibaba.fastjson.JSONObject;
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;

/**
 * 0xF4
 * 1小时内每5分时段降雨量  0.1 毫米
 * 12 字节 HEX
 */
@Slf4j
public class DataItemTemplateDRP extends DataItemTemplate {

    @Override
    public int stream(byte[] data, int index) {
        log.info("template {}", this.getClass().getName());
        this.macth = false;
        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;
            this.valueBuff = BytesHelp.subBytes(data, index + count, 12);
            count += 12;
            this.macth = true;
        }
        return count;
    }

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

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

    @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 (int i = 0; i < 12; i++) {
                Map<String,Object> value = new HashMap<>();
                value.put(ElementDataMetadataEnum.ELEMENT_METADATA_CODE.getName(), Integer.valueOf(i + 1).toString());
                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);
                if ("FF".equals(BytesHelp.byte2HexStr(BytesHelp.subBytes(this.valueBuff, i, 1)))) {
                    value.put(ElementDataMetadataEnum.ELEMENT_METADATA_VALUE.getName(), "");
                } else {
                    BigDecimal v = BigDecimal.valueOf((double) this.valueBuff[i] * 0.1);
                    value.put(ElementDataMetadataEnum.ELEMENT_METADATA_VALUE.getName(), v.setScale(1, BigDecimal.ROUND_DOWN).toPlainString());
                }
                values.add(value);
            }
        } else {
            List<Map<String,Object>> values = new LinkedList<>();
            for (int i = 0; i < 12; i++) {
                Map<String,Object> value = new HashMap<>();
                value.put(ElementDataMetadataEnum.ELEMENT_METADATA_CODE.getName(), Integer.valueOf(i + 1).toString());
                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);
                if ("FF".equals(BytesHelp.byte2HexStr(BytesHelp.subBytes(this.valueBuff, i, 1)))) {
                    value.put(ElementDataMetadataEnum.ELEMENT_METADATA_VALUE.getName(), "");
                } else {
                    BigDecimal v = BigDecimal.valueOf((double) this.valueBuff[i] * 0.1);
                    value.put(ElementDataMetadataEnum.ELEMENT_METADATA_VALUE.getName(), v.setScale(1, BigDecimal.ROUND_DOWN).toPlainString());
                }
                values.add(value);
            }
            keyStore.put(this.feture, values);
        }
    }
}
