/***********************************************************
Copyright (C) 2018 VeriSign, Inc.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-0107  USA

http://www.verisign.com/nds/naming/namestore/techdocs.html
 ***********************************************************/

package com.verisign.epp.codec.registry.v02;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.verisign.epp.codec.gen.EPPCodecComponent;
import com.verisign.epp.codec.gen.EPPCodecException;
import com.verisign.epp.codec.gen.EPPDecodeException;
import com.verisign.epp.codec.gen.EPPEncodeException;
import com.verisign.epp.codec.gen.EPPUtil;


/**
 * This class holds a {@code List} of supported status used in
 * {@link EPPRegistryDomain}, {@link EPPRegistryHost} and
 * {@link EPPRegistryContact}, as per RFC 5731, 5732 and 5733, respectively.
 *
 * @see com.verisign.epp.codec.registry.v02.EPPRegistryDomain
 * @see com.verisign.epp.codec.registry.v02.EPPRegistryHost
 * @see com.verisign.epp.codec.registry.v02.EPPRegistryContact
 */
public class EPPRegistrySupportedStatus implements EPPCodecComponent {

	/**
	 * Logger
	 */
	    private static Logger cat = LoggerFactory.getLogger(EPPRegistrySupportedStatus.class);
	      

	/**
	 * XML local name for {@code EPPRegistrySupportedStatus}.
	 */
	public static final String ELM_LOCALNAME = "supportedStatus";

	/**
	 * XML root tag for {@code EPPRegistrySupportedStatus}.
	 */
	public static final String ELM_NAME = EPPRegistryMapFactory.NS_PREFIX + ":" + ELM_LOCALNAME;

	/**
	 * XML Element Name of {@code status} attribute.
	 */
	public static final String ELM_NAME_STATUS = "status";

	/**
	 * {@code List} of status {@code String}
	 */
	private List statuses = new ArrayList();

	/**
	 * Encode a DOM Element tree from the attributes of the
	 * {@code EPPRegistrySupportedStatus} instance.
	 *
	 * @param aDocument
	 *           DOM Document that is being built. Used as an Element factory.
	 *
	 * @return Element Root DOM Element representing the
	 *         {@code EPPRegistrySupportedStatus} instance.
	 *
	 * @exception EPPEncodeException
	 *               - Unable to encode {@code EPPRegistrySupportedStatus}
	 *               instance.
	 */
	@Override
	public Element encode(Document aDocument) throws EPPEncodeException {
		try {
			validateState();
		}
		catch (EPPCodecException e) {
			throw new EPPEncodeException("Invalid state on EPPRegistrySupportedStatus.encode: " + e);
		}

		Element root = aDocument.createElementNS(EPPRegistryMapFactory.NS, ELM_NAME);

		EPPUtil.encodeList(aDocument, root, this.statuses, EPPRegistryMapFactory.NS,
		      EPPRegistryMapFactory.NS_PREFIX + ":" + ELM_NAME_STATUS);

		return root;
	}

	/**
	 * Decode the {@code EPPRegistrySupportedStatus} attributes from the aElement
	 * DOM Element tree.
	 *
	 * @param aElement
	 *           Root DOM Element to decode {@code EPPRegistrySupportedStatus}
	 *           from.
	 *
	 * @exception EPPDecodeException
	 *               Unable to decode aElement
	 */
	@Override
	public void decode(Element aElement) throws EPPDecodeException {
		this.statuses = EPPUtil.decodeList(aElement, EPPRegistryMapFactory.NS, ELM_NAME_STATUS);
	}

	/**
	 * Validate the state of the {@code EPPRegistrySupportedStatus} instance. A
	 * valid state means that all of the required attributes have been set. If
	 * validateState returns without an exception, the state is valid. If the
	 * state is not valid, the {@code EPPCodecException} will contain a
	 * description of the error.
	 *
	 * @throws EPPCodecException
	 */
	void validateState() throws EPPCodecException {
		if (!this.hasStatuses()) {
			throw new EPPCodecException("statuses is required");
		}
	}

	/**
	 * Clone {@code EPPRegistrySupportedStatus}.
	 *
	 * @return clone of {@code EPPRegistrySupportedStatus}
	 *
	 * @exception CloneNotSupportedException
	 *               standard Object.clone exception
	 */
	@Override
	public Object clone() throws CloneNotSupportedException {
		EPPRegistrySupportedStatus clone = (EPPRegistrySupportedStatus) super.clone();

		if (this.statuses != null) {
			clone.statuses = (List) ((ArrayList) this.statuses).clone();
		}

		return clone;
	}

	/**
	 * implements a deep {@code EPPRegistrySupportedStatus} compare.
	 *
	 * @param aObject
	 *           {@code EPPRegistrySupportedStatus} instance to compare with
	 *
	 * @return {@code true} if this object is the same as the aObject argument;
	 *         {@code false} otherwise
	 */
	@Override
	public boolean equals(Object aObject) {
		if (!(aObject instanceof EPPRegistrySupportedStatus)) {
			return false;
		}

		EPPRegistrySupportedStatus theComp = (EPPRegistrySupportedStatus) aObject;

		if (!((this.statuses == null) ? (theComp.statuses == null)
		      : EPPUtil.equalLists(this.statuses, theComp.statuses))) {
			cat.error("EPPRegistrySupportedStatus.equals(): statuses not equal");
			return false;
		}

		return true;
	}

