package org.xbib.io.webdav.common;

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

/**
 * <code>HrefProperty</code> is an extension to the common {@link DavProperty}.
 * The String representation of the property value is always displayed as text
 * inside an extra 'href' element. If the value is a String array each array
 * element is added as text to a separate 'href' element.
 *
 * @see DavConstants#XML_HREF
 * @see DavProperty#getValue()
 */
public class HrefProperty extends AbstractDavProperty<String[]> {

    private final String[] value;

    /**
     * Creates a new WebDAV property with the given <code>DavPropertyName</code>
     *
     * @param name                 the name of the property
     * @param value                the value of the property
     * @param isInvisibleInAllprop A value of true, defines this property to be invisible in PROPFIND/allprop
     *                             It will not be returned in a {@link DavConstants#PROPFIND_ALL_PROP DAV:allprop}
     *                             PROPFIND request.
     */
    public HrefProperty(DavPropertyName name, String value, boolean isInvisibleInAllprop) {
        super(name, isInvisibleInAllprop);
        this.value = new String[]{value};
    }

    /**
     * Creates a new WebDAV property with the given <code>DavPropertyName</code>
     *
     * @param name                 the name of the property
     * @param value                the value of the property
     * @param isInvisibleInAllprop A value of true, defines this property to be invisible in PROPFIND/allprop
     *                             It will not be returned in a {@link DavConstants#PROPFIND_ALL_PROP DAV:allprop}
     *                             PROPFIND request.
     */
    public HrefProperty(DavPropertyName name, String[] value, boolean isInvisibleInAllprop) {
        super(name, isInvisibleInAllprop);
        this.value = value;
    }

    /**
     * Create a new <code>HrefProperty</code> from the specified property.
     * Please note, that the property must have a <code>List</code> value
     * object, consisting of {@link #XML_HREF href} <code>Element</code> entries.
     *
     * @param prop
     */
    public HrefProperty(DavProperty<?> prop) {
        super(prop.getName(), prop.isInvisibleInAllprop());
        if (prop instanceof HrefProperty) {
            // already an HrefProperty: no parsing required
            this.value = ((HrefProperty) prop).value;
        } else {
            // assume property has be built from xml
            ArrayList<String> hrefList = new ArrayList<String>();
            Object val = prop.getValue();
            if (val instanceof List) {
                for (Object entry : ((List<?>) val)) {
                    if (entry instanceof Element && XML_HREF.equals(((Element) entry).getLocalName())) {
                        String href = DomUtil.getText((Element) entry);
                        if (href != null) {
                            hrefList.add(href);
                        }
                    }
                }
            } else if (val instanceof Element && XML_HREF.equals(((Element) val).getLocalName())) {
                String href = DomUtil.getTextTrim((Element) val);
                if (href != null) {
                    hrefList.add(href);
                }
            }
            value = hrefList.toArray(new String[hrefList.size()]);
        }
    }

    /**
     * Returns an Xml element with the following form:
     * <pre>
     * &lt;Z:name&gt;
     *    &lt;DAV:href&gt;value&lt;/DAV:href/&gt;
     * &lt;/Z:name&gt;
     * </pre>
     * where Z: represents the prefix of the namespace defined with the initial
     * webdav property name.
     *
     * @param document
     * @return Xml representation
     * @see DomUtil#hrefToXml(String, Document)
     */
    @Override
    public Element toXml(Document document) {
        Element elem = getName().toXml(document);
        String[] value = getValue();
        if (value != null) {
            for (String href : value) {
                elem.appendChild(DomUtil.hrefToXml(href, document));
            }
        }
        return elem;
    }

    /**
     * Returns an array of String.
     *
     * @return an array of String.
     * @see DavProperty#getValue()
     */
    public String[] getValue() {
        return value;
    }

    /**
     * Return an list of String containing the text of those DAV:href elements
     *
     * @return list of href String
     */
    public List<String> getHrefs() {
        return value != null ? Arrays.asList(value) : new ArrayList<>();
    }
}
