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

import java.lang.reflect.InvocationTargetException;

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

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.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.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.AnyValue;
import org.etsi.mts.tdl.CollectionDataInstance;
import org.etsi.mts.tdl.CollectionDataType;
import org.etsi.mts.tdl.DataElementUse;
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.Member;
import org.etsi.mts.tdl.MemberAssignment;
import org.etsi.mts.tdl.Parameter;
import org.etsi.mts.tdl.ParameterBinding;
import org.etsi.mts.tdl.SpecialValueUse;
import org.etsi.mts.tdl.tdlPackage;
import org.etsi.mts.tdl.tdlTables;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Any Value</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * </p>
 * <ul>
 *   <li>{@link org.etsi.mts.tdl.impl.AnyValueImpl#getDataType <em>Data Type</em>}</li>
 * </ul>
 *
 * @generated
 */
public class AnyValueImpl extends SpecialValueUseImpl implements AnyValue
{
	/**
	 * The cached value of the '{@link #getDataType() <em>Data Type</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getDataType()
	 * @generated
	 * @ordered
	 */
	protected DataType dataType;

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

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

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public DataType getDataType()
	{
		if (dataType != null && dataType.eIsProxy()) {
			InternalEObject oldDataType = (InternalEObject)dataType;
			dataType = (DataType)eResolveProxy(oldDataType);
			if (dataType != oldDataType) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, tdlPackage.ANY_VALUE__DATA_TYPE, oldDataType, dataType));
			}
		}
		return dataType;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public DataType basicGetDataType()
	{
		return dataType;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void setDataType(DataType newDataType)
	{
		DataType oldDataType = dataType;
		dataType = newDataType;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, tdlPackage.ANY_VALUE__DATA_TYPE, oldDataType, dataType));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public DataType resolveDataType()
	{
		/**
		 *
		 * if not self.dataType.oclIsUndefined()
		 * then self.dataType
		 * else
		 *   if self.container().oclIsTypeOf(MemberAssignment)
		 *   then
		 *     self.container()
		 *     .oclAsType(MemberAssignment).member.dataType
		 *   else
		 *     if self.container().oclIsTypeOf(ParameterBinding)
		 *     then
		 *       self.container()
		 *       .oclAsType(ParameterBinding).parameter.dataType
		 *     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*/ DataType dataType_0 = this.getDataType();
		final /*@NonInvalid*/ boolean oclIsUndefined = dataType_0 == 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_5;
		if (not) {
			local_5 = dataType_0;
		}
		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 = OclAnyOclIsTypeOfOperation.INSTANCE.evaluate(executor, container, TYP_tdl_c_c_MemberAssignment_0).booleanValue();
			/*@Thrown*/ DataType local_4;
			if (oclIsTypeOf) {
				final /*@Thrown*/ MemberAssignment oclAsType = (MemberAssignment)OclAnyOclAsTypeOperation.INSTANCE.evaluate(executor, container, TYP_tdl_c_c_MemberAssignment_0);
				final /*@Thrown*/ Member member = oclAsType.getMember();
				final /*@Thrown*/ DataType dataType_1 = member.getDataType();
				local_4 = 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_0 = OclAnyOclIsTypeOfOperation.INSTANCE.evaluate(executor, container, TYP_tdl_c_c_ParameterBinding_0).booleanValue();
				/*@Thrown*/ DataType local_3;
				if (oclIsTypeOf_0) {
					final /*@Thrown*/ ParameterBinding oclAsType_0 = (ParameterBinding)OclAnyOclAsTypeOperation.INSTANCE.evaluate(executor, container, TYP_tdl_c_c_ParameterBinding_0);
					final /*@Thrown*/ Parameter parameter = oclAsType_0.getParameter();
					final /*@Thrown*/ DataType dataType_2 = parameter.getDataType();
					local_3 = dataType_2;
				}
				else {
					final /*@NonInvalid*/ org.eclipse.ocl.pivot.Class TYP_tdl_c_c_CollectionDataInstance_0 = idResolver.getClass(tdlTables.CLSSid_CollectionDataInstance, null);
					final /*@NonInvalid*/ boolean oclIsTypeOf_1 = OclAnyOclIsTypeOfOperation.INSTANCE.evaluate(executor, container, TYP_tdl_c_c_CollectionDataInstance_0).booleanValue();
					/*@Thrown*/ DataType local_2;
					if (oclIsTypeOf_1) {
						final /*@NonInvalid*/ org.eclipse.ocl.pivot.Class TYP_tdl_c_c_CollectionDataType_0 = idResolver.getClass(tdlTables.CLSSid_CollectionDataType, null);
						final /*@Thrown*/ CollectionDataInstance oclAsType_1 = (CollectionDataInstance)OclAnyOclAsTypeOperation.INSTANCE.evaluate(executor, container, TYP_tdl_c_c_CollectionDataInstance_0);
						final /*@Thrown*/ DataType dataType_3 = oclAsType_1.getDataType();
						final /*@Thrown*/ CollectionDataType oclAsType_2 = (CollectionDataType)OclAnyOclAsTypeOperation.INSTANCE.evaluate(executor, dataType_3, TYP_tdl_c_c_CollectionDataType_0);
						final /*@Thrown*/ DataType itemType = oclAsType_2.getItemType();
						local_2 = 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_2 = OclAnyOclIsTypeOfOperation.INSTANCE.evaluate(executor, container, TYP_tdl_c_c_DataElementUse_0).booleanValue();
						/*@Thrown*/ DataType local_1;
						if (oclIsTypeOf_2) {
							final /*@NonInvalid*/ org.eclipse.ocl.pivot.Class TYP_tdl_c_c_CollectionDataType_1 = idResolver.getClass(tdlTables.CLSSid_CollectionDataType, null);
							final /*@Thrown*/ DataElementUse oclAsType_3 = (DataElementUse)OclAnyOclAsTypeOperation.INSTANCE.evaluate(executor, container, TYP_tdl_c_c_DataElementUse_0);
							final /*@Thrown*/ DataType resolveDataType = oclAsType_3.resolveDataType();
							final /*@Thrown*/ CollectionDataType oclAsType_4 = (CollectionDataType)OclAnyOclAsTypeOperation.INSTANCE.evaluate(executor, resolveDataType, TYP_tdl_c_c_CollectionDataType_1);
							final /*@Thrown*/ DataType itemType_0 = oclAsType_4.getItemType();
							local_1 = 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_3 = OclAnyOclIsTypeOfOperation.INSTANCE.evaluate(executor, container, TYP_tdl_c_c_DataInstanceUse_0).booleanValue();
							/*@Thrown*/ DataType local_0;
							if (oclIsTypeOf_3) {
								final /*@NonInvalid*/ org.eclipse.ocl.pivot.Class TYP_tdl_c_c_CollectionDataType_2 = idResolver.getClass(tdlTables.CLSSid_CollectionDataType, null);
								final /*@Thrown*/ DataInstanceUse oclAsType_5 = (DataInstanceUse)OclAnyOclAsTypeOperation.INSTANCE.evaluate(executor, container, TYP_tdl_c_c_DataInstanceUse_0);
								final /*@Thrown*/ DataType resolveDataType_0 = oclAsType_5.resolveDataType();
								final /*@Thrown*/ CollectionDataType oclAsType_6 = (CollectionDataType)OclAnyOclAsTypeOperation.INSTANCE.evaluate(executor, resolveDataType_0, TYP_tdl_c_c_CollectionDataType_2);
								final /*@Thrown*/ DataType itemType_1 = oclAsType_6.getItemType();
								local_0 = itemType_1;
							}
							else {
								local_0 = null;
							}
							local_1 = local_0;
						}
						local_2 = local_1;
					}
					local_3 = local_2;
				}
				local_4 = local_3;
			}
			local_5 = local_4;
		}
		return local_5;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType)
	{
		switch (featureID) {
			case tdlPackage.ANY_VALUE__DATA_TYPE:
				if (resolve) return getDataType();
				return basicGetDataType();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eSet(int featureID, Object newValue)
	{
		switch (featureID) {
			case tdlPackage.ANY_VALUE__DATA_TYPE:
				setDataType((DataType)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID)
	{
		switch (featureID) {
			case tdlPackage.ANY_VALUE__DATA_TYPE:
				setDataType((DataType)null);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID)
	{
		switch (featureID) {
			case tdlPackage.ANY_VALUE__DATA_TYPE:
				return dataType != null;
		}
		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.ANY_VALUE___RESOLVE_DATA_TYPE;
				default: return super.eDerivedOperationID(baseOperationID, baseClass);
			}
		}
		if (baseClass == SpecialValueUse.class) {
			switch (baseOperationID) {
				case tdlPackage.SPECIAL_VALUE_USE___RESOLVE_DATA_TYPE: return tdlPackage.ANY_VALUE___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.ANY_VALUE___RESOLVE_DATA_TYPE:
				return resolveDataType();
		}
		return super.eInvoke(operationID, arguments);
	}

} //AnyValueImpl
