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.ThreeArgFunction;
import org.luaj.vm2.lib.VarArgFunction;
import org.luaj.vm2.lib.jse.CoerceLuaToJava;

/**
 * Provides an API for dialogs.
 */
public class Dialogs extends LuaTable {

    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 ThreeArgFunction() {
            @Override
            public LuaValue call(LuaValue luaEntity, LuaValue luaPlayer, LuaValue message) {
                Entity entity = (Entity) CoerceLuaToJava.coerce(luaEntity, Entity.class);
                Player player = ScriptUtil.getPlayer(luaPlayer);
                plugin.getDialogs().tell(entity, player, message.checkjstring());
                return LuaValue.NIL;
            }
        });
    }
}

@@ -448,50 +448,8 @@
 on;%0A
-import org.luaj.vm2.lib.ThreeArgFunction;%0A
 impo
@@ -476,32 +476,32 @@
 VarArgFunction;%0A
+
 import org.luaj.
@@ -530,16 +530,42 @@
 oJava;%0A%0A
+import java.util.Random;%0A%0A
 /**%0A * P
@@ -596,16 +596,16 @@
 gs.%0A */%0A
-
 public c
@@ -635,16 +635,64 @@
 aTable %7B
+%0A    private final Random random = new Random();
 %0A%0A    pu
@@ -1842,13 +1842,11 @@
 new 
-Three
+Var
 ArgF
@@ -1902,78 +1902,38 @@
 lic 
-LuaValue call(LuaValue luaEntity, LuaValue luaPlayer, LuaValue message
+Varargs invoke(Varargs varargs
 ) %7B%0A
@@ -1948,16 +1948,22 @@
         
+final 
 Entity e
@@ -2006,17 +2006,22 @@
 rce(
-luaEntity
+varargs.arg(1)
 , En
@@ -2090,20 +2090,309 @@
 yer(
-luaPlayer);%0A
+varargs.arg(2));%0A                for (int i = 3; i %3C= varargs.narg(); i++) %7B%0A                    LuaValue message = varargs.arg(i);%0A                    if (message.istable()) %7B%0A                        message = message.checktable().get(random.nextInt(message.length()) + 1);%0A                    %7D%0A    
     
@@ -2461,32 +2461,51 @@
 heckjstring());%0A
+                %7D%0A%0A
                 

Allow sending random messages and multiple messages with tell.