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 {
	
	/**
 	* @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);
	}	

	/**
 	* Returns the result of joining two lines regarding the parameters.
	* @param line1 The first line to join
 	* @param line2 The second line to join
 	* @return The result of joining two lines regarding the parameters
 	* @throws MixException - If an mixing error occurs
 	*/
	@Override
	public String process(String line1, String line2) throws MixException {
		String join = 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("\\s"));
		List<String> list2 = Arrays.asList(line2.split("\\s"));		
		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(" "));
			String part3 = list2.stream().filter(s -> !s.equals(part1)).collect(Collectors.joining(" "));
			join = part1 + " " + part2 + " " + part3;				
		}		
		return join;
	}

}

@@ -765,10 +765,26 @@
 ters
+ or null if none
 .%0A
-
 %09* @
@@ -927,16 +927,32 @@
 rameters
+ or null if none
 %0A %09* @th
@@ -1086,16 +1086,84 @@
 ption %7B%0A
+%09%09if (line1 == null %7C%7C line2 == null) %7B%0A%09%09%09return null;%0A%09%09%7D else %7B%0A%09
 %09%09String
@@ -1174,24 +1174,25 @@
 n = null;%0A%09%09
+%09
 int col1 = 1
@@ -1193,16 +1193,17 @@
 l1 = 1;%0A
+%09
 %09%09int co
@@ -1210,16 +1210,17 @@
 l2 = 1;%0A
+%09
 %09%09if (th
@@ -1269,16 +1269,17 @@
 )) %7B%0A%09%09%09
+%09
 col1 = t
@@ -1323,24 +1323,25 @@
 asInt();%0A%09%09%09
+%09
 col2 = col1;
@@ -1341,22 +1341,24 @@
 = col1;%0A
+%09
 %09%09%7D%0A%09%09
+%09
 if (this
@@ -1402,16 +1402,17 @@
 OL2)) %7B%0A
+%09
 %09%09%09col2 
@@ -1463,22 +1463,24 @@
 t();%09%09%09%0A
+%09
 %09%09%7D%0A%09%09
+%09
 List%3CStr
@@ -1527,16 +1527,17 @@
 %5C%5Cs%22));%0A
+%09
 %09%09List%3CS
@@ -1590,16 +1590,17 @@
 ));%09%09%0A%09%09
+%09
 if (list
@@ -1693,24 +1693,25 @@
 - 1))) %7B%0A%09%09%09
+%09
 String part1
@@ -1734,16 +1734,17 @@
 1 - 1);%0A
+%09
 %09%09%09Strin
@@ -1831,24 +1831,25 @@
 g(%22 %22));%0A%09%09%09
+%09
 String part3
@@ -1933,16 +1933,17 @@
 %22));%0A%09%09%09
+%09
 join = p
@@ -1984,16 +1984,17 @@
 %09%09%09%0A
+%09
 %09%09%7D%09%09%0A%09%09
 retu
@@ -1989,16 +1989,17 @@
 %09%09%7D%09%09%0A%09%09
+%09
 return j
@@ -2003,14 +2003,18 @@
 n join;%0A
+%09%09%7D%0A
 %09%7D%0A%0A%7D%0A

update join operation