/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 大恒泰山系统
 * 创建日期:2021/12/16
 */
package org.ts.ddcs.dataStream.processor.szy;


import lombok.extern.slf4j.Slf4j;
import org.ts.ddcs.common.dict.BaseSettingFlag;
import org.ts.ddcs.common.metadata.JsonPackMetadataEnum;
import org.ts.ddcs.enums.SzyDatagramTypeCodeEnum;
import org.ts.ddcs.common.util.BytesHelp;
import org.ts.ddcs.dataStream.DataStreamAdapter;
import org.ts.ddcs.dataStream.DataStreamCache;
import org.ts.ddcs.dataStream.DataStreamLineManager;
import org.ts.ddcs.dataStream.DataStreamProcessor;
import org.ts.ddcs.datatemplate.TestState;
import org.ts.ddcs.metadata.DataStreamCacheMetadata;

import java.util.*;

/***
 * Date:2021/12/16
 * Title:水资源规约数据处理器模块
 * Description：水资源 读取遥测站IP和端口 0xE8
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@Slf4j
public class DataStreamSzyExtReadIpDatagramProcessor extends DataStreamProcessor {

    private DataStreamCache dataStreamCache;
    private Map<String, List<Map<String,Object>>> keyStore = new HashMap<>();

    @Override
    public void init(DataStreamLineManager context) {
        this.context = context;
        this.dataStreamCache = context.getCache();
    }

    @Override
    public void process(DataStreamAdapter dataStreamAdapter) {
        this.dataStreamCache.setState(TestState.MATHCE);
        this.dataStreamCache.setMatchProcessor(this);
        String datagramCode = this.dataStreamCache.getDatagramCode();
        if (SzyDatagramTypeCodeEnum.DATAGRAM_CODE_E8.getName().equals(datagramCode)) {
            byte[] dataArea = this.dataStreamCache.getBytesValue(DataStreamCacheMetadata.METADATA_DATA_AREA.getName());
            int count = 0;
            while (count < dataArea.length) {
                byte[] flagCodeBuff = BytesHelp.subBytes(dataArea, count, 1);
                count += 1;
                String flagCode = BytesHelp.byte2HexStr(flagCodeBuff);
                if (flagCode.equals(BaseSettingFlag.CENTER_ADD.getId())) {

                } else if (flagCode.equals(BaseSettingFlag.RTU_CODE.getId())) {

                } else if (flagCode.equals(BaseSettingFlag.PASSWORD.getId())) {

                } else if (flagCode.equals(BaseSettingFlag.IP1.getId())) {
                    byte[] kind = BytesHelp.subBytes(dataArea, count, 1);
                    count += 1;
                    byte[] len = BytesHelp.subBytes(dataArea, count, 1);
                    int dataTotalLen = (int) len[0];
                    count += 1;
                    byte[] value = BytesHelp.subBytes(dataArea, count, dataTotalLen);
                    count += dataTotalLen;
                    if (kind[0] != 0x00) {
                        Map<String,Object> data = new HashMap<>();
                        data.put("ip", BytesHelp.toIp(BytesHelp.subBytes(value, 0, 6)));
                        data.put("port", String.format("%d", Integer.parseInt(BytesHelp.byte2HexStr(BytesHelp.subBytes(value, 6, 3)))));
                        List<Map<String,Object>> list = new ArrayList<>();
                        list.add(data);
                        this.keyStore.put(BaseSettingFlag.IP1.getId(),list);
                    }
                } else if (flagCode.equals(BaseSettingFlag.IP1_SUB.getId())) {
                    byte[] kind = BytesHelp.subBytes(dataArea, count, 1);
                    count += 1;
                    byte[] len = BytesHelp.subBytes(dataArea, count, 1);
                    int dataTotalLen = (int) len[0];
                    count += 1;
                    byte[] value = BytesHelp.subBytes(dataArea, count, dataTotalLen);
                    count += dataTotalLen;
                } else if (flagCode.equals(BaseSettingFlag.IP2.getId())) {
                    byte[] kind = BytesHelp.subBytes(dataArea, count, 1);
                    count += 1;
                    byte[] len = BytesHelp.subBytes(dataArea, count, 1);
                    int dataTotalLen = (int) len[0];
                    count += 1;
                    byte[] value = BytesHelp.subBytes(dataArea, count, dataTotalLen);
                    count += dataTotalLen;
                    Map<String,Object> data = new HashMap<>();
                    if (kind[0] != 0x00) {
                        data.put("ip", BytesHelp.toIp(BytesHelp.subBytes(value, 0, 6)));
                        data.put("port", String.format("%d", Integer.parseInt(BytesHelp.byte2HexStr(BytesHelp.subBytes(value, 6, 3)))));
                        List<Map<String,Object>> list = new ArrayList<>();
                        list.add(data);
                        this.keyStore.put(BaseSettingFlag.IP2.getId(),list);
                    }
                } else if (flagCode.equals(BaseSettingFlag.IP2_SUB.getId())) {
                    byte[] kind = BytesHelp.subBytes(dataArea, count, 1);
                    count += 1;
                    byte[] len = BytesHelp.subBytes(dataArea, count, 1);
                    int dataTotalLen = (int) len[0];
                    count += 1;
                    byte[] value = BytesHelp.subBytes(dataArea, count, dataTotalLen);
                    count += dataTotalLen;
                } else if (flagCode.equals(BaseSettingFlag.IP3.getId())) {
                    byte[] kind = BytesHelp.subBytes(dataArea, count, 1);
                    count += 1;
                    byte[] len = BytesHelp.subBytes(dataArea, count, 1);
                    int dataTotalLen = (int) len[0];
                    count += 1;
                    byte[] value = BytesHelp.subBytes(dataArea, count, dataTotalLen);
                    count += dataTotalLen;
                    Map<String,Object> data = new HashMap<>();
                    if (kind[0] != 0x00) {
                        data.put("ip", BytesHelp.toIp(BytesHelp.subBytes(value, 0, 6)));
                        data.put("port", String.format("%d", Integer.parseInt(BytesHelp.byte2HexStr(BytesHelp.subBytes(value, 6, 3)))));
                        List<Map<String,Object>> list = new ArrayList<>();
                        list.add(data);
                        this.keyStore.put(BaseSettingFlag.IP3.getId(),list);
                    }
                } else if (flagCode.equals(BaseSettingFlag.IP3_SUB.getId())) {
                    byte[] kind = BytesHelp.subBytes(dataArea, count, 1);
                    count += 1;
                    byte[] len = BytesHelp.subBytes(dataArea, count, 1);
                    int dataTotalLen = (int) len[0];
                    count += 1;
                    byte[] value = BytesHelp.subBytes(dataArea, count, dataTotalLen);
                    count += dataTotalLen;
                } else if (flagCode.equals(BaseSettingFlag.IP4.getId())) {
                    byte[] kind = BytesHelp.subBytes(dataArea, count, 1);
                    count += 1;
                    byte[] len = BytesHelp.subBytes(dataArea, count, 1);
                    int dataTotalLen = (int) len[0];
                    count += 1;
                    byte[] value = BytesHelp.subBytes(dataArea, count, dataTotalLen);
                    count += dataTotalLen;
                    Map<String,Object> data = new HashMap<>();
                    if (kind[0] != 0x00) {
                        data.put("ip", BytesHelp.toIp(BytesHelp.subBytes(value, 0, 6)));
                        data.put("port", String.format("%d", Integer.parseInt(BytesHelp.byte2HexStr(BytesHelp.subBytes(value, 6, 3)))));
                        List<Map<String,Object>> list = new ArrayList<>();
                        list.add(data);
                        this.keyStore.put(BaseSettingFlag.IP4.getId(), list);
                    }
                } else if (flagCode.equals(BaseSettingFlag.IP4_SUB.getId())) {
                    byte[] kind = BytesHelp.subBytes(dataArea, count, 1);
                    count += 1;
                    byte[] len = BytesHelp.subBytes(dataArea, count, 1);
                    int dataTotalLen = (int) len[0];
                    count += 1;
                    byte[] value = BytesHelp.subBytes(dataArea, count, dataTotalLen);
                    count += dataTotalLen;
                } else if (flagCode.equals(BaseSettingFlag.WORK_TYPE.getId())) {

                } else if (flagCode.equals(BaseSettingFlag.COLLECT_SETTING.getId())) {

                } else if (flagCode.equals(BaseSettingFlag.SERVER_ADD.getId())) {

                } else if (flagCode.equals(BaseSettingFlag.DEVICE_ID.getId())) {

                }
            }
            this.next(false);
        } else {
            this.next(true);
        }
    }

    @Override
    public void close(DataStreamAdapter dataStreamAdapter) {

    }

    @Override
    public void batchComplete(DataStreamAdapter dataStreamAdapter) {
        log.info("接收到水资源 E8 读取遥测站IP和端口");

    }

    @Override
    public List<Map<String, Object>> sink(DataStreamAdapter dataStreamAdapter) {
        List<Map<String, Object>> datas = new ArrayList<>();
        Map<String, Object> data = new HashMap<>();
        data.put(JsonPackMetadataEnum.PACK_METADATA_DATAGRAM.getName(), BytesHelp.byte2HexStr(this.dataStreamCache.getDatagramBuff()));
        data.put(JsonPackMetadataEnum.PACK_METADATA_FROM_TIME.getName(), this.dataStreamCache.getStringValue(DataStreamCacheMetadata.METADATA_PICK_TIME.getName()));
        data.put(JsonPackMetadataEnum.PACK_METADATA_RTU_CODE.getName(), this.dataStreamCache.getRtuCode());
        datas.add(data);
        return datas;
    }

}
