package innovimax.mixthem.operation;

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

import java.util.Map;

/**
* <p>Zips two lines on a common field.</p>
* @see ILineOperation
* @author Innovimax
* @version 1.0
*/
public class DefaultLineZipping implements ILineOperation {

	private final Map<RuleParam, ParamValue> params;
	
	/**
 	* @param params The list of parameters (maybe empty)
	* @see innovimax.mixthem.operation.RuleParam
	* @see innovimax.mixthem.operation.ParamValue
	*/
	public DefaultLineZipping(Map<RuleParam, ParamValue> params) {
		this.params = params;
	}
	
	/**
 	* 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;
		if (line1 != null && line2 != null) {      			
			MixThem.LOGGER.info("Params " + params.toString());									
		}
		return zip;
	}
	
	private class ZipTuple {
		
		private final char c1;
		private final char c2;
		private final String sep;
		
		private ZipTuple(char c1, char c2) {
			this(c1, c2, " ");
		}

		private ZipTuple(char c1, char c2, String sep) {
			this.c1 = c1;
			this.c2 = c2;
			this.sep = sep;
		}
		
		@Override
		public String toString() {
			return '(' + this.c1 + this.sep + this.c2 + ')';
		}
	}

}

@@ -387,16 +387,76 @@
 ration %7B
+%0A%09%0A%09private final static String DEFAULT_ZIP_SEPARATOR = %22 %22;
 %0A%0A%09priva
@@ -1093,96 +1093,499 @@
 ;%0A%09%09
-if (line1 != null && line2 != null) %7B      %09%09%09%0A%09%09%09MixThem.LOGGER.info(%22Params %22 + params
+String sep = DEFAULT_ZIP_SEPARATOR;%0A%09%09if (this.params.containsKey(RuleParam._ZIP_SEP)) %7B%0A%09%09%09sep = this.params.get(RuleParam._ZIP_SEP).asString();%0A%09%09%7D%09%09%0A%09%09if (line1 != null && line2 != null) %7B      %09%09%09%0A%09%09%09zip = %22%5B%22;%0A%09%09%09char%5B%5D buf1 = line1.toCharArray();%0A%09%09%09char%5B%5D buf2 = line2.toCharArray();%0A%09%09%09int index = 0;%0A%09%09%09while (index %3C buf1.length && index %3C buf2.length) %7B%0A%09%09%09%09if (index %3E 0) %7B%0A%09%09%09%09%09zip += sep;%0A%09%09%09%09%7D%0A%09%09%09%09ZipTuple tuple = new ZipTuple(buf1%5Bindex%5D, buf2%5Bindex%5D, sep);%0A%09%09%09%09zip += tuple
 .toS
@@ -1595,19 +1595,42 @@
 ng()
-)
 ;
+%0A
 %09%09%09%09
-%09%09%09%09%09
+index++;%0A%09%09%09%7D%0A%09%09%09zip += %22%5D%22;
 %0A%09%09%7D
@@ -1991,11 +1991,12 @@
 urn 
-'
+%22
 ('
+%22
  + t
@@ -2001,16 +2001,21 @@
  this.c1
+ +%22'%22
  + this.
@@ -2019,16 +2019,22 @@
 is.sep +
+ %22'%22 +
  this.c2
@@ -2040,11 +2040,12 @@
 2 + 
+%22
 ')
-'
+%22
 ;%0A%09%09

update process using tuples