package innovimax.mixthem.operation;

import static innovimax.mixthem.MixConstants.*;
import innovimax.mixthem.MixException;
import innovimax.mixthem.arguments.ParamValue;
import innovimax.mixthem.arguments.RuleParam;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* <p>Joins two lines on a common field.</p>
* @see ILineOperation
* @author Innovimax
* @version 1.0
*/
public class DefaultLineJoining extends AbstractLineOperation {
	
	/**
 	* @param params The list of parameters (maybe empty)
	* @see innovimax.mixthem.operation.RuleParam
	* @see innovimax.mixthem.operation.ParamValue
	*/
	public DefaultLineJoining(Map<RuleParam, ParamValue> params) {
		super(params);
	}	

	@Override
	public String process(String line1, String line2) throws MixException {
		String join = null;
		if (line1 !=null && line2 != null) {
			int col1 = 1;
			int col2 = 1;
			if (this.params.containsKey(RuleParam._JOIN_COL1)) {
				col1 = this.params.get(RuleParam._JOIN_COL1).asInt();
				col2 = col1;
			}
			if (this.params.containsKey(RuleParam._JOIN_COL2)) {
				col2 = this.params.get(RuleParam._JOIN_COL2).asInt();			
			}
			List<String> list1 = Arrays.asList(line1.split(DEFAULT_SPLIT_CELL_REGEX));
			List<String> list2 = Arrays.asList(line2.split(DEFAULT_SPLIT_CELL_REGEX));		
			if (list1.size() >= col1 && list2.size() >= col2 && list1.get(col1 - 1).equals(list2.get(col2 - 1))) {
				String part1 = list1.get(col1 - 1);
				String part2 = list1.stream().filter(s -> !s.equals(part1)).collect(Collectors.joining(DEFAULT_CELL_SEPARATOR));
				String part3 = list2.stream().filter(s -> !s.equals(part1)).collect(Collectors.joining(DEFAULT_CELL_SEPARATOR));
				join = part1 + DEFAULT_CELL_SEPARATOR  + part2 + DEFAULT_CELL_SEPARATOR + part3;				
			}
		}
		return join;
	}

}

@@ -493,16 +493,68 @@
 ion %7B%0A%09%0A
+%09private final int col1;%0A%09private final int col2;%0A%09%0A
 %09/**%0A %09*
@@ -788,198 +788,21 @@
 );%0A%09
-%7D%09%0A%0A%09@Override%0A%09public String process(String line1, String line2) throws MixException %7B%0A%09%09String join = null;%0A%09%09if (line1 !=null && line2 != null) %7B%0A%09%09%09int col1 = 1;%0A%09%09%09int col2 = 1;%0A%09%09%09if (
+%09this.col1 = 
 this
@@ -846,22 +846,10 @@
 OL1)
-) %7B%0A%09%09%09%09col1 =
+ ?
  thi
@@ -894,39 +894,28 @@
 nt()
-;%0A%09%09%09%09col2 = col1;%0A%09%09%09%7D%0A%09%09%09if (
+ : 1;%0A%09%09this.col2 = 
 this
@@ -959,22 +959,10 @@
 OL2)
-) %7B%0A%09%09%09%09col2 =
+ ?
  thi
@@ -1007,17 +1007,173 @@
 nt()
-;%09%09%09%0A%09%09%09%7D
+ : this.col1;%09%09%0A%09%7D%09%0A%0A%09@Override%0A%09public String process(String line1, String line2) throws MixException %7B%0A%09%09String join = null;%0A%09%09if (line1 !=null && line2 != null) %7B
 %0A%09%09%09
@@ -1350,16 +1350,21 @@
 ze() %3E= 
+this.
 col1 && 
@@ -1379,16 +1379,21 @@
 ze() %3E= 
+this.
 col2 && 
@@ -1394,32 +1394,37 @@
 l2 && list1.get(
+this.
 col1 - 1).equals
@@ -1434,16 +1434,21 @@
 st2.get(
+this.
 col2 - 1
@@ -1482,16 +1482,21 @@
 st1.get(
+this.
 col1 - 1

move params setting to constructor