package innovimax.mixthem.operation;

import innovimax.mixthem.MixException;
import innovimax.mixthem.arguments.RuleParam;
import innovimax.mixthem.arguments.ParamValue;

import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
* <p>Zips two lines on a common field.</p>
* @see ILineOperation
* @author Innovimax
* @version 1.0
*/
public class DefaultLineZipping extends AbstractLineOperation {
	
	private final ZipType type;
	private final String sep;
	
	/**
	* Constructor
	* @param type The type of zip to process
 	* @param params The list of parameters (maybe empty)
	* @see innovimax.mixthem.operation.ZipType
	* @see innovimax.mixthem.arguments.RuleParam
	* @see innovimax.mixthem.arguments.ParamValue
	*/
	public DefaultLineZipping(final ZipType type, final Map<RuleParam, ParamValue> params) {
		super(params);
		this.type = type;
		this.sep = params.getOrDefault(RuleParam.ZIP_SEP, ZipOperation.DEFAULT_ZIP_SEPARATOR.getValue()).asString();	
	}
	
	@Override
	public void process(final String line1, final String line2, final LineResult result) throws MixException {
		result.reset();
		switch (this.type) {
			case LINE:
				result.setResult((line1 != null ? line1 : "") + this.sep + (line2 != null ? line2 : ""));
				break;
			case CELL:					
				final Iterator<String> iterator1 = line1 != null ? Arrays.asList(line1.split(CellOperation.DEFAULT_SPLIT_CELL_REGEX.getValue().asString())).iterator() : Collections.emptyIterator();
				final Iterator<String> iterator2 = line2 != null ? Arrays.asList(line2.split(CellOperation.DEFAULT_SPLIT_CELL_REGEX.getValue().asString())).iterator() : Collections.emptyIterator();				
				final StringBuffer buf = new StringBuffer();
				while (iterator1.hasNext() || iterator2.hasNext()) {						
					final String cell1 = iterator1.hasNext() ? iterator1.next() : "";
					final String cell2 = iterator2.hasNext() ? iterator2.next() : "";					
					if (buf.length() > 0) {						
						buf.append(CellOperation.DEFAULT_CELL_SEPARATOR.getValue().asString());
					}					
					buf.append(cell1 + this.sep + cell2);					
				}
				result.setResult(buf.toString());
		}
	}
	
	@Override
	public void process(final List<String> lineRange, final LineResult result) throws MixException {
		//TODO
		process(lineRange.get(0), lineRange.get(1), result);
	}

}

@@ -2301,15 +2301,8 @@
 %09%09//
-TODO%0A%09%09
 proc
@@ -2351,13 +2351,552 @@
 sult);%0A%09
+%09result.reset();%0A%09%09System.out.println(%22RANGE=%22+lineRange.toString());%0A%09%09boolean zipable = true;%09%09%0A%09%09for (int i=0; i %3C lineRange.size(); i++) %7B%09%09%09%0A%09%09%09if (lineRange.get(i) == null) %7B%0A%09%09%09%09zipable = false;%0A%09%09%09%09break;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09if (zipable) %7B%09%09%09%0A%09%09%09switch (this.type) %7B%0A%09%09%09%09case LINE:%0A%09%09%09%09%09StringBuilder zip = new StringBuilder();%0A%09%09%09%09%09int index = 0;%0A%09%09%09%09%09for (String line : lineRange) %7B%0A%09%09%09%09%09%09if (index %3E 0) %7B%0A%09%09%09%09%09%09%09zip.append(this.sep);%0A%09%09%09%09%09%09%7D%0A%09%09%09%09%09%09zip.append(line);%09%09%09%09%09%09%0A%09%09%09%09%09%7D%09%09%09%09%09%0A%09%09%09%09%09break;%0A%09%09%09%09case CELL:%0A%09%09%09%09%09//TODO%09%09%09%09%09%0A%09%09%09%7D%0A%09%09%7D%0A%09
 %7D
-%0A
 %0A%7D%0A

Update DefaultLineZipping.java