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;

/**
* <p>Zips two characters.</p>
* @see CharOperation
* @author Innovimax
* @version 1.0
*/
public class DefaultCharZipping extends AbstractCharOperation {
	
	private final String sep;

	/**
	* Constructor
 	* @param params The list of parameters (maybe empty)
	* @see innovimax.mixthem.arguments.RuleParam
	* @see innovimax.mixthem.arguments.ParamValue
	*/
	public DefaultCharZipping(Map<RuleParam, ParamValue> params) {
		super(params);		
		this.sep = params.getOrDefault(RuleParam.ZIP_SEP, ZipOperation.DEFAULT_ZIP_SEPARATOR.getValue()).asString();
	}
	
	@Override
	public void process(final int c1, final int c2, final CharResult result) throws MixException {
		result.reset();
		final int len = (c1 != -1 ? 1 : 0) + sep.length() + (c2 != -1 ? 1 : 0);
        	final int[] array = new int[len];
		if (c1 != -1) { 
			array[0] = c1; 
		}		
        	for (int i = 0; i < sep.length(); i++) {			
			array[i + 1] = (int) sep.charAt(i);
		}		
		if (c2 != -1) { 
			array[array.length - 1] = c2; 
		}        	
		result.setResult(Arrays.stream(array));
	}

	@Override
	public void process(final int[] charRange, final CharResult result) throws MixException {
		result.reset();
		int len = 0;
		for (int i=0; i < charRange.length; i++) {			
			if (charRange[i] != -1) {
				len++;
			}
		}
		len += (charRange.length - 1) * sep.length();
		final int[] array = new int[len];
		int index = 0;
		for (int i=0; i < charRange.length; i++) {			
			if (charRange[i] != -1) {
				array[index++] = charRange[i];
			} 
			if (index < array.length) {
        			for (int j = 0; j < sep.length(); j++) {
					array[index++] = (int) sep.charAt(j);
				}				
			}
		}
		result.setResult(Arrays.stream(array));
	}
	
}

@@ -1392,19 +1392,30 @@
 ;%0A%09%09
-int 
+boolean zipab
 le
-n
  = 
-0
+true
 ;%0A%09%09
@@ -1472,33 +1472,33 @@
 f (charRange%5Bi%5D 
-!
+=
 = -1) %7B%0A%09%09%09%09len+
@@ -1497,32 +1497,91 @@
 %09%09%09%09
-len++;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09len +=
+zipable = false;%0A%09%09%09%7D%0A%09%09%7D%0A%09%09if (zipable) %7B%09%09%09%0A%09%09%09final int len = charRange.length +
  (ch
@@ -1612,24 +1612,25 @@
 p.length();%0A
+%09
 %09%09final int%5B
@@ -1655,16 +1655,17 @@
 len%5D;%0A%09%09
+%09
 int inde
@@ -1671,16 +1671,17 @@
 ex = 0;%0A
+%09
 %09%09for (i
@@ -1720,40 +1720,8 @@
 +) %7B
-%09%09%09%0A%09%09%09if (charRange%5Bi%5D != -1) %7B
 %0A%09%09%09
@@ -1755,15 +1755,14 @@
 %5Bi%5D;
-%0A
+%09
 %09%09%09
-%7D 
 %0A
+%09
 %09%09%09i
@@ -1799,16 +1799,17 @@
      %09%09%09
+%09
 for (int
@@ -1842,24 +1842,25 @@
 j++) %7B%0A%09%09%09%09%09
+%09
 array%5Bindex+
@@ -1885,16 +1885,17 @@
 rAt(j);%0A
+%09
 %09%09%09%09%7D%09%09%09
@@ -1899,22 +1899,25 @@
 %09%09%09%09%0A%09%09%09
+%09
 %7D%0A
+%09
 %09%09%7D%0A
+%09
 %09%09result
@@ -1950,15 +1950,19 @@
 rray));%0A
+%09%09%7D%0A
 %09%7D%0A%09%0A%7D%0A

Update DefaultCharZipping.java