/**
 */
package ua.ansymo.hintco.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.EObjectWithInverseResolvingEList;
import org.eclipse.emf.ecore.util.InternalEList;

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;

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

	/**
	 * The cached value of the '{@link #getValueFrom() <em>Value From</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getValueFrom()
	 * @generated
	 * @ordered
	 */
	protected OutputPortInstance valueFrom;

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

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

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

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public OutputPortInstance getValueFrom() {
		if (valueFrom != null && valueFrom.eIsProxy()) {
			InternalEObject oldValueFrom = (InternalEObject)valueFrom;
			valueFrom = (OutputPortInstance)eResolveProxy(oldValueFrom);
			if (valueFrom != oldValueFrom) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, HintcoPackage.INPUT_PORT_INSTANCE__VALUE_FROM, oldValueFrom, valueFrom));
			}
		}
		return valueFrom;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public OutputPortInstance basicGetValueFrom() {
		return valueFrom;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public NotificationChain basicSetValueFrom(OutputPortInstance newValueFrom, NotificationChain msgs) {
		OutputPortInstance oldValueFrom = valueFrom;
		valueFrom = newValueFrom;
		if (eNotificationRequired()) {
			ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, HintcoPackage.INPUT_PORT_INSTANCE__VALUE_FROM, oldValueFrom, newValueFrom);
			if (msgs == null) msgs = notification; else msgs.add(notification);
		}
		return msgs;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setValueFrom(OutputPortInstance newValueFrom) {
		if (newValueFrom != valueFrom) {
			NotificationChain msgs = null;
			if (valueFrom != null)
				msgs = ((InternalEObject)valueFrom).eInverseRemove(this, HintcoPackage.OUTPUT_PORT_INSTANCE__VALUE_TO, OutputPortInstance.class, msgs);
			if (newValueFrom != null)
				msgs = ((InternalEObject)newValueFrom).eInverseAdd(this, HintcoPackage.OUTPUT_PORT_INSTANCE__VALUE_TO, OutputPortInstance.class, msgs);
			msgs = basicSetValueFrom(newValueFrom, msgs);
			if (msgs != null) msgs.dispatch();
		}
		else if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, HintcoPackage.INPUT_PORT_INSTANCE__VALUE_FROM, newValueFrom, newValueFrom));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public OutputPortInstance getRealValueFrom() {
		OutputPortInstance _xifexpression = null;
		OutputPortInstance _valueFrom = this.getValueFrom();
		if ((_valueFrom instanceof HierarchicalUnitPort)) {
			OutputPortInstance _valueFrom_1 = this.getValueFrom();
			_xifexpression = ((HierarchicalUnitPort) _valueFrom_1).getRealValueFrom();
		}
		else {
			_xifexpression = this.getValueFrom();
		}
		return _xifexpression;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public boolean getsValueFrom(final PortInstance p) {
		OutputPortInstance _valueFrom = this.getValueFrom();
		boolean _tripleEquals = (_valueFrom == p);
		if (_tripleEquals) {
			return true;
		}
		else {
			OutputPortInstance _valueFrom_1 = this.getValueFrom();
			if ((_valueFrom_1 instanceof InputPortInstance)) {
				OutputPortInstance _valueFrom_2 = this.getValueFrom();
				return ((InputPortInstance) _valueFrom_2).getsValueFrom(p);
			}
			else {
				return false;
			}
		}
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public boolean isInput() {
		return true;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public NotificationChain eInverseAdd(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case HintcoPackage.INPUT_PORT_INSTANCE__INTERNAL_VALUE_TO:
				return ((InternalEList<InternalEObject>)(InternalEList<?>)getInternalValueTo()).basicAdd(otherEnd, msgs);
			case HintcoPackage.INPUT_PORT_INSTANCE__VALUE_FROM:
				if (valueFrom != null)
					msgs = ((InternalEObject)valueFrom).eInverseRemove(this, HintcoPackage.OUTPUT_PORT_INSTANCE__VALUE_TO, OutputPortInstance.class, msgs);
				return basicSetValueFrom((OutputPortInstance)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.INPUT_PORT_INSTANCE__INTERNAL_VALUE_TO:
				return ((InternalEList<?>)getInternalValueTo()).basicRemove(otherEnd, msgs);
			case HintcoPackage.INPUT_PORT_INSTANCE__VALUE_FROM:
				return basicSetValueFrom(null, 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.INPUT_PORT_INSTANCE__INTERNAL_VALUE_TO:
				return getInternalValueTo();
			case HintcoPackage.INPUT_PORT_INSTANCE__VALUE_FROM:
				if (resolve) return getValueFrom();
				return basicGetValueFrom();
		}
		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.INPUT_PORT_INSTANCE__INTERNAL_VALUE_TO:
				getInternalValueTo().clear();
				getInternalValueTo().addAll((Collection<? extends OutputPortInstance>)newValue);
				return;
			case HintcoPackage.INPUT_PORT_INSTANCE__VALUE_FROM:
				setValueFrom((OutputPortInstance)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case HintcoPackage.INPUT_PORT_INSTANCE__INTERNAL_VALUE_TO:
				getInternalValueTo().clear();
				return;
			case HintcoPackage.INPUT_PORT_INSTANCE__VALUE_FROM:
				setValueFrom((OutputPortInstance)null);
				return;
		}
		super.eUnset(featureID);
	}

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

	/**
	 * <!-- 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.INPUT_PORT_INSTANCE___IS_INPUT;
				case HintcoPackage.PORT_INSTANCE___GETS_VALUE_FROM__PORTINSTANCE: return HintcoPackage.INPUT_PORT_INSTANCE___GETS_VALUE_FROM__PORTINSTANCE;
				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 HintcoPackage.INPUT_PORT_INSTANCE___GET_REAL_VALUE_FROM:
				return getRealValueFrom();
			case HintcoPackage.INPUT_PORT_INSTANCE___GETS_VALUE_FROM__PORTINSTANCE:
				return getsValueFrom((PortInstance)arguments.get(0));
			case HintcoPackage.INPUT_PORT_INSTANCE___IS_INPUT:
				return isInput();
		}
		return super.eInvoke(operationID, arguments);
	}

} //InputPortInstanceImpl
