package org.ts.ddcs.itemdatatemplate.szy;

import lombok.extern.slf4j.Slf4j;
import org.ts.ddcs.metadata.szy.SzyWaterQualityMetadataEnum;
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.util.*;

/**
 * 0x10
 * 自报 水质参数
 */
@Slf4j
public class DataItemTemplateSzyWaterQuality extends DataItemTemplate {


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

    @Override
    public int stream(byte[] data, int index, int batchSize) {
        log.info("template {}", this.getClass().getName());
        this.macth = true;
        this.dataList = new ArrayList<>();
        int count = 0;
        while (true) {
            if (count >= batchSize) {
                break;
            }
            Map<String, Object> datas = new HashMap<>();
            //5＋N*4个字节
            byte[] flag = BytesHelp.subBytes(data, index + count, 5);
            count += 5;
            byte[] dataFlag = new byte[40];
            for (int i = 0; i < flag.length; i++) {
                int d0 = BytesHelp.bcdToInt(flag[i], 0, 0);
                dataFlag[i * 8] = (byte) d0;
                int d1 = BytesHelp.bcdToInt(flag[i], 1, 1);
                dataFlag[i * 8 + 1] = (byte) d1;
                int d2 = BytesHelp.bcdToInt(flag[i], 2, 2);
                dataFlag[i * 8 + 2] = (byte) d2;
                int d3 = BytesHelp.bcdToInt(flag[i], 3, 3);
                dataFlag[i * 8 + 3] = (byte) d3;
                int d4 = BytesHelp.bcdToInt(flag[i], 4, 4);
                dataFlag[i * 8 + 4] = (byte) d4;
                int d5 = BytesHelp.bcdToInt(flag[i], 5, 5);
                dataFlag[i * 8 + 5] = (byte) d5;
                int d6 = BytesHelp.bcdToInt(flag[i], 6, 6);
                dataFlag[i * 8 + 6] = (byte) d6;
                int d7 = BytesHelp.bcdToInt(flag[i], 7, 7);
                dataFlag[i * 8 + 7] = (byte) d7;
            }
            for (int i = 0; i < dataFlag.length; i++) {
                if (dataFlag[i] == 1) {
                    if (i == 24) {
                        //含粪大肠菌群,5个字节
                        this.valueBuff = new byte[5];
                        this.valueBuff[0] = data[index + count + 4];
                        this.valueBuff[1] = data[index + count + 3];
                        this.valueBuff[2] = data[index + count + 2];
                        this.valueBuff[3] = data[index + count + 1];
                        this.valueBuff[4] = data[index + count];
                        count += 5;
                    } else {
                        this.valueBuff = new byte[4];
                        this.valueBuff[0] = data[index + count + 3];
                        this.valueBuff[1] = data[index + count + 2];
                        this.valueBuff[2] = data[index + count + 1];
                        this.valueBuff[3] = data[index + count];
                        count += 4;
                    }
                    String value = BytesHelp.byte2HexStr(this.valueBuff);
                    if (i == 0) {
                        //水温
                        String elementValue = value.substring(0, value.length() - 1) + "." + value.substring(value.length() - 1, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_WT.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 1) {
                        //pH 值
                        String elementValue = value.substring(0, value.length() - 2) + "." + value.substring(value.length() - 2, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_PH.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 2) {
                        //溶解氧
                        String elementValue = value.substring(0, value.length() - 2) + "." + value.substring(value.length() - 2, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_DOX.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 3) {
                        //高锰酸盐指数
                        String elementValue = value.substring(0, value.length() - 2) + "." + value.substring(value.length() - 2, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_CODMN.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 4) {
                        //电导率
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_COND.getName(), StringUtil.getPrettyNumber(value));
                    } else if (i == 5) {
                        //氧化还原电位
                        String elementValue = value.substring(0, value.length() - 1) + "." + value.substring(value.length() - 1, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_ORP.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 6) {
                        //浊度
                        this.datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_TURB.getName(), StringUtil.getPrettyNumber(value));
                    } else if (i == 7) {
                        //化学需氧量
                        String elementValue = value.substring(0, value.length() - 1) + "." + value.substring(value.length() - 1, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_CODCR.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 8) {
                        //五日生化需氧量
                        String elementValue = value.substring(0, value.length() - 1) + "." + value.substring(value.length() - 1, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_CODCR_5D.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 9) {
                        //氨氮
                        String elementValue = value.substring(0, value.length() - 2) + "." + value.substring(value.length() - 2, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_NH3N.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 10) {
                        //总氮
                        String elementValue = value.substring(0, value.length() - 2) + "." + value.substring(value.length() - 2, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_TN.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 11) {
                        //铜
                        String elementValue = value.substring(0, value.length() - 4) + "." + value.substring(value.length() - 4, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_CU.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 12) {
                        //锌
                        String elementValue = value.substring(0, value.length() - 4) + "." + value.substring(value.length() - 4, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_ZN.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 13) {
                        //氟化物
                        String elementValue = value.substring(0, value.length() - 2) + "." + value.substring(value.length() - 2, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_F.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 14) {
                        //硒
                        String elementValue = value.substring(0, value.length() - 5) + "." + value.substring(value.length() - 5, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_SE.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 15) {
                        //砷
                        String elementValue = value.substring(0, value.length() - 5) + "." + value.substring(value.length() - 5, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_ARS.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 16) {
                        //汞
                        String elementValue = value.substring(0, value.length() - 5) + "." + value.substring(value.length() - 5, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_HG.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 17) {
                        //镉
                        String elementValue = value.substring(0, value.length() - 5) + "." + value.substring(value.length() - 5, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_CD.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 18) {
                        //六价铬
                        String elementValue = value.substring(0, value.length() - 3) + "." + value.substring(value.length() - 3, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_CR6.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 19) {
                        //铅
                        String elementValue = value.substring(0, value.length() - 5) + "." + value.substring(value.length() - 5, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_PB.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 20) {
                        //氰化物
                        String elementValue = value.substring(0, value.length() - 3) + "." + value.substring(value.length() - 3, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_CYN.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 21) {
                        //挥发酚
                        String elementValue = value.substring(0, value.length() - 3) + "." + value.substring(value.length() - 3, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_VH.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 22) {
                        //苯酚
                        String elementValue = value.substring(0, value.length() - 2) + "." + value.substring(value.length() - 2, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_C6H5OH.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 23) {
                        //硫化物
                        String elementValue = value.substring(0, value.length() - 3) + "." + value.substring(value.length() - 3, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_COS.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 24) {
                        //粪大肠菌群
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_EPEC.getName(), StringUtil.getPrettyNumber(value));
                    } else if (i == 25) {
                        //硫酸盐
                        String elementValue = value.substring(0, value.length() - 2) + "." + value.substring(value.length() - 2, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_NaSO4.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 26) {
                        //氯化物
                        String elementValue = value.substring(0, value.length() - 2) + "." + value.substring(value.length() - 2, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_CL.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 27) {
                        //硝酸盐氮
                        String elementValue = value.substring(0, value.length() - 2) + "." + value.substring(value.length() - 2, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_NO3.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 28) {
                        //铁
                        String elementValue = value.substring(0, value.length() - 2) + "." + value.substring(value.length() - 2, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_FE.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 29) {
                        //锰
                        String elementValue = value.substring(0, value.length() - 2) + "." + value.substring(value.length() - 2, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_MN.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 30) {
                        //石油类
                        String elementValue = value.substring(0, value.length() - 2) + "." + value.substring(value.length() - 2, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_OIL.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 31) {
                        //阴离子表面活性剂
                        String elementValue = value.substring(0, value.length() - 2) + "." + value.substring(value.length() - 2, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_AS.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 32) {
                        //六六六
                        String elementValue = value.substring(0, value.length() - 6) + "." + value.substring(value.length() - 6, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_SIXSIXSIX.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 33) {
                        //滴滴涕
                        String elementValue = value.substring(0, value.length() - 6) + "." + value.substring(value.length() - 6, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_DDT.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 34) {
                        //有机氯农药
                        String elementValue = value.substring(0, value.length() - 6) + "." + value.substring(value.length() - 6, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_OCPs.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 35) {
                        //总磷
                        String elementValue = value.substring(0, value.length() - 3) + "." + value.substring(value.length() - 3, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_TP.getName(), StringUtil.getPrettyNumber(elementValue));
                    } else if (i == 36) {
                        //叶绿素
                        String elementValue = value.substring(0, value.length() - 3) + "." + value.substring(value.length() - 3, value.length());
                        datas.put(SzyWaterQualityMetadataEnum.METADATA_CODE_CHLA.getName(), StringUtil.getPrettyNumber(elementValue));
                    }
                }
            }
            this.dataList.add(datas);
        }
        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) {
                String elementValue = (String) datas.get(key);
                String desc = "";
                String unit = "";
                if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_WT.getName())) {
                    //水温
                    desc = "水温";
                    unit = "℃";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_PH.getName())) {
                    //pH 值
                    desc = "PH值";
                    unit = "";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_DOX.getName())) {
                    //溶解氧
                    desc = "溶解氧";
                    unit = "mg/L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_CODMN.getName())) {
                    //高锰酸盐指数
                    desc = "高锰酸盐指数";
                    unit = "mg/L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_COND.getName())) {
                    //电导率
                    desc = "电导率";
                    unit = "µs/cm";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_ORP.getName())) {
                    //氧化还原电位
                    desc = "氧化还原电位";
                    unit = "mv";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_TURB.getName())) {
                    //浊度
                    desc = "浊度";
                    unit = "度";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_CODCR.getName())) {
                    //化学需氧量
                    desc = "化学需氧量";
                    unit = "mg/L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_CODCR_5D.getName())) {
                    //五日生化需氧量
                    desc = "五日生化需氧量";
                    unit = "mg/L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_NH3N.getName())) {
                    //氨氮
                    desc = "氨氮";
                    unit = "mg/L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_TN.getName())) {
                    //总氮
                    desc = "总氮";
                    unit = "mg/L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_CU.getName())) {
                    //铜
                    desc = "铜";
                    unit = "mg/L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_ZN.getName())) {
                    //锌
                    desc = "锌";
                    unit = "mg/L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_F.getName())) {
                    //氟化物
                    desc = "氟化物";
                    unit = "mg/L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_SE.getName())) {
                    //硒
                    desc = "硒";
                    unit = "mg/L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_ARS.getName())) {
                    //砷
                    desc = "砷";
                    unit = "mg/L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_HG.getName())) {
                    //汞
                    desc = "汞";
                    unit = "mg/L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_CD.getName())) {
                    //镉
                    desc = "镉";
                    unit = "mg/L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_CR6.getName())) {
                    //六价铬
                    desc = "六价铬";
                    unit = "mg/L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_PB.getName())) {
                    //铅
                    desc = "铅";
                    unit = "mg/L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_CYN.getName())) {
                    //氰化物
                    desc = "氰化物";
                    unit = "mg/L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_VH.getName())) {
                    //挥发酚
                    desc = "挥发酚";
                    unit = "mg/L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_C6H5OH.getName())) {
                    //苯酚
                    desc = "苯酚";
                    unit = "mg/L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_COS.getName())) {
                    //硫化物
                    desc = "硫化物";
                    unit = "mg/L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_EPEC.getName())) {
                    //粪大肠菌群
                    desc = "粪大肠菌群";
                    unit = " 个／L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_NaSO4.getName())) {
                    //硫酸盐
                    desc = "硫酸盐";
                    unit = "mg/L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_CL.getName())) {
                    //氯化物
                    desc = "氯化物";
                    unit = "mg/L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_NO3.getName())) {
                    //硝酸盐氮
                    desc = "硝酸盐氮";
                    unit = "mg/L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_FE.getName())) {
                    //铁
                    desc = "铁";
                    unit = "mg/L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_MN.getName())) {
                    //锰
                    desc = "锰";
                    unit = "mg/L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_OIL.getName())) {
                    //石油类
                    desc = "石油类";
                    unit = "mg/L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_AS.getName())) {
                    //阴离子表面活性剂
                    desc = "阴离子表面活性剂";
                    unit = "mg/L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_SIXSIXSIX.getName())) {
                    //六六六
                    desc = "六六六";
                    unit = "mg/L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_DDT.getName())) {
                    //滴滴涕
                    desc = "滴滴涕";
                    unit = "mg/L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_OCPs.getName())) {
                    //有机氯农药
                    desc = "有机氯农药";
                    unit = "mg/L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_TP.getName())) {
                    //总磷
                    desc = "总磷";
                    unit = "mg/L";
                } else if (key.equals(SzyWaterQualityMetadataEnum.METADATA_CODE_CHLA.getName())) {
                    //叶绿素
                    desc = "叶绿素";
                    unit = "mg/L";
                }
                values.add(ElementJsonPackHelp.toJson(this.feture, key, "", elementValue, unit, desc));
            }
        }
        keyStore.put(this.feture, values);
    }
}
