package net.iponweb.disthene.reader.graphite.functions;

import net.iponweb.disthene.reader.beans.TimeSeries;
import net.iponweb.disthene.reader.exceptions.EvaluationException;
import net.iponweb.disthene.reader.exceptions.InvalidArgumentException;
import net.iponweb.disthene.reader.exceptions.TimeSeriesNotAlignedException;
import net.iponweb.disthene.reader.graphite.Target;
import net.iponweb.disthene.reader.graphite.evaluation.TargetEvaluator;
import net.iponweb.disthene.reader.utils.TimeSeriesUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Andrei Ivanov
 */
public class AliasByNodeFunction extends DistheneFunction {

    public AliasByNodeFunction(String text) {
        super(text, "aliasByNode");
    }

    @Override
    public List<TimeSeries> evaluate(TargetEvaluator evaluator) throws EvaluationException {
        List<TimeSeries> processedArguments = new ArrayList<>();
        processedArguments.addAll(evaluator.eval((Target) arguments.get(0)));

        if (processedArguments.size() == 0) return new ArrayList<>();

        if (!TimeSeriesUtils.checkAlignment(processedArguments)) {
            throw new TimeSeriesNotAlignedException();
        }

        int node = ((Double) arguments.get(1)).intValue();

        for (TimeSeries ts : processedArguments) {
            String[] split = ts.getName().split("\\.");
            if (node >= 0 && node < split.length) {
                ts.setName(split[node]);
            }
        }

        return processedArguments;
    }

    @Override
    public void checkArguments() throws InvalidArgumentException {
        if (arguments.size() != 2) throw new InvalidArgumentException("aliasByNode: number of arguments is " + arguments.size() + ". Must be two.");
        if (!(arguments.get(0) instanceof Target)) throw new InvalidArgumentException("aliasByNode: argument is " + arguments.get(0).getClass().getName() + ". Must be series");
        if (!(arguments.get(1) instanceof Double)) throw new InvalidArgumentException("aliasByNode: argument is " + arguments.get(1).getClass().getName() + ". Must be a number");
    }
}

@@ -46,24 +46,62 @@
 functions;%0A%0A
+import com.google.common.base.Joiner;%0A
 import net.i
@@ -1241,21 +1241,135 @@
      int
+%5B%5D
  node
+s = new int%5Barguments.size() - 1%5D;%0A        for (int i = 1; i %3C arguments.size(); i++) %7B%0A            nodes%5Bi - 1%5D
  = ((Dou
@@ -1387,17 +1387,17 @@
 nts.get(
-1
+i
 )).intVa
@@ -1402,16 +1402,26 @@
 Value();
+%0A        %7D
 %0A%0A      
@@ -1517,24 +1517,123 @@
 lit(%22%5C%5C.%22);%0A
+            List%3CString%3E parts = new ArrayList%3CString%3E();%0A            for (int node : nodes) %7B%0A    
             
@@ -1692,18 +1692,21 @@
     
-ts.setName
+    parts.add
 (spl
@@ -1728,17 +1728,87 @@
         
+  
-%7D
+  %7D%0A            %7D%0A            ts.setName(Joiner.on(%22.%22).join(parts));
 %0A       
@@ -1967,10 +1967,9 @@
 e() 
-!=
+%3C
  2) 
@@ -2074,16 +2074,25 @@
 Must be 
+at least 
 two.%22);%0A
@@ -2264,24 +2264,81 @@
 e series%22);%0A
+        for (int i = 1; i %3C arguments.size(); i++) %7B%0A    
         if (
@@ -2353,17 +2353,17 @@
 nts.get(
-1
+i
 ) instan
@@ -2375,16 +2375,32 @@
 Double))
+%0A               
  throw n
@@ -2424,35 +2424,36 @@
 tException(%22
-alias
+group
 ByNode
+s
 : argument i
@@ -2442,32 +2442,42 @@
 yNodes: argument
+ %22 + i + %22
  is %22 + argument
@@ -2482,17 +2482,17 @@
 nts.get(
-1
+i
 ).getCla
@@ -2527,20 +2527,30 @@
 a number%22);%0A
+        %7D%0A
     %7D%0A%7D%0A

allow to pass variable number of nodes to use as alias