/*
 *   This file is a part of the SteamWar software.
 *
 *   Copyright (C) 2025 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.libs;

import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.features.loader.LoaderRecorder;
import de.steamwar.bausystem.features.tpslimit.TPSUtils;
import de.steamwar.inventory.SWItem;
import de.steamwar.linkage.Linked;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.luaj.vm2.LuaString;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.OneArgFunction;
import org.luaj.vm2.lib.TwoArgFunction;

import java.text.SimpleDateFormat;
import java.util.Calendar;

@Linked
public class ServerLib implements LuaLib {
    @Override
    public String name() {
        return "server";
    }

    @Override
    public LuaTable get(Player player) {
        LuaTable serverLib = LuaValue.tableOf();
        serverLib.set("time", getter(() -> new SimpleDateFormat(BauSystem.MESSAGE.parse("TIME", player)).format(Calendar.getInstance().getTime())));
        serverLib.set("onlinePlayerCount", getter(Bukkit.getOnlinePlayers()::size));
        serverLib.set("ticks", getter(TPSUtils.currentTick));
        serverLib.set("getBlockAt", new OneArgFunction() {
            @Override
            public LuaValue call(LuaValue arg1) {
                if (!Permission.SUPERVISOR.hasPermission(player)) {
                    return LuaValue.NIL;
                }
                LuaTable pos = arg1.checktable();
                return valueOf(player.getWorld().getBlockAt(pos.get("x").checkint(), pos.get("y").checkint(), pos.get("z").checkint()).getType().name());
            }
        });
        serverLib.set("setBlockAt", new TwoArgFunction() {
            @Override
            public LuaValue call(LuaValue arg1, LuaValue arg2) {
                if (!Permission.SUPERVISOR.hasPermission(player)) {
                    return LuaValue.NIL;
                }
                LuaTable pos = arg1.checktable();
                LuaString material = arg2.checkstring();
                Material mat = SWItem.getMaterial(material.tojstring());
                if (mat == null) {
                    return NIL;
                }
                player.getWorld().getBlockAt(pos.get("x").checkint(), pos.get("y").checkint(), pos.get("z").checkint()).setType(mat);
                return NIL;
            }
        });
        serverLib.set("interactAt", new OneArgFunction() {
            @Override
            public LuaValue call(LuaValue arg1) {
                LuaTable pos = arg1.checktable();
                Block block = player.getWorld().getBlockAt(pos.get("x").checkint(), pos.get("y").checkint(), pos.get("z").checkint());
                LoaderRecorder.getLoaderInteractionElement(block, (loaderInteractionElement, s) -> {
                    loaderInteractionElement.execute(aLong -> {
                        // Ignore
                    });
                });
                return NIL;
            }
        });
        return serverLib;
    }
}
