package org.ts.ddcs.itemdatatemplate.szy;

import lombok.extern.slf4j.Slf4j;
import org.ts.ddcs.metadata.szy.SzyWarnMetadataEnum;
import org.ts.ddcs.common.util.BytesHelp;
import org.ts.ddcs.common.util.ElementJsonPackHelp;
import org.ts.ddcs.itemdatatemplate.DataItemTemplate;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;


/**
 * 0x13
 * 自报 报警状态
 */
@Slf4j
public class DataItemTemplateSzyWarnStatus extends DataItemTemplate {


    @Override
    public int stream(byte[] data, int index) throws UnsupportedEncodingException {
        log.info("template {}", this.getClass().getName());
        this.macth = true;
        this.intValues = new ArrayList<>();
        int count = 0;
        byte[] warnBuff = BytesHelp.subBytes(data, index + count, 2);
        byte tmpbyte = warnBuff[0];
        warnBuff[0] = warnBuff[1];
        warnBuff[1] = tmpbyte;
        String binStr = BytesHelp.BytesToBinStr(warnBuff);
        binStr = new StringBuilder(binStr).reverse().toString();
        for (int i = 0; i < 14; i++) {
            this.intValues.add(Integer.parseInt(binStr.substring(i, i + 1)));
            index += 1;
        }
        count += 2;
        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) {
        List<Map<String,Object>> values = new LinkedList<>();
        for (int i = 0; i < this.intValues.size(); i++) {
            int v = this.intValues.get(i);
            if (i == 0) {
                values.add(ElementJsonPackHelp.toJson(this.feture, SzyWarnMetadataEnum.METADATA_AC_WARN.getName(), "", String.valueOf(v), "", "工作交流电停电告警"));
            } else if (i == 1) {
                values.add(ElementJsonPackHelp.toJson(this.feture, SzyWarnMetadataEnum.METADATA_BT_V_WARN.getName(), "", String.valueOf(v), "", "蓄电池电压报警"));
            } else if (i == 2) {
                values.add(ElementJsonPackHelp.toJson(this.feture, SzyWarnMetadataEnum.METADATA_WATER_LEVEL_OUT_WARN.getName(), "", String.valueOf(v), "", "水位超限报警"));
            } else if (i == 3) {
                values.add(ElementJsonPackHelp.toJson(this.feture, SzyWarnMetadataEnum.METADATA_FLOW_OUT_WARN.getName(), "", String.valueOf(v), "", "流量超限报警"));
            } else if (i == 4) {
                values.add(ElementJsonPackHelp.toJson(this.feture, SzyWarnMetadataEnum.METADATA_WATER_QUALITY_WARN.getName(), "", String.valueOf(v), "", "水质超限报警"));
            } else if (i == 5) {
                values.add(ElementJsonPackHelp.toJson(this.feture, SzyWarnMetadataEnum.METADATA_FLOW_SENSOR_WARN.getName(), "", String.valueOf(v), "", "流量仪表故障报警"));
            } else if (i == 6) {
                values.add(ElementJsonPackHelp.toJson(this.feture, SzyWarnMetadataEnum.METADATA_WATER_PUMP_STATUS.getName(), "", String.valueOf(v), "", "水泵开停状态"));
            } else if (i == 7) {
                values.add(ElementJsonPackHelp.toJson(this.feture, SzyWarnMetadataEnum.METADATA_WATER_LEVEL_SENSOR_WARN.getName(), "", String.valueOf(v), "", "水位仪表故障报警"));
            } else if (i == 8) {
                values.add(ElementJsonPackHelp.toJson(this.feture, SzyWarnMetadataEnum.METADATA_WATER_PRESURE.getName(), "", String.valueOf(v), "", "水压超限报警"));
            } else if (i == 9) {
                values.add(ElementJsonPackHelp.toJson(this.feture, SzyWarnMetadataEnum.METADATA_RESERVE.getName(), "", String.valueOf(v), "", "备用"));
            } else if (i == 10) {
                values.add(ElementJsonPackHelp.toJson(this.feture, SzyWarnMetadataEnum.METADATA_IC_WARN.getName(), "", String.valueOf(v), "", "终端IC卡功能报警"));
            } else if (i == 11) {
                values.add(ElementJsonPackHelp.toJson(this.feture, SzyWarnMetadataEnum.METADATA_CONTROL_WARN.getName(), "", String.valueOf(v), "", "定值控制报警"));
            } else if (i == 12) {
                values.add(ElementJsonPackHelp.toJson(this.feture, SzyWarnMetadataEnum.METADATA_SURPLUS_WATER.getName(), "", String.valueOf(v), "", "剩余水量的下限报警"));
            } else if (i == 13) {
                values.add(ElementJsonPackHelp.toJson(this.feture, SzyWarnMetadataEnum.METADATA_DOOR_WARN.getName(), "", String.valueOf(v), "", "终端箱门状态报警"));
            }
        }
        keyStore.put(this.feture, values);
    }
}
