package org.ts.ddcs.DataTemplate.DtuTemplate;


import com.alibaba.fastjson.JSONObject;
import org.ts.ddcs.DataItemTemplate.DataItemTemplateInterface;
import org.ts.ddcs.DataTemplate.DataTemplateInterface;
import org.ts.ddcs.DataTemplate.TemplateSet;
import org.ts.ddcs.log.LogHelper;
import org.ts.ddcs.Tool.BytesHelp;
import org.ts.ddcs.Worker.DataProcess;
import org.ts.ddcs.utils.GBConstants;

import java.util.*;

/**
 * created by swp on 2017/8/23
 */
@SuppressWarnings("all")
public abstract class DtuTemplate 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 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_DTU;
    }

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

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

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

    @Override
    public String SupplementInfo() {
        return 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 getDataInfo() {
        JSONObject data = new JSONObject();
        data.put("func", this.fetureKey);
        List<JSONObject> items = new LinkedList<>();
        data.put("data", items);
        return data.toJSONString();
    }

    @Override
    public byte[] GetEndFlag() {
        return this.dataAreaEnd;
    }

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

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

    @Override
    public String GetViewName() {
        return "DTU透传";
    }

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

    @Override
    public boolean Macth(byte[] data) {
        return this.macth;
    }

    @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 boolean Macth() {
        return this.macth;
    }

    @Override
    public void Stream() {
    }

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

//    @Override
//    public String  RtuCodeString() {
//        return null;
//    }

    @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 HashMap<String, DataItemTemplateInterface> DataItem() {
        return this.dataItem;
    }

    @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;
    }
}
