package org.ts.ddcs.DataTemplate;


import org.ts.ddcs.log.LogHelper;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.*;

/**
 * created by swp on 2017/8/29
 */
@Component
@SuppressWarnings("all")
public class DataTemplateManager {

    private static class DataTemplateManagerHolder {
        private static DataTemplateManager instance = new DataTemplateManager();
    }

    public static DataTemplateManager newInstance() {
        return DataTemplateManager.DataTemplateManagerHolder.instance;
    }

    public ArrayList<ItemTemplateSet> itemTemplateSet = new ArrayList<>();
    public ArrayList<TemplateSet> templateSet = new ArrayList<>();

    private String templatexml = "";
    private String itemTemplatexml = "";

    public DataTemplateManager setTemplateConfig(String path) {
        this.templatexml = path;
        return this;
    }

    public DataTemplateManager setItemTemplateConfig(String path) {
        this.itemTemplatexml = path;
        return this;
    }

    public DataTemplateManager loadTemplateConfig() {
        try {
            String templatexml = this.templatexml;
            SAXReader reader = new SAXReader();
            Document document = reader.read(new File(templatexml));
            Element root = document.getRootElement();
            Element Templates = root.element("Templates");
            List items = Templates.elements("Item");
            for (Object o : items
            ) {
                TemplateSet t = new TemplateSet();
                Element e = (Element) o;
                String feture = e.element("Feature").getTextTrim();
                t.setFeature(feture);
                String dataTemplate = e.element("DataTemplate").getTextTrim();
                t.setDataTemplate(dataTemplate);
                String enable = e.element("Enable").getTextTrim();
                t.setEnable(enable);
                String agreement = e.element("Agreement").getTextTrim();
                t.setAgreement(agreement);
                String dataProcessor = e.element("DataProcessor").getTextTrim();
                t.setDataSaveProcess(dataProcessor);
                String relayProcessor = e.element("DataRelayProcessor").getTextTrim();
                t.setDataRelayProcess(relayProcessor);
                this.templateSet.add(t);
            }

        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return this;
    }

    public DataTemplateManager loadItemTemplateConfig() {
        try {
            String templatexml = this.itemTemplatexml;
            SAXReader reader = new SAXReader();
            Document document = reader.read(new File(templatexml));
            Element root = document.getRootElement();
            Element Templates = root.element("Templates");
            List items = Templates.elements("Item");
            for (Object o : items
            ) {
                ItemTemplateSet t = new ItemTemplateSet();
                Element e = (Element) o;
                String feture = e.element("Feture").getTextTrim();
                t.setFeature(feture);
                String dataTemplate = e.element("DataTemplate").getTextTrim();
                t.setDataTemplate(dataTemplate);
                String column = e.element("Column").getTextTrim();
                t.setColumn(column);
                String enable = e.element("Enable").getTextTrim();
                t.setEnable(enable);
                String desc = e.element("Desc").getTextTrim();
                t.setDesc(desc);
                String table = e.element("Table").getTextTrim();
                t.setTable(table);
                String agreement = e.element("Agreement").getTextTrim();
                t.setAgreement(agreement);
                this.itemTemplateSet.add(t);
            }

            LogHelper.info("load item template success!");

        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return this;
    }


    public synchronized DataTemplateLine createTemplateLine() throws IllegalAccessException, ClassNotFoundException, InstantiationException {
        DataTemplateLine line = new DataTemplateLine();
        for (TemplateSet set : this.templateSet) {
            if (set.getEnable().equals("1")) {
                line.cloneTemplate(set);
            }
        }
        return line;
    }

    public synchronized DataTemplateLine createTemplateLine(String agreement) throws IllegalAccessException, ClassNotFoundException, InstantiationException {
        DataTemplateLine line = new DataTemplateLine();
        for (TemplateSet set : this.templateSet) {
            if (set.getAgreement().equals("*") || set.getAgreement().equals(agreement)) {
                line.cloneTemplate(set);
            }
        }
        return line;
    }

    public DataItemTemplateLine createItemTemplateLine() throws IllegalAccessException, ClassNotFoundException, InstantiationException {
        DataItemTemplateLine line = new DataItemTemplateLine();
        for (ItemTemplateSet set : this.itemTemplateSet) {
            if (set.getEnable().equals("1")) {
                line.cloneItemTemplate(set);
            }
        }
        return line;
    }
}
