package de.slikey.effectlib.math;

import net.objecthunter.exp4j.Expression;
import net.objecthunter.exp4j.ExpressionBuilder;
import net.objecthunter.exp4j.function.Function;
import org.bukkit.configuration.ConfigurationSection;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.logging.Level;

public class EquationTransform implements Transform {
    private Expression expression;
    private static Function randFunction;
    private final Set<String> inputVariables;
    private boolean quiet;
    private Exception exception;

    @Override
    public void load(ConfigurationSection parameters) {
        setEquation(parameters.getString("equation", ""));
    }

    public EquationTransform() {
        inputVariables = new HashSet<String>();
    }
    
    public EquationTransform(String equation) {
        this(equation, "t");
    }

    public EquationTransform(String equation, String inputVariable) {
        inputVariables = new HashSet<String>();
        inputVariables.add(inputVariable);
        setEquation(equation);
    }

    public EquationTransform(String equation, String... inputVariables) {
        this.inputVariables = new HashSet<String>();
        for (String inputVariable : inputVariables) {
            this.inputVariables.add(inputVariable);
        }
        setEquation(equation);
    }

    public EquationTransform(String equation, Set<String> inputVariables) {
        this.inputVariables = inputVariables;
        setEquation(equation);
    }

    public EquationTransform(String equation, Set<String> inputVariables, boolean quiet) {
        this.quiet = quiet;
        this.inputVariables = inputVariables;
        setEquation(equation);
    }

    public boolean setEquation(String equation) {
        try {
            exception = null;
            if (randFunction == null) {
                randFunction = new Function("rand", 2) {
                    private Random random = new Random();
                    
                    @Override
                    public double apply(double... args) {
                        return random.nextDouble() * (args[1] - args[0]) + args[0];
                    }
                };
            }
            expression = new ExpressionBuilder(equation)
                .function(randFunction)
                .variables(inputVariables)
                .build();
        } catch (Exception ex) {
            expression = null;
            exception = ex;
            if (!quiet) {
                org.bukkit.Bukkit.getLogger().log(Level.WARNING, ex.getMessage()); 
            }
        }
        
        return exception == null;
    }

    @Override
    public double get(double t) {
        if (expression == null) {
            return 0;
        }
        for (String inputVariable : inputVariables) {
            expression.setVariable(inputVariable, t);
        }
        return get();
    }
    
    public double get(double... t) {
        if (expression == null) {
            return 0;
        }
        int index = 0;
        for (String inputVariable : inputVariables) {
            expression.setVariable(inputVariable, t[index]);
            if (index < t.length - 1) index++;
        }
        return get();
    }
    
    public void addVariable(String key) {
        inputVariables.add(key);
    }

    public void setVariable(String key, double value) {
        if (expression != null) {
            expression.setVariable(key, value);
        }
    }

    public double get() {
        if (expression == null) {
            return Double.NaN;
        }
        double value = Double.NaN;
        try {
            exception = null;
            value = expression.evaluate();
        } catch (Exception ex) {
            exception = ex;
            if (!quiet) {
                org.bukkit.Bukkit.getLogger().log(Level.WARNING, ex.getMessage());
            }
        }
        return value;
    }
    
    public void setQuiet(boolean quiet) {
        this.quiet = quiet;
    }
    
    public Exception getException() {
        return exception;
    }
}

@@ -4095,10 +4095,93 @@
 ;%0A    %7D%0A
+    %0A    public Set%3CString%3E getParameters() %7B%0A        return inputVariables;%0A    %7D%0A
 %7D%0A

Add EquationTransform.getParameters accessor