/*
 *   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.region.Point;
import lombok.AllArgsConstructor;
import org.bukkit.entity.Player;
import org.luaj.vm2.*;
import org.luaj.vm2.lib.VarArgFunction;
import org.luaj.vm2.lib.ZeroArgFunction;

import java.util.function.Consumer;
import java.util.function.Supplier;

public interface LuaLib {

    static <T> LuaValue runGetter(Supplier<T> supplier) {
        T value = supplier.get();
        if (value == null) {
            return LuaValue.NIL;
        }
        Class<?> clazz = value.getClass();
        if (clazz == Integer.class) {
            return LuaValue.valueOf((int) value);
        } else if (clazz == Long.class) {
            return LuaValue.valueOf((int) (long) value);
        } else if (clazz == Double.class) {
            return LuaValue.valueOf((double) value);
        } else if (clazz == Float.class) {
            return LuaValue.valueOf((float) value);
        } else if (clazz == String.class) {
            return LuaValue.valueOf((String) value);
        } else if (clazz == Boolean.class) {
            return LuaValue.valueOf((boolean) value);
        }
        return LuaValue.NIL;
    }

    default <T> LuaFunction getter(Supplier<T> supplier) {
        return new ZeroArgFunction() {
            @Override
            public LuaValue call() {
                return runGetter(supplier);
            }
        };
    }

    default <T> LuaFunction getterAndSetter(String name, Supplier<T> supplier, Consumer<T> consumer) {
        return new GetterAndSetter<>(name, supplier, consumer);
    }

    default String varArgsToString(Varargs varargs) {
        return de.steamwar.bausystem.features.script.lua.SteamWarLuaPlugin.varArgsToString(varargs);
    }

    default LuaTable toPos(Point point) {
        if (point == null) return LuaTable.tableOf();
        return LuaValue.tableOf(new LuaValue[] {
                LuaValue.valueOf("x"), LuaValue.valueOf(point.getX()),
                LuaValue.valueOf("y"), LuaValue.valueOf(point.getY()),
                LuaValue.valueOf("z"), LuaValue.valueOf(point.getZ())
        });
    }

    default Class<? extends LuaLib> parent() {
        return null;
    }
    String name();
    LuaTable get(Player player);

    @AllArgsConstructor
    class GetterAndSetter<T> extends VarArgFunction {

        private String name;
        private Supplier<T> supplier;
        private Consumer consumer;

        @Override
        public Varargs invoke(Varargs args) {
            if (args.narg() == 0) {
                return runGetter(supplier);
            } else {
                if (args.narg() == 1) {
                    LuaValue luaValue = args.arg(1);
                    try {
                        try {
                            consumer.accept(luaValue.toboolean());
                            return NIL;
                        } catch (Exception ingored) {}
                        try {
                            consumer.accept((long) luaValue.toint());
                            return NIL;
                        } catch (Exception ignored) {}
                        try {
                            consumer.accept(luaValue.toint());
                            return NIL;
                        } catch (Exception ignored) {}
                        try {
                            consumer.accept(luaValue.todouble());
                            return NIL;
                        } catch (Exception ignored) {}
                        try {
                            consumer.accept((float) luaValue.todouble());
                            return NIL;
                        } catch (Exception ignored) {}
                        try {
                            consumer.accept(luaValue.toString());
                            return NIL;
                        } catch (Exception ignored) {}
                        throw new LuaError("Invalid lua type: " + luaValue.typename());
                    } catch (Throwable throwable) {
                        throw new LuaError("Error in '" + name + "' " + throwable.getMessage());
                    }
                }
                return NIL;
            }
        }
    }
}
