package org.ts.ddcs.itemdatatemplate.sw;


import org.ts.ddcs.common.metadata.ElementDataMetadataEnum;
import org.ts.ddcs.common.util.BytesHelp;
import org.ts.ddcs.common.util.StringUtil;
import org.ts.ddcs.common.util.Sw2014DatagramTemplateHelp;
import org.ts.ddcs.common.util.ValueLen;
import org.ts.ddcs.itemdatatemplate.DataItemTemplate;

import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * 0xFF57
 * 上报状态（每一路的上报状态）
 * 连续6组，(1+5),1组总包索引，5组已发索引
 */
public class DataItemTemplateFF57 extends DataItemTemplate {


    @Override
    public int stream(byte[] data, int index) throws UnsupportedEncodingException {
        this.macth = false;
        this.intValues = 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.valueBuff = BytesHelp.subBytes(data, index + count, valueLen.getByteLen());
            if (null != this.valueBuff && this.valueBuff.length == 12) {
                for (int i = 0; i < 6; i++) {
                    byte[] valueBuff = BytesHelp.subBytes(this.valueBuff, i * 2, 2);
                    String hexStr = BytesHelp.byte2HexStr(valueBuff);
                    if ("FFFF".equals(hexStr)) {
                        //FFFF代表此路IP无效
                        this.intValues.add(-1);
                    } else {
                        String value = StringUtil.getPrettyNumber(hexStr);
                        this.intValues.add(Integer.parseInt(value));
                    }
                }
            }
            count += valueLen.getByteLen();
            this.valueLen = valueLen;
            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);
            if (this.intValues.size() == 6) {
                for (int i = 0; i < 6; i++) {
                    Integer v = this.intValues.get(i);
                    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(), v.toString());
                    value.put(ElementDataMetadataEnum.ELEMENT_METADATA_CODE.getName(), Integer.valueOf(i+1).toString());
                    values.add(value);
                }
            }
        } else {
            List<Map<String,Object>> values = new LinkedList<>();
            if (this.intValues.size() == 6) {
                for (int i = 0; i < 6; i++) {
                    Integer v = this.intValues.get(i);
                    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(), v.toString());
                    value.put(ElementDataMetadataEnum.ELEMENT_METADATA_CODE.getName(), Integer.valueOf(i+1).toString());
                    values.add(value);
                }
            }
            keyStore.put(this.feture, values);
        }
    }
}
