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 or null if none.
	* @param line1 The first line to join
 	* @param line2 The second line to join
 	* @return The result of joining two lines regarding the parameters or null if none
 	* @throws MixException - If an mixing error occurs
 	*/
	@Override
	public String process(String line1, String line2) throws MixException, ProcessException {
		if (line1 == null || line2 == null) {
			return null;
		} else {
			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;
		}
	}

}

@@ -692,322 +692,8 @@
 %7D%09%0A%0A
-%09/**%0A %09* Returns the result of joining two lines regarding the parameters or null if none.%0A%09* @param line1 The first line to join%0A %09* @param line2 The second line to join%0A %09* @return The result of joining two lines regarding the parameters or null if none%0A %09* @throws MixException - If an mixing error occurs%0A %09*/%0A
 %09@Ov
@@ -837,35 +837,44 @@
 %0A%09%09%09
-return null;%0A%09%09%7D else %7B
+throw new ProcessException();%0A%09%09%7D
 %0A%09%09
-%09
 Stri
@@ -887,25 +887,24 @@
 n = null;%0A%09%09
-%09
 int col1 = 1
@@ -905,17 +905,16 @@
 l1 = 1;%0A
-%09
 %09%09int co
@@ -923,17 +923,16 @@
  = 1;%0A%09%09
-%09
 if (this
@@ -972,25 +972,24 @@
 IN_COL1)) %7B%0A
-%09
 %09%09%09col1 = th
@@ -1033,17 +1033,16 @@
 sInt();%0A
-%09
 %09%09%09col2 
@@ -1051,20 +1051,18 @@
 col1;%0A%09%09
-%09
 %7D%0A
-%09
 %09%09if (th
@@ -1108,17 +1108,16 @@
 OL2)) %7B%0A
-%09
 %09%09%09col2 
@@ -1170,20 +1170,18 @@
 );%09%09%09%0A%09%09
-%09
 %7D%0A
-%09
 %09%09List%3CS
@@ -1230,17 +1230,16 @@
 %5C%5Cs%22));%0A
-%09
 %09%09List%3CS
@@ -1292,17 +1292,16 @@
 ));%09%09%0A%09%09
-%09
 if (list
@@ -1394,25 +1394,24 @@
 - 1))) %7B%0A%09%09%09
-%09
 String part1
@@ -1434,17 +1434,16 @@
 1 - 1);%0A
-%09
 %09%09%09Strin
@@ -1534,17 +1534,16 @@
 %22));%0A%09%09%09
-%09
 String p
@@ -1631,17 +1631,16 @@
 %22));%0A%09%09%09
-%09
 join = p
@@ -1681,17 +1681,16 @@
 %09%09%09%0A
-%09
 %09%09%7D%09%09%0A%09%09
 %09ret
@@ -1685,17 +1685,16 @@
 %09%09%7D%09%09%0A%09%09
-%09
 return j
@@ -1698,18 +1698,14 @@
 n join;%0A
-%09%09%7D%0A
 %09%7D%0A%0A%7D%0A

update join operation