package org.xbib.io.webdav.client;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xbib.io.webdav.common.DavProperty;
import org.xbib.io.webdav.api.Namespace;
import org.xbib.io.webdav.api.XMLizable;
import org.xbib.io.webdav.common.AbstractDavProperty;
import org.xbib.io.webdav.common.DomUtil;
import java.util.HashSet;
import java.util.Set;

/**
 * <code>QueryGrammerSet</code> is a {@link DavProperty} that
 * encapsulates the 'supported-query-grammer-set' as defined by the
 * Webdav SEARCH internet draft.
 */
public class QueryGrammerSet extends AbstractDavProperty<Set<? extends XMLizable>> implements SearchConstants {

    private final Set<Grammer> queryGrammers = new HashSet<>();

    /**
     * Create a new empty <code>QueryGrammerSet</code>. Supported query grammers
     * may be added by calling {@link #addQueryLanguage(String, Namespace)}.
     */
    public QueryGrammerSet() {
        super(QUERY_GRAMMER_SET, true);
    }

    /**
     * Add another query queryGrammer to this set.
     *
     * @param grammerName
     * @param namespace
     */
    public void addQueryLanguage(String grammerName, Namespace namespace) {
        queryGrammers.add(new Grammer(grammerName, namespace));
    }

    /**
     * Return a String array containing the URIs of the query
     * languages supported.
     *
     * @return names of the supported query languages
     */
    public String[] getQueryLanguages() {
        int size = queryGrammers.size();
        if (size > 0) {
            String[] qLangStr = new String[size];
            Grammer[] grammers = queryGrammers.toArray(new Grammer[size]);
            for (int i = 0; i < grammers.length; i++) {
                qLangStr[i] = grammers[i].namespace.getURI() + grammers[i].localName;
            }
            return qLangStr;
        } else {
            return new String[0];
        }
    }

    /**
     * Return the Xml representation of this property according to the definition
     * of the 'supported-query-grammer-set'.
     *
     * @param document
     * @return Xml representation
     * @see SearchConstants#QUERY_GRAMMER_SET
     * @see XMLizable#toXml(Document)
     */
    @Override
    public Element toXml(Document document) {
        Element elem = getName().toXml(document);
        for (Grammer qGrammer : queryGrammers) {
            elem.appendChild(qGrammer.toXml(document));
        }
        return elem;
    }

    /**
     * Returns the set of supported query grammers.
     *
     * @return list of supported query languages.
     * @see DavProperty#getValue()
     */
    public Set<? extends XMLizable> getValue() {
        return queryGrammers;
    }

    private static class Grammer implements XMLizable {

        private final String localName;
        private final Namespace namespace;
        private final int hashCode;

        Grammer(String localName, Namespace namespace) {
            this.localName = localName;
            this.namespace = namespace;
            hashCode = DomUtil.getExpandedName(localName, namespace).hashCode();
        }

        @Override
        public int hashCode() {
            return hashCode;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == this) {
                return true;
            }
            if (obj instanceof Grammer) {
                return obj.hashCode() == hashCode();
            }
            return false;
        }

        /**
         * @see XMLizable#toXml(Document)
         */
        public Element toXml(Document document) {
            Element sqg = DomUtil.createElement(document, XML_QUERY_GRAMMAR, SearchConstants.NAMESPACE);
            Element grammer = DomUtil.addChildElement(sqg, XML_GRAMMER, SearchConstants.NAMESPACE);
            DomUtil.addChildElement(grammer, localName, namespace);
            return sqg;
        }
    }
}