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

import de.craften.plugins.rpgplus.RpgPlus;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.OneArgFunction;
import org.luaj.vm2.lib.TwoArgFunction;

/**
 * Lua module for storage.
 */
public class Storage extends LuaTable {
    final de.craften.plugins.rpgplus.components.storage.Storage storage = RpgPlus.getPlugin(RpgPlus.class).getStorage();

    public Storage() {
        set("get", new TwoArgFunction() {
            @Override
            public LuaValue call(LuaValue key, LuaValue defaultValue) {
                return LuaValueConverter.convert(storage.get(key.checkjstring(), defaultValue.isnil() ? null : defaultValue.checkjstring()));
            }
        });

        set("set", new TwoArgFunction() {
            @Override
            public LuaValue call(LuaValue key, LuaValue value) {
                storage.set(key.checkjstring(), key.checkjstring());
                return LuaValue.NIL;
            }
        });

        set("of", new OneArgFunction() {
            @Override
            public LuaValue call(LuaValue player) {
                return new PlayerTable(Bukkit.getServer().getPlayer(player.checkjstring()));
            }
        });
    }

    private class PlayerTable extends LuaTable {
        public PlayerTable(final OfflinePlayer player) {
            set("get", new TwoArgFunction() {
                @Override
                public LuaValue call(LuaValue key, LuaValue defaultValue) {
                    return LuaValue.valueOf(storage.get(player, key.checkjstring(), defaultValue.checkjstring()));
                }
            });

            set("set", new TwoArgFunction() {
                @Override
                public LuaValue call(LuaValue key, LuaValue value) {
                    storage.set(player, key.checkjstring(), value.checkjstring());
                    return LuaValue.NIL;
                }
            });
        }
    }
}

@@ -295,16 +295,39 @@
 ction;%0A%0A
+import java.util.Map;%0A%0A
 /**%0A * L
@@ -964,32 +964,348 @@
 aValue value) %7B%0A
+                if (value.istable()) %7B%0A                    for (Map.Entry%3CString, String%3E entry : LuaValueConverter.convertFromTable(value.checktable()).entrySet()) %7B%0A                        storage.set(key.checkjstring() + %22.%22 + entry.getKey(), entry.getValue());%0A                    %7D%0A                %7D else %7B%0A    
                 
@@ -1349,32 +1349,50 @@
 heckjstring());%0A
+                %7D%0A
                 
@@ -2217,32 +2217,368 @@
 aValue value) %7B%0A
+                    if (value.istable()) %7B%0A                        for (Map.Entry%3CString, String%3E entry : LuaValueConverter.convertFromTable(value.checktable()).entrySet()) %7B%0A                            storage.set(key.checkjstring() + %22.%22 + entry.getKey(), entry.getValue());%0A                        %7D%0A                    %7D else %7B%0A    
                 
@@ -2636,32 +2636,54 @@
 heckjstring());%0A
+                    %7D%0A
                 

Allow saving tables into the storage (unpacks the values).