package innovimax.mixthem.operation;

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 {
	
	private final int col1;
	private final int col2;
	
	/**
 	* @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);
		this.col1 = this.params.containsKey(RuleParam.JOIN_COL1) ? this.params.get(RuleParam.JOIN_COL1).asInt() : JoinOperation.DEFAULT_JOIN_COLUMN.toInteger();
		this.col2 = this.params.containsKey(RuleParam.JOIN_COL2) ? this.params.get(RuleParam.JOIN_COL2).asInt() : JoinOperation.DEFAULT_JOIN_COLUMN.toInteger();
	}	

	@Override
	public void process(String line1, String line2, LineResult result) throws MixException {
		result.reset();
		if (line1 !=null && line2 != null) {
			List<String> list1 = Arrays.asList(line1.split(CellOperation.DEFAULT_SPLIT_CELL_REGEX.toString()));
			List<String> list2 = Arrays.asList(line2.split(CellOperation.DEFAULT_SPLIT_CELL_REGEX.toString()));		
			if (list1.size() >= this.col1 && list2.size() >= this.col2 && list1.get(this.col1 - 1).equals(list2.get(this.col2 - 1))) {
				String part1 = list1.get(this.col1 - 1);
				String part2 = list1.stream().filter(s -> !s.equals(part1)).collect(Collectors.joining(CellOperation.DEFAULT_CELL_SEPARATOR.toString()));
				String part3 = list2.stream().filter(s -> !s.equals(part1)).collect(Collectors.joining(CellOperation.DEFAULT_CELL_SEPARATOR.toString()));
				result.setResult(part1 + CellOperation.DEFAULT_CELL_SEPARATOR.toString()  + 
						 part2 + CellOperation.DEFAULT_CELL_SEPARATOR.toString() + part3);
			}
		}
		result.exploreBothFiles();  // TODO: preserve lines
	}

}

@@ -1183,16 +1183,17 @@
 line1 !=
+ 
 null && 
@@ -1420,20 +1420,31 @@
 );%09%09%0A%09%09%09
-if (
+String cell1 = 
 list1.si
@@ -1461,18 +1461,73 @@
 is.col1 
-&&
+? list1.get(this.col1 - 1) : null;%0A      %09%09String cell2 =
  list2.s
@@ -1545,18 +1545,17 @@
 is.col2 
-&&
+?
  list1.g
@@ -1569,51 +1569,117 @@
 .col
-1
+2
  - 1)
-.equals(list2.get(this.col2 - 1))) %7B%0A
+ : null;%0A%09%09%09if (cell1 != null && cell2 != null) %7B%0A%09%09%09%09switch (cell1.compareTo(cell2)) %7B%0A%09%09%09%09%09case 0:%0A%09%09
 %09%09%09%09
@@ -1719,16 +1719,18 @@
 1 - 1);%0A
+%09%09
 %09%09%09%09Stri
@@ -1863,24 +1863,26 @@
 ng()));%0A%09%09%09%09
+%09%09
 String part3
@@ -2011,16 +2011,18 @@
 ));%0A%09%09%09%09
+%09%09
 result.s
@@ -2090,16 +2090,18 @@
 g()  + %0A
+%09%09
 %09%09%09%09%09%09 p
@@ -2172,14 +2172,9 @@
 %0A%09%09%09
-%7D%0A%09%09%7D%0A
+%09
 %09%09re
@@ -2201,33 +2201,145 @@
 s();
-  // TODO: preserve lines
+%0A%09%09%09%09%09%09break;%0A%09%09%09%09%09case 1:%0A%09%09%09%09%09%09result.preserveFirstLine();%0A%09%09%09%09%09%09break;%0A%09%09%09%09%09default:%0A%09%09%09%09%09%09result.preserveSecondLine();%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D
 %0A%09%7D%0A

Update DefaultLineJoining.java