package org.xbib.io.webdav.common;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xbib.io.webdav.api.EventType;
import org.xbib.io.webdav.api.Namespace;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <code>DefaultEventType</code> defines a simple EventType implementation that
 * only consists of a qualified event name consisting of namespace plus local
 * name.
 */
public class DefaultEventType implements EventType {

    private static final Namespace DCR_NAMESPACE = Namespace.getNamespace("dcr", "http://www.day.com/jcr/webdav/1.0");

    private static final Map<String, EventType> eventTypes = new HashMap<>();

    private final String localName;

    private final Namespace namespace;

    /**
     * Avoid instantiation of <code>DefaultEventType</code>. Since the amount
     * of available (and registered) events is considered to be limited, the
     * static {@link #create(String, Namespace) method is defined.
     *
     * @param localName
     * @param namespace
     */
    private DefaultEventType(String localName, Namespace namespace) {
        this.localName = localName;
        this.namespace = namespace;
    }

    /**
     * Factory method to create a new <code>EventType</code>.
     *
     * @param localName
     * @param namespace
     * @return
     */
    public static EventType create(String localName, Namespace namespace) {
        if (localName == null || "".equals(localName)) {
            throw new IllegalArgumentException("null and '' are not valid local names of an event type.");
        }
        String key = DomUtil.getExpandedName(localName, namespace);
        if (eventTypes.containsKey(key)) {
            return eventTypes.get(key);
        } else {
            EventType type = new DefaultEventType(localName, namespace);
            eventTypes.put(key, type);
            return type;
        }
    }

    /**
     * Factory method to create an array of new <code>EventType</code> for the
     * specified localNames and the specified namespace.
     *
     * @param localNames
     * @param namespace
     * @return An array of event types.
     */
    public static EventType[] create(String[] localNames, Namespace namespace) {
        EventType[] types = new EventType[localNames.length];
        for (int i = 0; i < localNames.length; i++) {
            types[i] = create(localNames[i], namespace);
        }
        return types;
    }

    /**
     * Retrieves one or multiple <code>EventType</code>s from the 'eventtype'
     * Xml element. While a subscription may register multiple types (thus
     * the 'eventtype' contains multiple child elements), a single event may only
     * refer to one single type.
     *
     * @param eventType
     * @return
     */
    public static EventType[] createFromXml(Element eventType) {
        if (!DomUtil.matches(eventType, "eventtype", DCR_NAMESPACE)) {
            throw new IllegalArgumentException("'eventtype' element expected which contains a least a single child element.");
        }
        List<EventType> etypes = new ArrayList<>();
        ElementIterator it = DomUtil.getChildren(eventType);
        while (it.hasNext()) {
            Element el = it.nextElement();
            etypes.add(create(el.getLocalName(), DomUtil.getNamespace(el)));
        }
        return etypes.toArray(new EventType[etypes.size()]);
    }

    @Override
    public String getName() {
        return localName;
    }

    @Override
    public Namespace getNamespace() {
        return namespace;
    }

    @Override
    public Element toXml(Document document) {
        return DomUtil.createElement(document, localName, namespace);
    }
}
