package me.km.snuviscript.commands;

import me.hammerle.snuviscript.code.ScriptManager;
import me.km.databank.DataBank;
import me.km.events.CustomEventCaller;
import me.km.permissions.ModCommandManager;
import me.km.permissions.Permissions;
import me.km.playerbank.IPlayerBank;
import me.km.plots.WorldPlotMap;
import me.km.scheduler.SnuviScheduler;
import me.km.snuviscript.IScriptBank;
import me.km.snuviscript.Scripts;
import static me.km.snuviscript.commands.CommandUtils.getId;
import net.minecraft.server.MinecraftServer;

public class DeprecatedCommands {
    public static void registerFunctions(ScriptManager sm, Scripts scripts, Permissions perms, SnuviScheduler scheduler, 
            MinecraftServer server, IPlayerBank playerBank, CustomEventCaller cec, IScriptBank scriptBank, 
            DataBank dataBank, WorldPlotMap plots, ModCommandManager commands) {
        sm.registerFunction("getglobalvar", (sc, in) -> {
            Object o = in[0].get(sc);
            if(in.length == 2) {
                return scriptBank.getVar(in[1].getString(sc), getId(playerBank, o));
            }
            return scriptBank.getVar(in[1].getString(sc), getId(playerBank, o), in[2].get(sc));
        });
        sm.registerAlias("getglobalvar", "ggv");
        sm.registerConsumer("setglobalvar", (sc, in) -> {
            final String value = in[2].getString(sc);
            final String var = in[1].getString(sc);
            final int id = getId(playerBank, in[0].get(sc));
            scheduler.scheduleAsyncTask(() -> {
                try {
                    scriptBank.setVar(value, var, id);
                } catch(Exception ex) {
                    scheduler.scheduleTask(() -> {
                        sc.getScriptManager().getLogger().print("Worker error", ex, null, sc.getName(), sc, null);
                    });
                }
            });
        });
        sm.registerAlias("setglobalvar", "sgv");
        sm.registerConsumer("delglobalvar", (sc, in) -> {
            final String var = in[1].getString(sc);
            final int id = getId(playerBank, in[0].get(sc));
            scheduler.scheduleAsyncTask(() -> {
                try {
                    scriptBank.deleteVar(var, id);
                } catch(Exception ex) {
                    scheduler.scheduleTask(() -> {
                        sc.getScriptManager().getLogger().print("Worker error", ex, null, sc.getName(), sc, null);
                    });
                }
            });
        });
        sm.registerAlias("delglobalvar", "dgv");
        sm.registerConsumer("gmap.removeall", (sc, in) -> {
            final String map = in[0].getString(sc);
            scheduler.scheduleAsyncTask(() -> {
                try {
                    scriptBank.removeMap(map);
                } catch(Exception ex) {
                    scheduler.scheduleTask(() -> {
                        sc.getScriptManager().getLogger().print("Worker error", ex, null, sc.getName(), sc, null);
                    });
                }
            });
        });
        sm.registerConsumer("gmap.add", (sc, in) -> {
            final String map = in[0].getString(sc);
            final String key = in[1].getString(sc);
            final String value = in[2].getString(sc);
            scheduler.scheduleAsyncTask(() -> {
                try {
                    scriptBank.addMapElement(map, key, value);
                } catch(Exception ex) {
                    scheduler.scheduleTask(() -> {
                        sc.getScriptManager().getLogger().print("Worker error", ex, null, sc.getName(), sc, null);
                    });
                }
            });
        });
        sm.registerConsumer("gmap.remove", (sc, in) -> {
            final String map = in[0].getString(sc);
            final String key = in[1].getString(sc);
            scheduler.scheduleAsyncTask(() -> {
                try {
                    scriptBank.removeMapElement(map, key);
                } catch(Exception ex) {
                    scheduler.scheduleTask(() -> {
                        sc.getScriptManager().getLogger().print("Worker error", ex, null, sc.getName(), sc, null);
                    });
                }
            });
        });
        sm.registerFunction("gmap.get", (sc, in) -> scriptBank.getMapValue(in[0].getString(sc), in[1].getString(sc)));
        sm.registerFunction("gmap.getordefault", (sc, in) -> {
            Object o = scriptBank.getMapValue(in[0].getString(sc), in[1].getString(sc));
            if(o == null) {
                return in[2].get(sc);
            }
            return o;
        });
        sm.registerConsumer("gdmap.removeall", (sc, in) -> {
            if(in.length >= 2) {
                final String map = in[0].getString(sc);
                final String key = in[1].getString(sc);
                scheduler.scheduleAsyncTask(() -> {
                    try {
                        scriptBank.removeDualMapElement(map, key);
                    } catch(Exception ex) {
                        scheduler.scheduleTask(() -> {
                            sc.getScriptManager().getLogger().print("Worker error", ex, null, sc.getName(), sc, null);
                        });
                    }
                });
            } else {
                final String map = in[0].getString(sc);
                scheduler.scheduleAsyncTask(() -> {
                    try {
                        scriptBank.removeDualMap(map);
                    } catch(Exception ex) {
                        scheduler.scheduleTask(() -> {
                            sc.getScriptManager().getLogger().print("Worker error", ex, null, sc.getName(), sc, null);
                        });
                    }
                });
            }
        });
        sm.registerConsumer("gdmap.add", (sc, in) -> {
            final String map = in[0].getString(sc);
            final String key1 = in[1].getString(sc);
            final String key2 = in[2].getString(sc);
            final String value = in[3].getString(sc);
            scheduler.scheduleAsyncTask(() -> {
                try {
                    scriptBank.addDualMapElement(map, key1, key2, value);
                } catch(Exception ex) {
                    scheduler.scheduleTask(() -> {
                        sc.getScriptManager().getLogger().print("Worker error", ex, null, sc.getName(), sc, null);
                    });
                }
            });
        });
        sm.registerConsumer("gdmap.remove", (sc, in) -> {
            final String map = in[0].getString(sc);
            final String key1 = in[1].getString(sc);
            final String key2 = in[2].getString(sc);
            scheduler.scheduleAsyncTask(() -> {
                try {
                    scriptBank.removeDualMapElement(map, key1, key2);
                } catch(Exception ex) {
                    scheduler.scheduleTask(() -> {
                        sc.getScriptManager().getLogger().print("Worker error", ex, null, sc.getName(), sc, null);
                    });
                }
            });
        });
        sm.registerFunction("gdmap.get", (sc, in) -> {
            return scriptBank.getDualMapValue(in[0].getString(sc), in[1].getString(sc), in[2].getString(sc));
        });
        sm.registerFunction("gdmap.getordefault", (sc, in) -> {
            Object o = scriptBank.getDualMapValue(in[0].getString(sc), in[1].getString(sc), in[2].getString(sc));
            if(o == null) {
                return in[3].get(sc);
            }
            return o;
        });
    }
}
