/**
 */
package org.etsi.mts.tdl.impl;

import java.lang.reflect.InvocationTargetException;

import java.util.Collection;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.impl.ENotificationImpl;

import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.InternalEList;

import org.eclipse.ocl.pivot.evaluation.Executor;
import org.eclipse.ocl.pivot.ids.IdResolver;
import org.eclipse.ocl.pivot.library.oclany.OclAnyOclAsTypeOperation;
import org.eclipse.ocl.pivot.library.oclany.OclAnyOclIsKindOfOperation;
import org.eclipse.ocl.pivot.library.oclany.OclAnyOclIsTypeOfOperation;
import org.eclipse.ocl.pivot.utilities.PivotUtil;
import org.eclipse.ocl.pivot.utilities.ValueUtil;
import org.eclipse.ocl.pivot.values.InvalidValueException;
import org.etsi.mts.tdl.CollectionDataInstance;
import org.etsi.mts.tdl.CollectionDataType;
import org.etsi.mts.tdl.DataElementUse;
import org.etsi.mts.tdl.DataInstance;
import org.etsi.mts.tdl.DataInstanceUse;
import org.etsi.mts.tdl.DataType;
import org.etsi.mts.tdl.DataUse;
import org.etsi.mts.tdl.Element;
import org.etsi.mts.tdl.FormalParameter;
import org.etsi.mts.tdl.Function;
import org.etsi.mts.tdl.Member;
import org.etsi.mts.tdl.MemberAssignment;
import org.etsi.mts.tdl.NamedElement;
import org.etsi.mts.tdl.ParameterBinding;
import org.etsi.mts.tdl.UnassignedMemberTreatment;
import org.etsi.mts.tdl.tdlPackage;
import org.etsi.mts.tdl.tdlTables;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Data Element Use</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * </p>
 * <ul>
 *   <li>{@link org.etsi.mts.tdl.impl.DataElementUseImpl#getUnassignedMember <em>Unassigned Member</em>}</li>
 *   <li>{@link org.etsi.mts.tdl.impl.DataElementUseImpl#getDataElement <em>Data Element</em>}</li>
 *   <li>{@link org.etsi.mts.tdl.impl.DataElementUseImpl#getItem <em>Item</em>}</li>
 * </ul>
 *
 * @generated
 */
public class DataElementUseImpl extends DataUseImpl implements DataElementUse
{
	/**
	 * The default value of the '{@link #getUnassignedMember() <em>Unassigned Member</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getUnassignedMember()
	 * @generated
	 * @ordered
	 */
	protected static final UnassignedMemberTreatment UNASSIGNED_MEMBER_EDEFAULT = UnassignedMemberTreatment.UNDEFINED;

	/**
	 * The cached value of the '{@link #getUnassignedMember() <em>Unassigned Member</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getUnassignedMember()
	 * @generated
	 * @ordered
	 */
	protected UnassignedMemberTreatment unassignedMember = UNASSIGNED_MEMBER_EDEFAULT;

	/**
	 * The cached value of the '{@link #getDataElement() <em>Data Element</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getDataElement()
	 * @generated
	 * @ordered
	 */
	protected NamedElement dataElement;

	/**
	 * The cached value of the '{@link #getItem() <em>Item</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getItem()
	 * @generated
	 * @ordered
	 */
	protected EList<DataUse> item;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected DataElementUseImpl()
	{
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass()
	{
		return tdlPackage.Literals.DATA_ELEMENT_USE;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public UnassignedMemberTreatment getUnassignedMember()
	{
		return unassignedMember;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void setUnassignedMember(UnassignedMemberTreatment newUnassignedMember)
	{
		UnassignedMemberTreatment oldUnassignedMember = unassignedMember;
		unassignedMember = newUnassignedMember == null ? UNASSIGNED_MEMBER_EDEFAULT : newUnassignedMember;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, tdlPackage.DATA_ELEMENT_USE__UNASSIGNED_MEMBER, oldUnassignedMember, unassignedMember));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NamedElement getDataElement()
	{
		if (dataElement != null && dataElement.eIsProxy()) {
			InternalEObject oldDataElement = (InternalEObject)dataElement;
			dataElement = (NamedElement)eResolveProxy(oldDataElement);
			if (dataElement != oldDataElement) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, tdlPackage.DATA_ELEMENT_USE__DATA_ELEMENT, oldDataElement, dataElement));
			}
		}
		return dataElement;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NamedElement basicGetDataElement()
	{
		return dataElement;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void setDataElement(NamedElement newDataElement)
	{
		NamedElement oldDataElement = dataElement;
		dataElement = newDataElement;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, tdlPackage.DATA_ELEMENT_USE__DATA_ELEMENT, oldDataElement, dataElement));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public EList<DataUse> getItem()
	{
		if (item == null) {
			item = new EObjectContainmentEList<DataUse>(DataUse.class, this, tdlPackage.DATA_ELEMENT_USE__ITEM);
		}
		return item;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public DataType resolveDataType()
	{
		/**
		 *
		 * if not self.dataElement.oclIsUndefined()
		 * then
		 *   if self.dataElement.oclIsKindOf(DataType)
		 *   then self.dataElement.oclAsType(DataType)
		 *   else
		 *     if self.dataElement.oclIsKindOf(DataInstance)
		 *     then self.dataElement.oclAsType(DataInstance).dataType
		 *     else
		 *       if self.dataElement.oclIsTypeOf(FormalParameter)
		 *       then self.dataElement.oclAsType(FormalParameter).dataType
		 *       else
		 *         if self.dataElement.oclIsTypeOf(Function)
		 *         then self.dataElement.oclAsType(Function).returnType
		 *         else null
		 *         endif
		 *       endif
		 *     endif
		 *   endif
		 * else
		 *   if self.container().oclIsTypeOf(MemberAssignment)
		 *   then
		 *     self.container()
		 *     .oclAsType(MemberAssignment).member.dataType
		 *   else
		 *     if self.container().oclIsTypeOf(ParameterBinding)
		 *     then
		 *       self.container()
		 *       .oclAsType(ParameterBinding)
		 *       .resolveParameterType()
		 *     else
		 *       if
		 *         self.container()
		 *         .oclIsTypeOf(CollectionDataInstance)
		 *       then
		 *         self.container()
		 *         .oclAsType(CollectionDataInstance)
		 *         .dataType.oclAsType(CollectionDataType).itemType
		 *       else
		 *         if self.container().oclIsTypeOf(DataElementUse)
		 *         then
		 *           self.container()
		 *           .oclAsType(DataElementUse)
		 *           .resolveDataType()
		 *           .oclAsType(CollectionDataType).itemType
		 *         else
		 *           if self.container().oclIsTypeOf(DataInstanceUse)
		 *           then
		 *             self.container()
		 *             .oclAsType(DataInstanceUse)
		 *             .resolveDataType()
		 *             .oclAsType(CollectionDataType).itemType
		 *           else null
		 *           endif
		 *         endif
		 *       endif
		 *     endif
		 *   endif
		 * endif
		 */
		final /*@NonInvalid*/ Executor executor = PivotUtil.getExecutor(this);
		final /*@NonInvalid*/ IdResolver idResolver = executor.getIdResolver();
		final /*@NonInvalid*/ NamedElement dataElement_1 = this.getDataElement();
		final /*@NonInvalid*/ boolean oclIsUndefined = dataElement_1 == null;
		final /*@NonInvalid*/ Boolean not;
		if (!oclIsUndefined) {
			not = ValueUtil.TRUE_VALUE;
		}
		else {
			if (oclIsUndefined) {
				not = ValueUtil.FALSE_VALUE;
			}
			else {
				not = null;
			}
		}
		if (not == null) {
			throw new InvalidValueException("Null if condition");
		}
		/*@Thrown*/ DataType local_9;
		if (not) {
			final /*@NonInvalid*/ org.eclipse.ocl.pivot.Class TYP_tdl_c_c_DataType_1 = idResolver.getClass(tdlTables.CLSSid_DataType, null);
			final /*@Thrown*/ boolean oclIsKindOf = OclAnyOclIsKindOfOperation.INSTANCE.evaluate(executor, dataElement_1, TYP_tdl_c_c_DataType_1).booleanValue();
			/*@Thrown*/ DataType local_3;
			if (oclIsKindOf) {
				final /*@Thrown*/ DataType oclAsType = (DataType)OclAnyOclAsTypeOperation.INSTANCE.evaluate(executor, dataElement_1, TYP_tdl_c_c_DataType_1);
				local_3 = oclAsType;
			}
			else {
				final /*@NonInvalid*/ org.eclipse.ocl.pivot.Class TYP_tdl_c_c_DataInstance_0 = idResolver.getClass(tdlTables.CLSSid_DataInstance, null);
				final /*@Thrown*/ boolean oclIsKindOf_0 = OclAnyOclIsKindOfOperation.INSTANCE.evaluate(executor, dataElement_1, TYP_tdl_c_c_DataInstance_0).booleanValue();
				/*@Thrown*/ DataType local_2;
				if (oclIsKindOf_0) {
					final /*@Thrown*/ DataInstance oclAsType_0 = (DataInstance)OclAnyOclAsTypeOperation.INSTANCE.evaluate(executor, dataElement_1, TYP_tdl_c_c_DataInstance_0);
					final /*@Thrown*/ DataType dataType = oclAsType_0.getDataType();
					local_2 = dataType;
				}
				else {
					final /*@NonInvalid*/ org.eclipse.ocl.pivot.Class TYP_tdl_c_c_FormalParameter_0 = idResolver.getClass(tdlTables.CLSSid_FormalParameter, null);
					final /*@Thrown*/ boolean oclIsTypeOf = OclAnyOclIsTypeOfOperation.INSTANCE.evaluate(executor, dataElement_1, TYP_tdl_c_c_FormalParameter_0).booleanValue();
					/*@Thrown*/ DataType local_1;
					if (oclIsTypeOf) {
						final /*@Thrown*/ FormalParameter oclAsType_1 = (FormalParameter)OclAnyOclAsTypeOperation.INSTANCE.evaluate(executor, dataElement_1, TYP_tdl_c_c_FormalParameter_0);
						final /*@Thrown*/ DataType dataType_0 = oclAsType_1.getDataType();
						local_1 = dataType_0;
					}
					else {
						final /*@NonInvalid*/ org.eclipse.ocl.pivot.Class TYP_tdl_c_c_Function_0 = idResolver.getClass(tdlTables.CLSSid_Function, null);
						final /*@Thrown*/ boolean oclIsTypeOf_0 = OclAnyOclIsTypeOfOperation.INSTANCE.evaluate(executor, dataElement_1, TYP_tdl_c_c_Function_0).booleanValue();
						/*@Thrown*/ DataType local_0;
						if (oclIsTypeOf_0) {
							final /*@Thrown*/ Function oclAsType_2 = (Function)OclAnyOclAsTypeOperation.INSTANCE.evaluate(executor, dataElement_1, TYP_tdl_c_c_Function_0);
							final /*@Thrown*/ DataType returnType = oclAsType_2.getReturnType();
							local_0 = returnType;
						}
						else {
							local_0 = null;
						}
						local_1 = local_0;
					}
					local_2 = local_1;
				}
				local_3 = local_2;
			}
			local_9 = local_3;
		}
		else {
			final /*@NonInvalid*/ org.eclipse.ocl.pivot.Class TYP_tdl_c_c_MemberAssignment_0 = idResolver.getClass(tdlTables.CLSSid_MemberAssignment, null);
			final /*@NonInvalid*/ Element container = this.container();
			final /*@NonInvalid*/ boolean oclIsTypeOf_1 = OclAnyOclIsTypeOfOperation.INSTANCE.evaluate(executor, container, TYP_tdl_c_c_MemberAssignment_0).booleanValue();
			/*@Thrown*/ DataType local_8;
			if (oclIsTypeOf_1) {
				final /*@Thrown*/ MemberAssignment oclAsType_3 = (MemberAssignment)OclAnyOclAsTypeOperation.INSTANCE.evaluate(executor, container, TYP_tdl_c_c_MemberAssignment_0);
				final /*@Thrown*/ Member member = oclAsType_3.getMember();
				final /*@Thrown*/ DataType dataType_1 = member.getDataType();
				local_8 = dataType_1;
			}
			else {
				final /*@NonInvalid*/ org.eclipse.ocl.pivot.Class TYP_tdl_c_c_ParameterBinding_0 = idResolver.getClass(tdlTables.CLSSid_ParameterBinding, null);
				final /*@NonInvalid*/ boolean oclIsTypeOf_2 = OclAnyOclIsTypeOfOperation.INSTANCE.evaluate(executor, container, TYP_tdl_c_c_ParameterBinding_0).booleanValue();
				/*@Thrown*/ DataType local_7;
				if (oclIsTypeOf_2) {
					final /*@Thrown*/ ParameterBinding oclAsType_4 = (ParameterBinding)OclAnyOclAsTypeOperation.INSTANCE.evaluate(executor, container, TYP_tdl_c_c_ParameterBinding_0);
					final /*@Thrown*/ DataType resolveParameterType = oclAsType_4.resolveParameterType();
					local_7 = resolveParameterType;
				}
				else {
					final /*@NonInvalid*/ org.eclipse.ocl.pivot.Class TYP_tdl_c_c_CollectionDataInstance_0 = idResolver.getClass(tdlTables.CLSSid_CollectionDataInstance, null);
					final /*@NonInvalid*/ boolean oclIsTypeOf_3 = OclAnyOclIsTypeOfOperation.INSTANCE.evaluate(executor, container, TYP_tdl_c_c_CollectionDataInstance_0).booleanValue();
					/*@Thrown*/ DataType local_6;
					if (oclIsTypeOf_3) {
						final /*@NonInvalid*/ org.eclipse.ocl.pivot.Class TYP_tdl_c_c_CollectionDataType_0 = idResolver.getClass(tdlTables.CLSSid_CollectionDataType, null);
						final /*@Thrown*/ CollectionDataInstance oclAsType_5 = (CollectionDataInstance)OclAnyOclAsTypeOperation.INSTANCE.evaluate(executor, container, TYP_tdl_c_c_CollectionDataInstance_0);
						final /*@Thrown*/ DataType dataType_2 = oclAsType_5.getDataType();
						final /*@Thrown*/ CollectionDataType oclAsType_6 = (CollectionDataType)OclAnyOclAsTypeOperation.INSTANCE.evaluate(executor, dataType_2, TYP_tdl_c_c_CollectionDataType_0);
						final /*@Thrown*/ DataType itemType = oclAsType_6.getItemType();
						local_6 = itemType;
					}
					else {
						final /*@NonInvalid*/ org.eclipse.ocl.pivot.Class TYP_tdl_c_c_DataElementUse_0 = idResolver.getClass(tdlTables.CLSSid_DataElementUse, null);
						final /*@NonInvalid*/ boolean oclIsTypeOf_4 = OclAnyOclIsTypeOfOperation.INSTANCE.evaluate(executor, container, TYP_tdl_c_c_DataElementUse_0).booleanValue();
						/*@Thrown*/ DataType local_5;
						if (oclIsTypeOf_4) {
							final /*@NonInvalid*/ org.eclipse.ocl.pivot.Class TYP_tdl_c_c_CollectionDataType_1 = idResolver.getClass(tdlTables.CLSSid_CollectionDataType, null);
							final /*@Thrown*/ DataElementUse oclAsType_7 = (DataElementUse)OclAnyOclAsTypeOperation.INSTANCE.evaluate(executor, container, TYP_tdl_c_c_DataElementUse_0);
							final /*@Thrown*/ DataType resolveDataType = oclAsType_7.resolveDataType();
							final /*@Thrown*/ CollectionDataType oclAsType_8 = (CollectionDataType)OclAnyOclAsTypeOperation.INSTANCE.evaluate(executor, resolveDataType, TYP_tdl_c_c_CollectionDataType_1);
							final /*@Thrown*/ DataType itemType_0 = oclAsType_8.getItemType();
							local_5 = itemType_0;
						}
						else {
							final /*@NonInvalid*/ org.eclipse.ocl.pivot.Class TYP_tdl_c_c_DataInstanceUse_0 = idResolver.getClass(tdlTables.CLSSid_DataInstanceUse, null);
							final /*@NonInvalid*/ boolean oclIsTypeOf_5 = OclAnyOclIsTypeOfOperation.INSTANCE.evaluate(executor, container, TYP_tdl_c_c_DataInstanceUse_0).booleanValue();
							/*@Thrown*/ DataType local_4;
							if (oclIsTypeOf_5) {
								final /*@NonInvalid*/ org.eclipse.ocl.pivot.Class TYP_tdl_c_c_CollectionDataType_2 = idResolver.getClass(tdlTables.CLSSid_CollectionDataType, null);
								final /*@Thrown*/ DataInstanceUse oclAsType_9 = (DataInstanceUse)OclAnyOclAsTypeOperation.INSTANCE.evaluate(executor, container, TYP_tdl_c_c_DataInstanceUse_0);
								final /*@Thrown*/ DataType resolveDataType_0 = oclAsType_9.resolveDataType();
								final /*@Thrown*/ CollectionDataType oclAsType_10 = (CollectionDataType)OclAnyOclAsTypeOperation.INSTANCE.evaluate(executor, resolveDataType_0, TYP_tdl_c_c_CollectionDataType_2);
								final /*@Thrown*/ DataType itemType_1 = oclAsType_10.getItemType();
								local_4 = itemType_1;
							}
							else {
								local_4 = null;
							}
							local_5 = local_4;
						}
						local_6 = local_5;
					}
					local_7 = local_6;
				}
				local_8 = local_7;
			}
			local_9 = local_8;
		}
		return local_9;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs)
	{
		switch (featureID) {
			case tdlPackage.DATA_ELEMENT_USE__ITEM:
				return ((InternalEList<?>)getItem()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType)
	{
		switch (featureID) {
			case tdlPackage.DATA_ELEMENT_USE__UNASSIGNED_MEMBER:
				return getUnassignedMember();
			case tdlPackage.DATA_ELEMENT_USE__DATA_ELEMENT:
				if (resolve) return getDataElement();
				return basicGetDataElement();
			case tdlPackage.DATA_ELEMENT_USE__ITEM:
				return getItem();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue)
	{
		switch (featureID) {
			case tdlPackage.DATA_ELEMENT_USE__UNASSIGNED_MEMBER:
				setUnassignedMember((UnassignedMemberTreatment)newValue);
				return;
			case tdlPackage.DATA_ELEMENT_USE__DATA_ELEMENT:
				setDataElement((NamedElement)newValue);
				return;
			case tdlPackage.DATA_ELEMENT_USE__ITEM:
				getItem().clear();
				getItem().addAll((Collection<? extends DataUse>)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID)
	{
		switch (featureID) {
			case tdlPackage.DATA_ELEMENT_USE__UNASSIGNED_MEMBER:
				setUnassignedMember(UNASSIGNED_MEMBER_EDEFAULT);
				return;
			case tdlPackage.DATA_ELEMENT_USE__DATA_ELEMENT:
				setDataElement((NamedElement)null);
				return;
			case tdlPackage.DATA_ELEMENT_USE__ITEM:
				getItem().clear();
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID)
	{
		switch (featureID) {
			case tdlPackage.DATA_ELEMENT_USE__UNASSIGNED_MEMBER:
				return unassignedMember != UNASSIGNED_MEMBER_EDEFAULT;
			case tdlPackage.DATA_ELEMENT_USE__DATA_ELEMENT:
				return dataElement != null;
			case tdlPackage.DATA_ELEMENT_USE__ITEM:
				return item != null && !item.isEmpty();
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public int eDerivedOperationID(int baseOperationID, Class<?> baseClass)
	{
		if (baseClass == DataUse.class) {
			switch (baseOperationID) {
				case tdlPackage.DATA_USE___RESOLVE_DATA_TYPE: return tdlPackage.DATA_ELEMENT_USE___RESOLVE_DATA_TYPE;
				default: return super.eDerivedOperationID(baseOperationID, baseClass);
			}
		}
		return super.eDerivedOperationID(baseOperationID, baseClass);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eInvoke(int operationID, EList<?> arguments) throws InvocationTargetException
	{
		switch (operationID) {
			case tdlPackage.DATA_ELEMENT_USE___RESOLVE_DATA_TYPE:
				return resolveDataType();
		}
		return super.eInvoke(operationID, arguments);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString()
	{
		if (eIsProxy()) return super.toString();

		StringBuilder result = new StringBuilder(super.toString());
		result.append(" (unassignedMember: ");
		result.append(unassignedMember);
		result.append(')');
		return result.toString();
	}

} //DataElementUseImpl
