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.*;

@SuppressWarnings("all")
public abstract class DataTemplateSzy2012 implements DataTemplateInterface {
    protected String templateName;
    protected String fetureKey;
    protected boolean macth = false;
    protected byte[] startFlag;
    protected byte[] centerStation;
    protected byte[] rtuCode;
    protected byte[] rtuPassword;
    protected byte[] funcCode;
    protected int dataLen;
    protected byte[] dataArea;
    protected byte[] dataAreaStart;
    protected byte[] dataAreaEnd;
    protected byte[] crc;
    protected byte[] controlArea;
    protected String supplementInfo;
    protected boolean needCheckOutlierData = false;

    protected HashMap<String, DataItemTemplateInterface> dataItem;

    @Override
    public void Init(TemplateSet template) {
        this.fetureKey = template.feature;
        this.templateName = template.dataTemplate;
    }

    @Override
    public String GetAgreement() {
        return GBConstants.AGREEMENT_KIND_SZY;
    }

    @Override
    public boolean ClearCommand() {
        return true;
    }

    @Override
    public String GetDatagram() {
        return null;
    }

    @Override
    public void Stream() {

    }

    @Override
    public void SupplementInfo(String supplementInfo) {
        this.supplementInfo = supplementInfo;
    }


    @Override
    public boolean NeedCheckOutlierData() {
        return this.needCheckOutlierData;
    }

    @Override
    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()) {
                    outlierDataList.add(itemTemplateInterface);
                }
            }
            return outlierDataList;
        }
        return null;
    }

    @Override
    public String SupplementInfo() {
        return supplementInfo;
    }

    @Override
    public byte[] GetEndFlag() {
        return new byte[0];
    }

    @Override
    public String getDataInfo() {
        return null;
    }

    @Override
    public boolean Macth() {
        return this.macth;
    }

    @Override
    public boolean Macth(byte[] data) {
        String sf = BytesHelp.byte2HexStr(BytesHelp.subBytes(data, 0, 1));
        if (!sf.equals("68")) {
            return macth;
        }

        byte[] fc = BytesHelp.subBytes(data, 9, 1);
        if (this.fetureKey.equals(BytesHelp.byte2HexStr(fc))) {
            int count = 0;
            startFlag = BytesHelp.subBytes(data, count, 1);
            count += 1;
            dataLen = data[count] < 0 ? data[count] + 256 : data[count];
            count += 1;
            byte sfs = data[count];
            count += 1;
            if (dataLen > 0) {
                int dataCount = 0;
                controlArea = BytesHelp.subBytes(data, count + dataCount, 1);
                dataCount += 1;
                rtuCode = BytesHelp.subBytes(data, count + dataCount, 5);
                dataCount += 5;
                funcCode = BytesHelp.subBytes(data, count + dataCount, 1);
                dataCount += 1;
                if (dataLen - dataCount > 0) {
                    dataArea = BytesHelp.subBytes(data, count + dataCount, dataLen - dataCount);
                }

            }
            count += dataLen;
            crc = BytesHelp.subBytes(data, count, 1);
            count += 1;
            dataAreaEnd = BytesHelp.subBytes(data, count, 1);
            count += 1;
            if (data.length == count) {
                this.macth = true;
            } else {
                LogHelper.info("*************************   macth error *********************");
            }
        }
        return this.macth;
    }

    @Override
    public String GetFeture() {
        return this.fetureKey;
    }

    @Override
    public String GetName() {
        return this.templateName;
    }

    @Override
    public String GetViewName() {
        return "水资源报文";
    }

    @Override
    public byte[] RtuCode() {
        return this.rtuCode;
    }

    @Override
    public byte[] CenterStation() {
        return this.centerStation;
    }

    @Override
    public byte[] RtuPassword() {
        return this.rtuPassword;
    }

    @Override
    public byte[] FuncCode() {
        return this.funcCode;
    }

    @Override
    public byte[] SerialNo() {
        return new byte[0];
    }

    @Override
    public byte[] SendPacketTime() {
        return new byte[0];
    }

    @Override
    public byte[] ReceiveTime() {
        return new byte[0];
    }

    @Override
    public byte[] SiteKind() {
        return new byte[0];
    }

    @Override
    public byte[] CollectTime() {
        return new byte[0];
    }

    @Override
    public byte[] GetContent() {
        return this.dataArea;
    }

    @Override
    public HashMap<String, DataItemTemplateInterface> DataItem() {
        return this.dataItem;
    }

    @Override
    public LinkedList<DataItemTemplateInterface> MacthDataItem() {
        LinkedList<DataItemTemplateInterface> macthItem = new LinkedList<>();
        if (dataItem != null && dataItem.size() > 0) {
            Set<String> keys = dataItem.keySet();
            for (String key : keys) {
                DataItemTemplateInterface item = dataItem.get(key);
                macthItem.addLast(item);
            }
        }
        return macthItem;
    }

    @Override
    public LinkedList<DataItemTemplateInterface> MacthDataItem(String tableName) {
        LinkedList<DataItemTemplateInterface> macthItem = new LinkedList<>();
        if (dataItem != null && dataItem.size() > 0) {
            Set<String> keys = dataItem.keySet();
            for (String key : keys) {
                DataItemTemplateInterface item = dataItem.get(key);
                if (item.GetTableName().equals(tableName)) {
                    macthItem.addLast(item);
                }
            }
        }
        return macthItem;
    }

    @Override
    public LinkedList<DataItemTemplateInterface> matchDataItem(String tableName, String column) {
        return null;
    }

    @Override
    public LinkedList<DataItemTemplateInterface> matchDataItemByFeature(String feature) {
        return null;
    }

    @Override
    public List<Map<String, String>> toTextInfo() {
        List<Map<String, String>> textInfo = new LinkedList<>();
        if (dataItem != null && dataItem.size() > 0) {
            Set<String> keys = dataItem.keySet();
            for (String key : keys) {
                DataItemTemplateInterface item = dataItem.get(key);
                Map<String, String> text = new HashMap<>();
                text.put("name", item.GetDesc());
                text.put("value", item.toString());
                textInfo.add(text);
            }
        }
        return textInfo;
    }
}
