/*
 *   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.linkage.Linked;
import org.bukkit.entity.Player;
import org.luaj.vm2.LuaError;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Varargs;
import org.luaj.vm2.lib.VarArgFunction;

import java.util.Random;

@Linked
public class RandomLib implements LuaLib {

    private static Random random = new Random();

    @Override
    public String name() {
        return "random";
    }

    @Override
    public LuaTable get(Player player) {
        LuaTable randomLib = new LuaTable();
        randomLib.set("nextInt", new NextInt());
        randomLib.set("nextDouble", new NextDouble());
        randomLib.set("nextBool", new NextBool());
        return randomLib;
    }

    private static class NextInt extends VarArgFunction {

        @Override
        public Varargs invoke(Varargs args) {
            if (args.narg() == 0) {
                return LuaValue.valueOf(random.nextInt());
            }
            if (args.narg() == 1) {
                return LuaValue.valueOf(random.nextInt(args.checkint(1)));
            }
            if (args.narg() == 2) {
                return LuaValue.valueOf(random.nextInt(args.checkint(1), args.checkint(2)));
            }
            throw new LuaError("Invalid number of arguments for random.nextInt() zero, one or two expected got " + args.narg());
        }
    }

    private static class NextDouble extends VarArgFunction {

        @Override
        public Varargs invoke(Varargs args) {
            if (args.narg() == 0) {
                return LuaValue.valueOf(random.nextDouble());
            }
            if (args.narg() == 1) {
                return LuaValue.valueOf(random.nextDouble(args.checkdouble(1)));
            }
            if (args.narg() == 2) {
                return LuaValue.valueOf(random.nextDouble(args.checkdouble(1), args.checkdouble(2)));
            }
            throw new LuaError("Invalid number of arguments for random.nextDouble() zero, one or two expected got " + args.narg());
        }
    }

    private static class NextBool extends VarArgFunction {

            @Override
            public Varargs invoke(Varargs args) {
                if (args.narg() == 0) {
                    return LuaValue.valueOf(random.nextBoolean());
                }
                throw new LuaError("Invalid number of arguments for random.nextBoolean() zero expected got " + args.narg());
            }
    }
}
