package dataMapper.diagram.custom.configuration.operators;

import java.util.ArrayList;

import org.eclipse.emf.common.util.EList;

import dataMapper.Element;
import dataMapper.Operator;
import dataMapper.OperatorRightConnector;
import dataMapper.SchemaDataType;
import dataMapper.Split;
import dataMapper.TreeNode;
import dataMapper.diagram.custom.configuration.function.AssignmentStatement;

public class SplitTransform implements OperatorsTransformer {

	@Override
	public AssignmentStatement transform(Operator operator) {
		AssignmentStatement assign = new AssignmentStatement();
		StringBuilder statement = new StringBuilder();
		ArrayList<Element> splitOutputs = getOutputElements(operator);
		Element splitInput = getInputElement(operator);
		String index = "";
		if(splitInput.getFieldParent().getSchemaDataType().equals(SchemaDataType.ARRAY)){
			index = "[i]";
		}
		Split split = (Split) operator;
		int i=0;
		for(Element output : splitOutputs){

				statement.append(output.getFieldParent().getName()+index+"."+output.getName()+ " = " + splitInput.getFieldParent().getName()+index+"."+splitInput.getName()+".split(\""+split.getDelimiter()+"\")"+"["+i+"];\n");

			i++;
		}
		
		assign.setStatement(statement.toString());
		return assign;
	}
	
	private Element getInputElement( Operator operator) {
		return operator.getBasicContainer().getLeftContainer().getLeftConnectors().get(0).getInNode().getIncomingLink().get(0).getOutNode().getElementParent();
	}
	@Override
	public TreeNode getOutputElementParent(Operator operator) {
		return getOutputElements(operator).get(0).getFieldParent();
	}
	
	private ArrayList<Element> getOutputElements(Operator operator) {
		EList<OperatorRightConnector> rightConnectors = operator.getBasicContainer().getRightContainer().getRightConnectors();
		ArrayList<Element> elementList = new ArrayList<Element>();
		for(OperatorRightConnector connector : rightConnectors){
			elementList.add(connector.getOutNode().getOutgoingLink().get(0).getInNode().getElementParent());
		}
		return elementList;
	}

}

@@ -952,16 +952,52 @@
 tputs)%7B%0A
+%09%09%09if(split.getDelimiter() != null)%7B
 %0A%09%09%09%09sta
@@ -1202,17 +1202,230 @@
 %22%5D;%5Cn%22);
-%0A
+%09%09%09%09%0A%09%09%09%7D%0A%09%09%09else %7B%0A%09%09%09%09statement.append(output.getFieldParent().getName()+index+%22.%22+output.getName()+ %22 = %22 + splitInput.getFieldParent().getName()+index+%22.%22+splitInput.getName()+%22.split(%5C%22%5C%22)%22+%22%5B%22+i+%22%5D;%5Cn%22);%0A%09%09%09%7D
 %0A%09%09%09i++;

handle split operator default delimiter