package com.ericsson.ims.mtas.xdms.communication.diversion.document;

import javax.xml.bind.JAXBElement;

import com.ericsson.ims.mtas.xdms.cai3g.document.Cai3gDocument;
import com.ericsson.ims.mtas.xdms.cai3g.document.ICai3gDocument;
import com.ericsson.ims.mtas.xdms.communication.diversion.factory.CommunicationDiversionObjectFactory;
import com.ericsson.ims.mtas.xdms.schema.cai3g.AnyMOIdType;
import com.ericsson.ims.mtas.xdms.schema.cai3g.Create;
import com.ericsson.ims.mtas.xdms.schema.cai3g.Delete;
import com.ericsson.ims.mtas.xdms.schema.cai3g.Get;
import com.ericsson.ims.mtas.xdms.schema.cai3g.Set;
import com.ericsson.ims.mtas.xdms.schema.communication.diversion.CdivOperatorConfigurationType;
import com.ericsson.ims.mtas.xdms.schema.communication.diversion.CdivUserConfigurationType;
import com.ericsson.ims.mtas.xdms.schema.communication.diversion.CommunicationDiversionType;
import com.ericsson.ims.mtas.xdms.schema.communication.diversion.ObjectFactory;

public class CommunicationDiversionDocument implements ICommunicationDiversionDocument
{

	private ICai3gDocument cai3gDocument;

	private CommunicationDiversionObjectFactory factory;

	public CommunicationDiversionDocument()
	{
		factory = new CommunicationDiversionObjectFactory();
		cai3gDocument = new Cai3gDocument();
	}

	@Override
	public Create createCreate(String moType, String id)
	{
		Create create = cai3gDocument.createCreate(moType);

		AnyMOIdType anyMOIdType = cai3gDocument.getFactory().createAnyMOIdType();

		anyMOIdType.getAny().add(factory.createPublicId(id));

		JAXBElement<AnyMOIdType> anyMOIdTypeElement = cai3gDocument.getFactory().createCreateMOId(anyMOIdType);

		create.setMOId(anyMOIdTypeElement);

		return create;
	}

	@Override
	public Get createGet(String moType, String id)
	{
		Get get = cai3gDocument.createGet(moType);

		get.setMOType(cai3gDocument.getFactory().createGetMOType(moType));

		AnyMOIdType anyMOIdType = cai3gDocument.getFactory().createAnyMOIdType();

		anyMOIdType.getAny().add(factory.createPublicId(id));

		JAXBElement<AnyMOIdType> anyMOIdTypeElement = cai3gDocument.getFactory().createGetMOId(anyMOIdType);

		get.setMOId(anyMOIdTypeElement);

		return get;
	}

	@Override
	public Set createSet(String moType, String id)
	{
		Set set = cai3gDocument.createSet(moType);

		set.setMOType(cai3gDocument.getFactory().createSetMOType(moType));

		AnyMOIdType anyMOIdType = cai3gDocument.getFactory().createAnyMOIdType();

		anyMOIdType.getAny().add(factory.createPublicId(id));

		JAXBElement<AnyMOIdType> anyMOIdTypeElement = cai3gDocument.getFactory().createSetMOId(anyMOIdType);

		set.setMOId(anyMOIdTypeElement);

		return set;
	}

	@Override
	public Delete createDelete(String moType, String id)
	{
		Delete delete = cai3gDocument.createDelete(moType);

		delete.setMOType(cai3gDocument.getFactory().createDeleteMOType(moType));

		AnyMOIdType anyMOIdType = cai3gDocument.getFactory().createAnyMOIdType();

		anyMOIdType.getAny().add(factory.createPublicId(id));

		JAXBElement<AnyMOIdType> anyMOIdTypeElement = cai3gDocument.getFactory().createDeleteMOId(anyMOIdType);

		delete.setMOId(anyMOIdTypeElement);

		return delete;
	}

	/**
	 * Operator configuration part
	 */
	@Override
	public JAXBElement<CdivOperatorConfigurationType> operatorConfiguration()
	{
		JAXBElement<CdivOperatorConfigurationType> operatorConfiguration;

		CdivOperatorConfigurationType operatorConfigurationType = factory.createCdivOperatorConfigurationType();

		JAXBElement<String> operatorActive = factory.createCdivOperatorConfigurationTypeActivated("activated");

		operatorConfigurationType.setActivated(operatorActive);

		operatorConfiguration = factory
				.createCommunicationDiversionTypeCdivOperatorConfiguration(operatorConfigurationType);

		return operatorConfiguration;
	}

	/**
	 * User configuration part
	 */
	@Override
	public JAXBElement<CdivUserConfigurationType> userConfiguration()
	{
		JAXBElement<CdivUserConfigurationType> userConfiguration;

		CdivUserConfigurationType userConfigurationType = factory.createCdivUserConfigurationType();

		JAXBElement<Boolean> userActive = factory.createCdivUserConfigurationTypeActive(true);

		userConfigurationType.setActive(userActive);

		userConfiguration = factory.createCommunicationDiversionTypeCdivUserConfiguration(userConfigurationType);

		return userConfiguration;
	}

	@Override
	public JAXBElement<CommunicationDiversionType> createDefault()
	{
		CommunicationDiversionType cdt = factory.createCommunicationDiversionType();

		cdt.setCdivOperatorConfiguration(operatorConfiguration());

		cdt.setCdivUserConfiguration(userConfiguration());

		JAXBElement<CommunicationDiversionType> cdiv = factory.createCreateMMTelCommunicationDiversion(cdt);

		return cdiv;
	}

	@Override
	public ObjectFactory getFactory()
	{
		return factory;
	}

	@Override
	public JAXBElement<String> createPublicIdentity(String id)
	{
		return factory.createPublicId(id);
	}
}
