package org.xbib.io.webdav.common;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xbib.io.webdav.api.DavConstants;
import org.xbib.io.webdav.api.Namespace;
import org.xbib.io.webdav.api.Subscription;
import org.xbib.io.webdav.api.XMLizable;
import java.util.ArrayList;
import java.util.List;

/**
 * <code>SubscriptionDiscovery</code> encapsulates the 'subscriptiondiscovery'
 * property of a webdav resource.
 */
public class SubscriptionDiscovery extends AbstractDavProperty<Subscription[]> {

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

    private static final DavPropertyName SUBSCRIPTIONDISCOVERY = DavPropertyName.create("subscriptiondiscovery", DCR_NAMESPACE);

    private final Subscription[] subscriptions;

    /**
     * Create a new <code>SubscriptionDiscovery</code> that lists the given
     * subscriptions.
     *
     * @param subscriptions
     */
    public SubscriptionDiscovery(Subscription[] subscriptions) {
        super(SUBSCRIPTIONDISCOVERY, true);
        if (subscriptions != null) {
            this.subscriptions = subscriptions;
        } else {
            this.subscriptions = new Subscription[0];
        }
    }

    /**
     * Create a new <code>SubscriptionDiscovery</code> that contains a single
     * subscription entry.
     *
     * @param subscription
     */
    public SubscriptionDiscovery(Subscription subscription) {
        super(SUBSCRIPTIONDISCOVERY, true);
        if (subscription != null) {
            this.subscriptions = new Subscription[]{subscription};
        } else {
            this.subscriptions = new Subscription[0];
        }
    }

    /**
     * Returns an array of {@link Subscription}s.
     *
     * @return an array of {@link Subscription}s
     * @see DavProperty#getValue()
     */
    public Subscription[] getValue() {
        return subscriptions;
    }

    /**
     * Returns the Xml representation of the subscription discovery.
     *
     * @param document
     * @return Xml representation
     * @see XMLizable#toXml(Document)
     */
    @Override
    public Element toXml(Document document) {
        Element elem = getName().toXml(document);
        for (Subscription subscription : subscriptions) {
            elem.appendChild(subscription.toXml(document));
        }
        return elem;
    }

    public static SubscriptionDiscovery createFromXml(Element sDiscoveryElement) {
        if (!DomUtil.matches(sDiscoveryElement, SUBSCRIPTIONDISCOVERY.getName(), SUBSCRIPTIONDISCOVERY.getNamespace())) {
            throw new IllegalArgumentException("'subscriptiondiscovery' element expected.");
        }

        List<Subscription> subscriptions = new ArrayList<Subscription>();
        ElementIterator it = DomUtil.getChildren(sDiscoveryElement, "subscription", DCR_NAMESPACE);
        while (it.hasNext()) {
            final Element sb = it.nextElement();
            // anonymous inner class: Subscription interface
            Subscription s = new Subscription() {
                /**
                 * @see Subscription#getSubscriptionId()
                 */
                public String getSubscriptionId() {
                    Element ltEl = DomUtil.getChildElement(sb, "subscriptionid", DCR_NAMESPACE);
                    if (ltEl != null) {
                        return DomUtil.getChildText(sb, DavConstants.XML_HREF, DavConstants.NAMESPACE);
                    }
                    return null;
                }

                public boolean eventsProvideNodeTypeInformation() {
                    String t = DomUtil.getChildText(sb, "eventswithnodetypes", DCR_NAMESPACE);
                    return Boolean.parseBoolean(t);
                }

                public boolean eventsProvideNoLocalFlag() {
                    String t = DomUtil.getChildText(sb,  "eventswithlocalflag", DCR_NAMESPACE);
                    return Boolean.parseBoolean(t);
                }

                public Element toXml(Document document) {
                    return (Element) document.importNode(sb, true);
                }
            };
            subscriptions.add(s);
        }

        return new SubscriptionDiscovery(subscriptions.toArray(new Subscription[subscriptions.size()]));
    }
}