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

/**
 * The <code>Type</code> class encapsulates the lock type as defined by RFC 2518.
 */
public class Type implements XMLizable {

    private static final Map<String, Type> types = new HashMap<>();

    public static final Type WRITE = Type.create(DavConstants.XML_WRITE, DavConstants.NAMESPACE);

    private final String localName;
    private final Namespace namespace;

    private int hashCode = -1;

    /**
     * Private constructor.
     *
     * @param name
     * @param namespace
     */
    private Type(String name, Namespace namespace) {
        this.localName = name;
        this.namespace = namespace;
    }

    /**
     * Returns the Xml representation of this lock <code>Type</code>.
     *
     * @return Xml representation
     * @see XMLizable#toXml(Document)
     */
    public Element toXml(Document document) {
        Element lockType = DomUtil.createElement(document, DavConstants.XML_LOCKTYPE, DavConstants.NAMESPACE);
        DomUtil.addChildElement(lockType, localName, namespace);
        return lockType;
    }

    @Override
    public int hashCode() {
        if (hashCode == -1) {
            StringBuilder b = new StringBuilder();
            b.append("LockType : {").append(namespace).append("}").append(localName);
            hashCode = b.toString().hashCode();
        }
        return hashCode;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof Type) {
            Type other = (Type) obj;
            return localName.equals(other.localName) && namespace.equals(other.namespace);
        }
        return false;
    }

    /**
     * Create a <code>Type</code> object from the given Xml element.
     *
     * @param lockType
     * @return <code>Type</code> object.
     */
    public static Type createFromXml(Element lockType) {
        if (lockType != null && DavConstants.XML_LOCKTYPE.equals(lockType.getLocalName())) {
            // we have the parent element and must retrieve the type first
            lockType = DomUtil.getFirstChildElement(lockType);
        }
        if (lockType == null) {
            throw new IllegalArgumentException("'null' is not valid lock type entry.");
        }
        Namespace namespace = Namespace.getNamespace(lockType.getPrefix(), lockType.getNamespaceURI());
        return create(lockType.getLocalName(), namespace);
    }

    /**
     * Create a <code>Type</code> object from the given localName and namespace.
     *
     * @param localName
     * @param namespace
     * @return <code>Type</code> object.
     */
    public static Type create(String localName, Namespace namespace) {
        String key = DomUtil.getExpandedName(localName, namespace);
        if (types.containsKey(key)) {
            return types.get(key);
        } else {
            Type type = new Type(localName, namespace);
            types.put(key, type);
            return type;
        }
    }
}
