/**
 */
package ua.ansymo.hintco.impl;

import com.google.common.base.Objects;

import com.google.common.collect.Iterators;

import java.lang.reflect.InvocationTargetException;

import java.util.Collection;

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.util.EObjectWithInverseResolvingEList;
import org.eclipse.emf.ecore.util.InternalEList;

import org.eclipse.xtext.xbase.lib.Functions.Function1;

import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.IteratorExtensions;

import ua.ansymo.hintco.HierarchicalUnitPort;
import ua.ansymo.hintco.HintcoPackage;
import ua.ansymo.hintco.InputPortInstance;
import ua.ansymo.hintco.OutputPortInstance;
import ua.ansymo.hintco.PortInstance;
import ua.ansymo.hintco.UnitInstance;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Hierarchical Unit Port</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * </p>
 * <ul>
 *   <li>{@link ua.ansymo.hintco.impl.HierarchicalUnitPortImpl#getValueTo <em>Value To</em>}</li>
 *   <li>{@link ua.ansymo.hintco.impl.HierarchicalUnitPortImpl#getInternalValueFrom <em>Internal Value From</em>}</li>
 * </ul>
 *
 * @generated
 */
public class HierarchicalUnitPortImpl extends InputPortInstanceImpl implements HierarchicalUnitPort {
	/**
	 * The cached value of the '{@link #getValueTo() <em>Value To</em>}' reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getValueTo()
	 * @generated
	 * @ordered
	 */
	protected EList<InputPortInstance> valueTo;

	/**
	 * The cached value of the '{@link #getInternalValueFrom() <em>Internal Value From</em>}' reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getInternalValueFrom()
	 * @generated
	 * @ordered
	 */
	protected EList<InputPortInstance> internalValueFrom;

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

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

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<InputPortInstance> getValueTo() {
		if (valueTo == null) {
			valueTo = new EObjectWithInverseResolvingEList<InputPortInstance>(InputPortInstance.class, this, HintcoPackage.HIERARCHICAL_UNIT_PORT__VALUE_TO, HintcoPackage.INPUT_PORT_INSTANCE__VALUE_FROM);
		}
		return valueTo;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<InputPortInstance> getInternalValueFrom() {
		if (internalValueFrom == null) {
			internalValueFrom = new EObjectWithInverseResolvingEList.ManyInverse<InputPortInstance>(InputPortInstance.class, this, HintcoPackage.HIERARCHICAL_UNIT_PORT__INTERNAL_VALUE_FROM, HintcoPackage.INPUT_PORT_INSTANCE__INTERNAL_VALUE_TO);
		}
		return internalValueFrom;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public boolean isInput() {
		boolean _xblockexpression = false;
		{
			final Function1<OutputPortInstance, Boolean> _function = new Function1<OutputPortInstance, Boolean>() {
				public Boolean apply(final OutputPortInstance p) {
					OutputPortInstance _valueFrom = HierarchicalUnitPortImpl.this.getValueFrom();
					return Boolean.valueOf(Objects.equal(p, _valueFrom));
				}
			};
			boolean _exists = IteratorExtensions.<OutputPortInstance>exists(Iterators.<OutputPortInstance>filter(this.getUnit().eAllContents(), OutputPortInstance.class), _function);
			final boolean auxIsInput = (!_exists);
			final boolean auxAltIsInput = this.altIsInput();
			if (((auxIsInput && (!auxAltIsInput)) || ((!auxIsInput) && auxAltIsInput))) {
				throw new RuntimeException("Something wrong with the implementation of isInput");
			}
			_xblockexpression = auxIsInput;
		}
		return _xblockexpression;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public boolean altIsInput() {
		boolean _xifexpression = false;
		boolean _isEmpty = this.getValueTo().isEmpty();
		if (_isEmpty) {
			_xifexpression = false;
		}
		else {
			final Function1<InputPortInstance, Boolean> _function = new Function1<InputPortInstance, Boolean>() {
				public Boolean apply(final InputPortInstance trgP) {
					boolean _xifexpression = false;
					UnitInstance _unit = trgP.getUnit();
					UnitInstance _unit_1 = HierarchicalUnitPortImpl.this.getUnit();
					boolean _tripleEquals = (_unit == _unit_1);
					if (_tripleEquals) {
						_xifexpression = false;
					}
					else {
						boolean _xblockexpression = false;
						{
							final Function1<InputPortInstance, Boolean> _function = new Function1<InputPortInstance, Boolean>() {
								public Boolean apply(final InputPortInstance ip) {
									return Boolean.valueOf((ip == trgP));
								}
							};
							final boolean internal = IteratorExtensions.<InputPortInstance>exists(Iterators.<InputPortInstance>filter(HierarchicalUnitPortImpl.this.getUnit().eAllContents(), InputPortInstance.class), _function);
							boolean _xifexpression_1 = false;
							if ((!internal)) {
								_xifexpression_1 = false;
							}
							else {
								boolean _xifexpression_2 = false;
								if ((trgP instanceof HierarchicalUnitPort)) {
									_xifexpression_2 = ((HierarchicalUnitPort)trgP).altIsInput();
								}
								else {
									_xifexpression_2 = true;
								}
								_xifexpression_1 = _xifexpression_2;
							}
							_xblockexpression = _xifexpression_1;
						}
						_xifexpression = _xblockexpression;
					}
					return Boolean.valueOf(_xifexpression);
				}
			};
			_xifexpression = IterableExtensions.<InputPortInstance>forall(this.getValueTo(), _function);
		}
		return _xifexpression;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case HintcoPackage.HIERARCHICAL_UNIT_PORT__VALUE_TO:
				return ((InternalEList<InternalEObject>)(InternalEList<?>)getValueTo()).basicAdd(otherEnd, msgs);
			case HintcoPackage.HIERARCHICAL_UNIT_PORT__INTERNAL_VALUE_FROM:
				return ((InternalEList<InternalEObject>)(InternalEList<?>)getInternalValueFrom()).basicAdd(otherEnd, msgs);
		}
		return super.eInverseAdd(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case HintcoPackage.HIERARCHICAL_UNIT_PORT__VALUE_TO:
				return ((InternalEList<?>)getValueTo()).basicRemove(otherEnd, msgs);
			case HintcoPackage.HIERARCHICAL_UNIT_PORT__INTERNAL_VALUE_FROM:
				return ((InternalEList<?>)getInternalValueFrom()).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 HintcoPackage.HIERARCHICAL_UNIT_PORT__VALUE_TO:
				return getValueTo();
			case HintcoPackage.HIERARCHICAL_UNIT_PORT__INTERNAL_VALUE_FROM:
				return getInternalValueFrom();
		}
		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 HintcoPackage.HIERARCHICAL_UNIT_PORT__VALUE_TO:
				getValueTo().clear();
				getValueTo().addAll((Collection<? extends InputPortInstance>)newValue);
				return;
			case HintcoPackage.HIERARCHICAL_UNIT_PORT__INTERNAL_VALUE_FROM:
				getInternalValueFrom().clear();
				getInternalValueFrom().addAll((Collection<? extends InputPortInstance>)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case HintcoPackage.HIERARCHICAL_UNIT_PORT__VALUE_TO:
				getValueTo().clear();
				return;
			case HintcoPackage.HIERARCHICAL_UNIT_PORT__INTERNAL_VALUE_FROM:
				getInternalValueFrom().clear();
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case HintcoPackage.HIERARCHICAL_UNIT_PORT__VALUE_TO:
				return valueTo != null && !valueTo.isEmpty();
			case HintcoPackage.HIERARCHICAL_UNIT_PORT__INTERNAL_VALUE_FROM:
				return internalValueFrom != null && !internalValueFrom.isEmpty();
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public int eBaseStructuralFeatureID(int derivedFeatureID, Class<?> baseClass) {
		if (baseClass == OutputPortInstance.class) {
			switch (derivedFeatureID) {
				case HintcoPackage.HIERARCHICAL_UNIT_PORT__VALUE_TO: return HintcoPackage.OUTPUT_PORT_INSTANCE__VALUE_TO;
				case HintcoPackage.HIERARCHICAL_UNIT_PORT__INTERNAL_VALUE_FROM: return HintcoPackage.OUTPUT_PORT_INSTANCE__INTERNAL_VALUE_FROM;
				default: return -1;
			}
		}
		return super.eBaseStructuralFeatureID(derivedFeatureID, baseClass);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public int eDerivedStructuralFeatureID(int baseFeatureID, Class<?> baseClass) {
		if (baseClass == OutputPortInstance.class) {
			switch (baseFeatureID) {
				case HintcoPackage.OUTPUT_PORT_INSTANCE__VALUE_TO: return HintcoPackage.HIERARCHICAL_UNIT_PORT__VALUE_TO;
				case HintcoPackage.OUTPUT_PORT_INSTANCE__INTERNAL_VALUE_FROM: return HintcoPackage.HIERARCHICAL_UNIT_PORT__INTERNAL_VALUE_FROM;
				default: return -1;
			}
		}
		return super.eDerivedStructuralFeatureID(baseFeatureID, baseClass);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public int eDerivedOperationID(int baseOperationID, Class<?> baseClass) {
		if (baseClass == PortInstance.class) {
			switch (baseOperationID) {
				case HintcoPackage.PORT_INSTANCE___IS_INPUT: return HintcoPackage.HIERARCHICAL_UNIT_PORT___IS_INPUT;
				default: return super.eDerivedOperationID(baseOperationID, baseClass);
			}
		}
		if (baseClass == InputPortInstance.class) {
			switch (baseOperationID) {
				case HintcoPackage.INPUT_PORT_INSTANCE___IS_INPUT: return HintcoPackage.HIERARCHICAL_UNIT_PORT___IS_INPUT;
				default: return super.eDerivedOperationID(baseOperationID, baseClass);
			}
		}
		if (baseClass == OutputPortInstance.class) {
			switch (baseOperationID) {
				case HintcoPackage.OUTPUT_PORT_INSTANCE___IS_INPUT: return HintcoPackage.HIERARCHICAL_UNIT_PORT___IS_INPUT;
				default: return -1;
			}
		}
		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 HintcoPackage.HIERARCHICAL_UNIT_PORT___IS_INPUT:
				return isInput();
			case HintcoPackage.HIERARCHICAL_UNIT_PORT___ALT_IS_INPUT:
				return altIsInput();
		}
		return super.eInvoke(operationID, arguments);
	}

} //HierarchicalUnitPortImpl
