package org.ts.ddcs.DataTemplate;

import org.ts.ddcs.DataItemTemplate.DataItemTemplateInterface;
import org.ts.ddcs.Log.LogHelper;
import org.ts.ddcs.Tool.BytesHelp;
import org.ts.ddcs.utils.GBConstants;

import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * 0x30
 * 测试报
 */
public class DataTemplateF30 extends DataTemplate {
    private byte[] serialNo;
    private byte[] sendPacketTime;
    private byte[] addrFlag;
    private byte[] dataAreaRtuCode;
    private byte[] siteKind;
    private byte[] collectTimeFlag;
    private byte[] collectTime;


    @Override
    public byte[] SerialNo() {
        return this.serialNo;
    }

    @Override
    public byte[] SendPacketTime() {
        return this.sendPacketTime;
    }

    @Override
    public byte[] SiteKind() {
        return this.siteKind;
    }

    @Override
    public byte[] CollectTime() {
        return this.collectTime;
    }

    @Override
    public String GetViewName() {
        return "测试报";
    }


    @Override
    @SuppressWarnings("all")
    public void Stream() {
        try {
            if (dataLen > 0 && dataLen == dataArea.length) {
                int count = 0;
                serialNo = BytesHelp.subBytes(dataArea, count, 2); //流水号
                count += 2;
                sendPacketTime = BytesHelp.subBytes(dataArea, count, 6); // 发报时间
                count += 6;
                addrFlag = BytesHelp.subBytes(dataArea, count, 2); //地址标识符
                count += 2;
                dataAreaRtuCode = BytesHelp.subBytes(dataArea, count, 5); //遥测站地址
                count += 5;
                siteKind = BytesHelp.subBytes(dataArea, count, 1);//遥测站分类码
                count += 1;
                collectTimeFlag = BytesHelp.subBytes(dataArea, count, 2); //采集时间
                count += 2;
                collectTime = BytesHelp.subBytes(dataArea, count, 5); //采集时间
                count += 5;

                if (dataArea.length - count > 0) {
                    byte[] contentView = BytesHelp.subBytes(dataArea, count, dataArea.length - count);
                    if (null != contentView && contentView.length > 0) {
                        DataItemTemplateLine line = DataTemplateManager.newInstance().createItemTemplateLine(GBConstants.AGREEMENT_KIND_SW);
                        line.stream(contentView);
                        dataItem = line.MacthTemplate();
                    }
                }
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }

    @Override
    @SuppressWarnings("all")
    public List<DataItemTemplateInterface> OutlierData(){
        if (null != this.dataItem){
            List<DataItemTemplateInterface> outlierDataList=new LinkedList<>();
           Set<String> keys = this.dataItem.keySet();
           for (String key : keys){
               DataItemTemplateInterface itemTemplateInterface =  (DataItemTemplateInterface)this.dataItem.get(key);
               if (itemTemplateInterface.OutlierData()){
                   byte[] valueBuff = itemTemplateInterface.GetByteArray();
                   String hexValue = BytesHelp.byte2HexStr(valueBuff);
                   LogHelper.info("data outlier " +hexValue + " " + itemTemplateInterface.GetColumnName()+" "+itemTemplateInterface.GetName()+" "+itemTemplateInterface.GetDesc());
                   outlierDataList.add(itemTemplateInterface);
               }
           }
           return outlierDataList;
        }
        return null;
    }
}