	/**
	 * Implementation of {@code Object.toString}, which will result in an
	 * indented XML {@code String} representation of the concrete
	 * {@code EPPCodecComponent}.
	 *
	 * @return Indented XML {@code String} if successful; {@code ERROR}
	 *         otherwise.
	 */
	@Override
	public String toString() {
		return EPPUtil.toString(this);
	}

	/**
	 * Is there any statuses set?
	 * 
	 * @return <code>true</code> if there is at least one status set.
	 */
	public boolean hasStatuses() {
		if (this.statuses != null && !this.statuses.isEmpty()) {
			return true;
		}
		else {
			return false;
		}
	}

	/**
	 * Gets the {@code List} of statuses.
	 *
	 * @return {@code List} of statuses in {@code String}
	 */
	public List getStatuses() {
		return this.statuses;
	}

	/**
	 * Sets the {@code List} of statuses.
	 *
	 * @param statuses
	 *           {@code List} of statuses in {@code String}
	 */
	public void setStatuses(List statuses) {
		this.statuses = statuses;
	}

	/**
	 * Add one status to an existing {@code List}.
	 *
	 * @param status
	 *           status {@code String}
	 */
	public void addStatus(String status) {
		if (this.statuses == null) {
			this.statuses = new ArrayList();
		}
		this.statuses.add(status);
	}

	/**
	 * Constants for the supported statuses.
	 */
	public static class Status {
		public static final String DOMAIN_CLIENTDELETEPROHIBITED = "clientDeleteProhibited";
		public static final String DOMAIN_SERVERDELETEPROHIBITED = "serverDeleteProhibited";
		public static final String DOMAIN_CLIENTHOLD = "clientHold";
		public static final String DOMAIN_SERVERHOLD = "serverHold";
		public static final String DOMAIN_CLIENTRENEWPROHIBITED = "clientRenewProhibited";
		public static final String DOMAIN_SERVERRENEWPROHIBITED = "serverRenewProhibited";
		public static final String DOMAIN_CLIENTTRANSFERPROHIBITED = "clientTransferProhibited";
		public static final String DOMAIN_SERVERTRANSFERPROHIBITED = "serverTransferProhibited";
		public static final String DOMAIN_CLIENTUPDATEPROHIBITED = "clientUpdateProhibited";
		public static final String DOMAIN_SERVERUPDATEPROHIBITED = "serverUpdateProhibited";
		public static final String DOMAIN_INACTIVE = "inactive";
		public static final String DOMAIN_OK = "ok";
		public static final String DOMAIN_PENDINGCREATE = "pendingCreate";
		public static final String DOMAIN_PENDINGDELETE = "pendingDelete";
		public static final String DOMAIN_PENDINGRENEW = "pendingRenew";
		public static final String DOMAIN_PENDINGTRANSFER = "pendingTransfer";
		public static final String DOMAIN_PENDINGUPDATE = "pendingUpdate";

		public static final String HOST_CLIENTDELETEPROHIBITED = "clientDeleteProhibited";
		public static final String HOST_SERVERDELETEPROHIBITED = "serverDeleteProhibited";
		public static final String HOST_CLIENTUPDATEPROHIBITED = "clientUpdateProhibited";
		public static final String HOST_SERVERUPDATEPROHIBITED = "serverUpdateProhibited";
		public static final String HOST_LINKED = "linked";
		public static final String HOST_OK = "ok";
		public static final String HOST_PENDINGCREATE = "pendingCreate";
		public static final String HOST_PENDINGDELETE = "pendingDelete";
		public static final String HOST_PENDINGTRANSFER = "pendingTransfer";
		public static final String HOST_PENDINGUPDATE = "pendingUpdate";

		public static final String CONTACT_CLIENTDELETEPROHIBITED = "clientDeleteProhibited";
		public static final String CONTACT_SERVERDELETEPROHIBITED = "serverDeleteProhibited";
		public static final String CONTACT_CLIENTTRANSFERPROHIBITED = "clientTransferProhibited";
		public static final String CONTACT_SERVERTRANSFERPROHIBITED = "serverTransferProhibited";
		public static final String CONTACT_CLIENTUPDATEPROHIBITED = "clientUpdateProhibited";
		public static final String CONTACT_SERVERUPDATEPROHIBITED = "serverUpdateProhibited";
		public static final String CONTACT_LINKED = "linked";
		public static final String CONTACT_OK = "ok";
		public static final String CONTACT_PENDINGCREATE = "pendingCreate";
		public static final String CONTACT_PENDINGDELETE = "pendingDelete";
		public static final String CONTACT_PENDINGTRANSFER = "pendingTransfer";
		public static final String CONTACT_PENDINGUPDATE = "pendingUpdate";
	}

	/**
	 * Returns the XML namespace associated with the {@code EPPCodecComponent}.
	 *
	 * @return XML namespace for the {@code EPPCodecComponent}.
	 */
	@Override
	public String getNamespace() {
		return EPPRegistryMapFactory.NS;
	}

}
