package eu.eudat.models.data.components.commons.datafield;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import java.util.HashMap;
import java.util.Map;

public class AutoCompleteData extends ComboBoxData<AutoCompleteData> {
    private String url;
    private Option autoCompleteOptions;
    private String optionsRoot;
    private Boolean multiAutoComplete;

    public String getOptionsRoot() {
        return optionsRoot;
    }
    public void setOptionsRoot(String optionsRoot) {
        this.optionsRoot = optionsRoot;
    }

    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }

    public Option getAutoCompleteOptions() {
        return autoCompleteOptions;
    }
    public void setAutoCompleteOptions(Option autoCompleteOptions) {
        this.autoCompleteOptions = autoCompleteOptions;
    }

    public Boolean getMultiAutoComplete() { return multiAutoComplete; }
    public void setMultiAutoComplete(Boolean multiAutoComplete) { this.multiAutoComplete = multiAutoComplete; }

    @Override
    public Element toXml(Document doc) {
        Element root = super.toXml(doc);

        root.setAttribute("url", this.url);
        root.setAttribute("optionsRoot", this.optionsRoot);
        if (this.multiAutoComplete != null)
            root.setAttribute("multiAutoComplete", this.multiAutoComplete.toString());
        Element element = doc.createElement("option");
        element.setAttribute("label", this.autoCompleteOptions.getLabel());
        element.setAttribute("value", autoCompleteOptions.getValue());
        element.setAttribute("source", autoCompleteOptions.getSource());
        root.appendChild(element);
        return root;
    }

    @Override
    public AutoCompleteData fromXml(Element item) {
        super.fromXml(item);
        this.url = item.getAttribute("url");
        this.optionsRoot = item.getAttribute("optionsRoot");
        this.multiAutoComplete = Boolean.parseBoolean(item.getAttribute("multiAutoComplete"));
        Element optionElement = (Element) item.getElementsByTagName("option").item(0);
        if (optionElement != null) {
            this.autoCompleteOptions = new Option();
            this.autoCompleteOptions.setLabel(optionElement.getAttribute("label"));
            this.autoCompleteOptions.setValue(optionElement.getAttribute("value"));
            this.autoCompleteOptions.setSource(optionElement.getAttribute("source"));
        }
        return this;
    }

    @Override
    public AutoCompleteData fromData(Object data) {
        super.fromData(data);
        this.autoCompleteOptions = new Option();
        if (data != null) {
            this.url = (String) ((Map<String, Object>) data).get("url");
            this.optionsRoot = (String) ((Map<String, Object>) data).get("optionsRoot");
            this.multiAutoComplete = (Boolean) ((Map<Boolean, Object>) data).get("multiAutoComplete");
            Map<String, String> options = ((Map<String, Map<String, String>>) data).get("autoCompleteOptions");
            if (options != null) {
                this.autoCompleteOptions.setLabel(options.get("label"));
                this.autoCompleteOptions.setValue(options.get("value"));
                this.autoCompleteOptions.setSource(options.get("source"));
            }
        }

        return this;
    }

    @Override
    public Object toData() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Map<String, Object> toMap(Element item) {
        HashMap dataMap = new HashMap();
        dataMap.put("label", item != null ? item.getAttribute("label") : "");
        dataMap.put("url", item != null ? item.getAttribute("url") : "");
        dataMap.put("type", item != null ? item.getAttribute("type") : "autocomplete");
        dataMap.put("optionsRoot", item != null ? item.getAttribute("optionsRoot") : "");
        Element optionElement = (Element) item.getElementsByTagName("option").item(0);
//        if (optionElement != null) {
//            this.autoCompleteOptions = new Option();
//            this.autoCompleteOptions.setLabel(optionElement.getAttribute("label"));
//            this.autoCompleteOptions.setValue(optionElement.getAttribute("value"));
//        }
        dataMap.put("autoCompleteOptions", item != null ? optionToMap(optionElement) : null);
        return dataMap;
    }

    private Map<String, Object> optionToMap(Element item){
        HashMap dataMap = new HashMap();
        dataMap.put("label", item != null ? item.getAttribute("label") : "");
        dataMap.put("value", item != null ? item.getAttribute("value") : "");
        dataMap.put("source", item != null ? item.getAttribute("source") : "");
        return dataMap;
    }
}
