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.Map;
import java.util.List;

/**
* <p>Zips two lines on a common field.</p>
* @see ILineOperation
* @author Innovimax
* @version 1.0
*/
public class DefaultLineZipping extends AbstractLineOperation {
	
	private final static String DEFAULT_ZIP_SEPARATOR = "";

	private final ZipType type;
	
	/**
	* 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(ZipType type, Map<RuleParam, ParamValue> params) {
		super(params);
		this.type = type;		
	}
	
	/**
 	* Returns the result of zipping two lines.
	* @param line1 The first line to zip
 	* @param line2 The second line to zip	
 	* @return The result of zipping two lines
 	* @throws MixException - If an mixing error occurs
 	*/
	@Override
	public String process(String line1, String line2) throws MixException {
		String zip = null;
		String sep = DEFAULT_ZIP_SEPARATOR;
		if (this.params.containsKey(RuleParam._ZIP_SEP)) {
			sep = this.params.get(RuleParam._ZIP_SEP).asString();
		}		
		switch (this.type) {
			case _LINE:
				zip = line1 + sep + line2;
				break;
			case _CELL:					
				List<String> list1 = Arrays.asList(line1.split("\\s"));
				List<String> list2 = Arrays.asList(line2.split("\\s"));
				int index = 0;
				while (index < list1.size() && index < list2.size()) {						
					if (index == 0) {
						zip = "";
					} else {
						zip += " ";  // cell separator
					}
					zip += list1.get(index) + sep + list2.get(index);
					index++;
				}
		}
		return zip;
	}

}

@@ -1192,16 +1192,84 @@
 ption %7B%0A
+%09%09if (line1 == null %7C%7C line2 == null) %7B%0A%09%09%09return null;%0A%09%09%7D else %7B%0A%09
 %09%09String
@@ -1281,16 +1281,17 @@
 = null;%0A
+%09
 %09%09String
@@ -1322,16 +1322,17 @@
 ATOR;%0A%09%09
+%09
 if (this
@@ -1374,16 +1374,17 @@
 SEP)) %7B%0A
+%09
 %09%09%09sep =
@@ -1436,16 +1436,17 @@
 ();%0A
+%09
 %09%09%7D%09%09%0A%09%09
 swit
@@ -1441,16 +1441,17 @@
 %09%09%7D%09%09%0A%09%09
+%09
 switch (
@@ -1462,24 +1462,25 @@
 .type) %7B%0A%09%09%09
+%09
 case _LINE:%0A
@@ -1479,16 +1479,17 @@
  _LINE:%0A
+%09
 %09%09%09%09zip 
@@ -1519,15 +1519,17 @@
 %09%09%09%09
+%09
 break;%0A
+%09
 %09%09%09c
@@ -1540,24 +1540,25 @@
 _CELL:%09%09%09%09%09%0A
+%09
 %09%09%09%09List%3CStr
@@ -1605,24 +1605,25 @@
 %5C%5Cs%22));%0A%09%09%09%09
+%09
 List%3CString%3E
@@ -1670,16 +1670,17 @@
 ));%0A%09%09%09%09
+%09
 int inde
@@ -1686,16 +1686,17 @@
 ex = 0;%0A
+%09
 %09%09%09%09whil
@@ -1757,16 +1757,17 @@
 %09%09%0A%09%09%09%09%09
+%09
 if (inde
@@ -1778,24 +1778,25 @@
  0) %7B%0A%09%09%09%09%09%09
+%09
 zip = %22%22;%0A%09%09
@@ -1793,16 +1793,17 @@
 p = %22%22;%0A
+%09
 %09%09%09%09%09%7D e
@@ -1810,24 +1810,25 @@
 lse %7B%0A%09%09%09%09%09%09
+%09
 zip += %22 %22; 
@@ -1851,18 +1851,20 @@
 or%0A%09%09%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09%09%09zip
@@ -1915,16 +1915,17 @@
 );%0A%09%09%09%09%09
+%09
 index++;
@@ -1933,16 +1933,19 @@
 %09%09%09%09
+%09
 %7D%0A
+%09
 %09%09%7D%0A%09%09
+%09
 retu
@@ -1952,14 +1952,18 @@
 rn zip;%0A
+%09%09%7D%0A
 %09%7D%0A%0A%7D%0A

update zip operation