package de.craften.plugins.rpgplus.scripting.api;

import de.craften.plugins.rpgplus.RpgPlus;
import de.craften.plugins.rpgplus.components.dialogs.AnswerHandler;
import de.craften.plugins.rpgplus.scripting.util.ScriptUtil;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.luaj.vm2.LuaFunction;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Varargs;
import org.luaj.vm2.lib.OneArgFunction;
import org.luaj.vm2.lib.VarArgFunction;
import org.luaj.vm2.lib.jse.CoerceLuaToJava;

import java.util.Random;

/**
 * Provides an API for dialogs.
 */
public class Dialogs extends LuaTable {
    private final Random random = new Random();

    public Dialogs(final RpgPlus plugin) {
        set("ask", new VarArgFunction() {
            @Override
            public Varargs invoke(Varargs varargs) {
                final Entity entity = (Entity) CoerceLuaToJava.coerce(varargs.arg(1), Entity.class);
                Player player = ScriptUtil.getPlayer(varargs.arg(2));
                final LuaFunction callback = varargs.checkfunction(4);
                plugin.getDialogs().ask(entity, player, varargs.checkjstring(3), new AnswerHandler() {
                    @Override
                    public void handleAnswer(final Player player, String answer) {
                        callback.invoke(LuaValue.valueOf(answer), new OneArgFunction() {
                            @Override
                            public LuaValue call(LuaValue message) {
                                plugin.getDialogs().tell(entity, player, message.checkjstring());
                                return LuaValue.NIL;
                            }
                        });
                    }
                });
                return LuaValue.NIL;
            }
        });

        set("tell", new VarArgFunction() {
            @Override
            public Varargs invoke(Varargs varargs) {
                final Entity entity = (Entity) CoerceLuaToJava.coerce(varargs.arg(1), Entity.class);
                Player player = ScriptUtil.getPlayer(varargs.arg(2));
                for (int i = 3; i <= varargs.narg(); i++) {
                    LuaValue message = varargs.arg(i);
                    if (message.istable()) {
                        message = message.checktable().get(random.nextInt(message.length()) + 1);
                    }
                    plugin.getDialogs().tell(entity, player, message.checkjstring());
                }

                return LuaValue.NIL;
            }
        });
    }
}

@@ -1147,31 +1147,43 @@
 er, 
-varargs.checkjstrin
+messageAlternatives(varargs.ar
 g(3)
+)
 , ne
@@ -1587,38 +1587,44 @@
 yer, message
-.checkjstring(
+Alternatives(message
 ));%0A        
@@ -2205,27 +2205,69 @@
     
-LuaValue message = 
+plugin.getDialogs().tell(entity, player, messageAlternatives(
 vara
@@ -2276,16 +2276,18 @@
 s.arg(i)
+))
 ;%0A      
@@ -2279,32 +2279,44 @@
 rg(i)));%0A       
+ 
+        %7D%0A%0A
              if 
@@ -2316,43 +2316,159 @@
     
-if (message.istable()) %7B%0A          
+   return LuaValue.NIL;%0A            %7D%0A        %7D);%0A    %7D%0A%0A    private String messageAlternatives(LuaValue messages) %7B%0A        if (messages.istable()) %7B%0A
     
@@ -2479,17 +2479,22 @@
     
- 
+return
  message
  = m
@@ -2493,18 +2493,9 @@
 sage
- = message
+s
 .che
@@ -2530,16 +2530,17 @@
 (message
+s
 .length(
@@ -2550,100 +2550,68 @@
 + 1)
-;%0A                    %7D%0A                    plugin.getDialogs().tell(entity, player,
+.checkjstring();%0A        %7D else %7B%0A            return
  message
 .che
@@ -2606,16 +2606,17 @@
  message
+s
 .checkjs
@@ -2622,17 +2622,16 @@
 string()
-)
 ;%0A      
@@ -2636,81 +2636,9 @@
     
-   
-     %7D%0A%0A                return LuaValue.NIL;%0A            %7D%0A        %7D);
+%7D
 %0A   

Add random message support for ask.