package innovimax.mixthem.operation;

import innovimax.mixthem.MixException;
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 static String DEFAULT_ZIP_SEPARATOR = " ";

	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;
		String sep = DEFAULT_ZIP_SEPARATOR;
		if (this.params.containsKey(RuleParam._ZIP_SEP)) {
			sep = this.params.get(RuleParam._ZIP_SEP).asString();
		}		
		if (line1 != null && line2 != null) {      			
			zip = "[";
			char[] buf1 = line1.toCharArray();
			char[] buf2 = line2.toCharArray();
			int index = 0;
			while (index < buf1.length && index < buf2.length) {
				if (index > 0) {
					zip += sep;
				}
				ZipTuple tuple = new ZipTuple(buf1[index], buf2[index], sep);
				zip += tuple.toString();
				index++;
			}
			zip += "]";
		}
		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 + "')";
		}
	}

}

@@ -410,17 +410,16 @@
 ATOR = %22
- 
 %22;%0A%0A%09pri
@@ -1268,754 +1268,46 @@
 p = 
-%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
+line1
  +
-=
  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.toString();%0A%09%09%09%09index++;%0A%09%09%09%7D%0A%09%09%09zip += %22%5D%22;%0A%09%09%7D%0A%09%09return zip;%0A%09%7D%0A%09%0A%09private class ZipTuple %7B%0A%09%09%0A%09%09private final char c1;%0A%09%09private final char c2;%0A%09%09private final String sep;%0A%09%09%0A%09%09private ZipTuple(char c1, char c2) %7B%0A%09%09%09this(c1, c2, %22 %22);%0A%09%09%7D%0A%0A%09%09private ZipTuple(char c1, char c2, String sep) %7B%0A%09%09%09this.c1 = c1;%0A%09%09%09this.c2 = c2;%0A%09%09%09this.sep = sep;%0A%09%09%7D%0A%09%09%0A%09%09@Override%0A%09%09public String toString() %7B%0A%09%09%09return %22('%22 + this.c1 +%22'%22 + this.sep + %22'%22 + this.c2 + %22')%22;%0A%09%09%7D
+ + line2;%0A%09%09%7D%0A%09%09return zip;
 %0A%09%7D%0A

update zip-line process