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.XMLizable;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * MultiStatus representing the content of a multistatus response body and
 * allows to retrieve the Xml representation.
 */
public class MultiStatus implements DavConstants, XMLizable {

    /**
     * Map collecting the responses for this multistatus, where every href must
     * only occur one single time.
     */
    private final Map<String, MultiStatusResponse> responses = new LinkedHashMap<>();

    /**
     * A general response description at the multistatus top level is used to
     * provide a general message describing the overarching nature of the response.
     * If this value is available an application may use it instead of
     * presenting the individual response descriptions contained within the
     * responses.
     */
    private String responseDescription;

    /**
     * Add a <code>MultiStatusResponse</code> element to this <code>MultiStatus</code>
     * <p>
     * This method is synchronized to avoid the problem described in
     * <a href="https://issues.apache.org/jira/browse/JCR-2755">JCR-2755</a>.
     *
     * @param response
     */
    public synchronized void addResponse(MultiStatusResponse response) {
        responses.put(response.getHref(), response);
    }

    /**
     * Returns the multistatus responses present as array.
     * <p>
     * This method is synchronized to avoid the problem described in
     * <a href="https://issues.apache.org/jira/browse/JCR-2755">JCR-2755</a>.
     *
     * @return array of all {@link MultiStatusResponse responses} present in this
     * multistatus.
     */
    public synchronized MultiStatusResponse[] getResponses() {
        return responses.values().toArray(new MultiStatusResponse[responses.size()]);
    }

    /**
     * Set the response description.
     *
     * @param responseDescription
     */
    public void setResponseDescription(String responseDescription) {
        this.responseDescription = responseDescription;
    }

    /**
     * Returns the response description.
     *
     * @return responseDescription
     */
    public String getResponseDescription() {
        return responseDescription;
    }

    /**
     * Return the Xml representation of this <code>MultiStatus</code>.
     *
     * @param document
     * @return Xml document
     */
    public Element toXml(Document document) {
        Element multistatus = DomUtil.createElement(document, XML_MULTISTATUS, NAMESPACE);
        for (MultiStatusResponse resp : getResponses()) {
            multistatus.appendChild(resp.toXml(document));
        }
        if (responseDescription != null) {
            Element respDesc = DomUtil.createElement(document, XML_RESPONSEDESCRIPTION, NAMESPACE, responseDescription);
            multistatus.appendChild(respDesc);
        }
        return multistatus;
    }

    /**
     * Build a <code>MultiStatus</code> from the specified xml element.
     *
     * @param multistatusElement element
     * @return new <code>MultiStatus</code> instance.
     * @throws IllegalArgumentException if the given document is <code>null</code>
     *                                  or does not provide the required element.
     */
    public static MultiStatus createFromXml(Element multistatusElement) {
        if (!DomUtil.matches(multistatusElement, XML_MULTISTATUS, NAMESPACE)) {
            throw new IllegalArgumentException("DAV:multistatus element expected.");
        }
        MultiStatus multistatus = new MultiStatus();
        ElementIterator it = DomUtil.getChildren(multistatusElement, XML_RESPONSE, NAMESPACE);
        while (it.hasNext()) {
            Element respElem = it.nextElement();
            MultiStatusResponse response = MultiStatusResponse.createFromXml(respElem);
            multistatus.addResponse(response);
        }
        multistatus.setResponseDescription(DomUtil.getChildText(multistatusElement, XML_RESPONSEDESCRIPTION, NAMESPACE));
        return multistatus;
    }
}
