/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2023  SteamWar.de-Serverteam
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package de.steamwar.bausystem.features.script.lua;

import de.steamwar.bausystem.BauSystem;
import org.bukkit.entity.Player;
import org.luaj.vm2.Globals;
import org.luaj.vm2.LoadState;
import org.luaj.vm2.LuaError;
import org.luaj.vm2.LuaFunction;
import org.luaj.vm2.compiler.LuaC;
import org.luaj.vm2.lib.Bit32Lib;
import org.luaj.vm2.lib.PackageLib;
import org.luaj.vm2.lib.StringLib;
import org.luaj.vm2.lib.TableLib;
import org.luaj.vm2.lib.jse.JseBaseLib;
import org.luaj.vm2.lib.jse.JseMathLib;

import java.util.function.BiConsumer;
import java.util.function.Consumer;

public class SteamWarPlatform {

    public static Globals createClickGlobals(Player player) {
        Globals globals = new Globals();
        globals.load(new JseBaseLib());
        globals.load(new PackageLib());
        globals.load(new JseMathLib());
        globals.load(new TableLib());
        globals.load(new Bit32Lib());
        globals.load(new StringLib());
        globals.load(new SteamWarLuaPlugin(player));

        globals.load(new SteamWarGlobalLuaPlugin((eventType, luaFunction) -> {
            throw new LuaError(BauSystem.MESSAGE.parse("SCRIPT_ERROR_ONLY_IN_GLOBAL", player));
        }, (s, luaFunction) -> {
            throw new LuaError(BauSystem.MESSAGE.parse("SCRIPT_ERROR_ONLY_IN_GLOBAL", player));
        }, commandRegister -> {
            throw new LuaError(BauSystem.MESSAGE.parse("SCRIPT_ERROR_ONLY_IN_GLOBAL", player));
        }));

        LoadState.install(globals);
        LuaC.install(globals);
        return globals;
    }

    public static Globals createGlobalGlobals(Player player, BiConsumer<SteamWarGlobalLuaPlugin.EventType, LuaFunction> eventConsumer, BiConsumer<String, LuaFunction> hotkeyConsumer, Consumer<CommandRegister> commandConsumer) {
        Globals globals = createClickGlobals(player);
        globals.load(new SteamWarGlobalLuaPlugin(eventConsumer, hotkeyConsumer, commandConsumer));
        return globals;
    }

    public static Globals createGlobalParser(BiConsumer<SteamWarGlobalLuaPlugin.EventType, LuaFunction> eventConsumer, BiConsumer<String, LuaFunction> hotkeyConsumer, Consumer<CommandRegister> commandConsumer) {
        Globals globals = new Globals();
        globals.load(new SteamWarGlobalLuaPlugin(eventConsumer, hotkeyConsumer, commandConsumer));
        LoadState.install(globals);
        LuaC.install(globals);
        return globals;
    }
}
