package org.eclipse.cosmos.me.sdd.tooling.btg.aggregator.comparisons;

import java.util.Iterator;
import java.util.Vector;

import org.eclipse.cosmos.me.sdd.schema.DerivedVariableType;
import org.eclipse.cosmos.me.sdd.schema.ObjectFactory;
import org.eclipse.cosmos.me.sdd.schema.ParametersType;
import org.eclipse.cosmos.me.sdd.schema.ResourcePropertyType;
import org.eclipse.cosmos.me.sdd.schema.VariablesType;

public class VariablesCompare
{

	public static VariablesType merge(VariablesType v1, VariablesType v2)
	{
		ObjectFactory objectFactory = new ObjectFactory();
		VariablesType merged = objectFactory.createVariablesType();
		
		//get parameter types out and merge em.
		Vector <ParametersType> parameters1 = new Vector <ParametersType>();
		Vector <ResourcePropertyType> properties1 = new Vector <ResourcePropertyType>();
		Vector <DerivedVariableType> dvs1 = new Vector <DerivedVariableType>();
		Vector <ParametersType> parameters2 = new Vector <ParametersType>();
		Vector <ResourcePropertyType> properties2 = new Vector <ResourcePropertyType>();
		Vector <DerivedVariableType> dvs2 = new Vector <DerivedVariableType>();
		
		Iterator <Object> iter1 = v1.getParametersOrResourcePropertyOrDerivedVariable().iterator();
		Iterator <Object> iter2 = v2.getParametersOrResourcePropertyOrDerivedVariable().iterator();
		
		while (iter1.hasNext())
		{
			Object o = iter1.next();
			if (o instanceof ParametersType)
				parameters1.add((ParametersType)o);
			if (o instanceof ResourcePropertyType)
				properties1.add((ResourcePropertyType)o);
			if (o instanceof DerivedVariableType)
				dvs1.add((DerivedVariableType)o);
		}
		
		while (iter2.hasNext())
		{
			Object o = iter2.next();
			if (o instanceof ParametersType)
				parameters2.add((ParametersType)o);
			if (o instanceof ResourcePropertyType)
				properties2.add((ResourcePropertyType)o);
			if (o instanceof DerivedVariableType)
				dvs2.add((DerivedVariableType)o);
		}
		
		merged.getParametersOrResourcePropertyOrDerivedVariable().add(ParametersCompare.merge(parameters1,parameters2));
		merged.getParametersOrResourcePropertyOrDerivedVariable().addAll(ResourcePropertyCompare.merge(properties1,properties2));
		merged.getParametersOrResourcePropertyOrDerivedVariable().addAll(DerivedVariableCompare.merge(dvs1,dvs2));
		
		
		return merged;
	}

}
