package org.xbib.io.webdav.common;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xbib.io.webdav.api.DavConstants;
import org.xbib.io.webdav.api.XMLizable;
import java.util.Collection;

/**
 * <code>AbstractDavProperty</code> provides generic METHODS used by various
 * implementations of the {@link DavProperty} interface.
 */
public abstract class AbstractDavProperty<T> implements DavProperty<T> {

    private final DavPropertyName name;
    private final boolean isInvisibleInAllprop;

    /**
     * Create a new <code>AbstractDavProperty</code> with the given {@link DavPropertyName}
     * and a boolean flag indicating whether this property should be suppressed
     * in PROPFIND/allprop responses.
     */
    public AbstractDavProperty(DavPropertyName name, boolean isInvisibleInAllprop) {
        this.name = name;
        this.isInvisibleInAllprop = isInvisibleInAllprop;
    }

    /**
     * Computes the hash code using this property's name and value.
     *
     * @return the hash code
     */
    @Override
    public int hashCode() {
        int hashCode = getName().hashCode();
        if (getValue() != null) {
            hashCode += getValue().hashCode();
        }
        return hashCode % Integer.MAX_VALUE;
    }

    /**
     * Checks if this property has the same {@link DavPropertyName name}
     * and value as the given one.
     *
     * @param obj the object to compare to
     * @return <code>true</code> if the 2 objects are equal;
     * <code>false</code> otherwise
     */
    @Override
    public boolean equals(Object obj) {
        if (obj instanceof DavProperty) {
            DavProperty<?> prop = (DavProperty<?>) obj;
            boolean equalName = getName().equals(prop.getName());
            boolean equalValue = (getValue() == null) ? prop.getValue() == null : getValue().equals(prop.getValue());
            return equalName && equalValue;
        }
        return false;
    }


    /**
     * Return a XML element representation of this property. The value of the
     * property will be added as text or as child element.
     * <pre>
     * new DavProperty("displayname", "WebDAV Directory").toXml
     * gives a element like:
     * &lt;D:displayname&gt;WebDAV Directory&lt;/D:displayname&gt;
     *
     * new DavProperty("resourcetype", new Element("collection")).toXml
     * gives a element like:
     * &lt;D:resourcetype&gt;&lt;D:collection/&gt;&lt;/D:resourcetype&gt;
     *
     * Element[] customVals = { new Element("bla", customNamespace), new Element("bli", customNamespace) };
     * new DavProperty("custom-property", customVals, customNamespace).toXml
     * gives an element like
     * &lt;Z:custom-property&gt;
     *    &lt;Z:bla/&gt;
     *    &lt;Z:bli/&gt;
     * &lt;/Z:custom-property&gt;
     * </pre>
     *
     * @param document
     * @return a XML element of this property
     * @see XMLizable#toXml(Document)
     */
    public Element toXml(Document document) {
        Element elem = getName().toXml(document);
        T value = getValue();
        // todo: improve....
        if (value != null) {
            if (value instanceof XMLizable) {
                elem.appendChild(((XMLizable) value).toXml(document));
            } else if (value instanceof Node) {
                Node n = document.importNode((Node) value, true);
                elem.appendChild(n);
            } else if (value instanceof Node[]) {
                for (int i = 0; i < ((Node[]) value).length; i++) {
                    Node n = document.importNode(((Node[]) value)[i], true);
                    elem.appendChild(n);
                }
            } else if (value instanceof Collection) {
                for (Object entry : ((Collection<?>) value)) {
                    if (entry instanceof XMLizable) {
                        elem.appendChild(((XMLizable) entry).toXml(document));
                    } else if (entry instanceof Node) {
                        Node n = document.importNode((Node) entry, true);
                        elem.appendChild(n);
                    } else {
                        DomUtil.setText(elem, entry.toString());
                    }
                }
            } else {
                DomUtil.setText(elem, value.toString());
            }
        }
        return elem;
    }

    /**
     * Returns the name of this property.
     *
     * @return name
     * @see DavProperty#getName()
     */
    public DavPropertyName getName() {
        return name;
    }

    /**
     * Return <code>true</code> if this property should be suppressed
     * in a PROPFIND/{@link DavConstants#PROPFIND_ALL_PROP DAV:allprop}
     * response. See RFC 4918, Section 9.1.
     *
     * @see DavProperty#isInvisibleInAllprop()
     */
    public boolean isInvisibleInAllprop() {
        return isInvisibleInAllprop;
    }
}
