/*
 *   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.Bukkit;
import org.bukkit.boss.BarColor;
import org.bukkit.boss.BarFlag;
import org.bukkit.boss.BarStyle;
import org.bukkit.boss.BossBar;
import org.bukkit.entity.Player;
import org.luaj.vm2.LuaError;
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.OneArgFunction;
import org.luaj.vm2.lib.ThreeArgFunction;
import org.luaj.vm2.lib.ZeroArgFunction;

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

    @Override
    public LuaTable get(Player player) {
        LuaTable table = new LuaTable();

        table.set("create", new ThreeArgFunction() {

            @Override
            public LuaValue call(LuaValue arg1, LuaValue arg2, LuaValue arg3) {
                String title = arg1.checkjstring();
                BarStyle style;
                BarColor color;
                try {
                    color = BarColor.valueOf(arg2.checkjstring());
                    style = BarStyle.valueOf(arg3.checkjstring());
                } catch (IllegalArgumentException e) {
                    throw new LuaError("Invalid color or style");
                }
                BossBar bossBar = Bukkit.createBossBar(title, color, style);
                bossBar.addPlayer(player);

                LuaTable bbTable = new LuaTable();

                bbTable.set("title", getterAndSetter("title", bossBar::getTitle, bossBar::setTitle));
                bbTable.set("style", getterAndSetter("style", () -> bossBar.getStyle().name(), s -> {
                    try {
                        bossBar.setStyle(BarStyle.valueOf(s));
                    } catch (IllegalArgumentException e) {
                        throw new LuaError("Invalid style");
                    }
                }));
                bbTable.set("color", getterAndSetter("color", () -> bossBar.getColor().name(), s -> {
                    try {
                        bossBar.setColor(BarColor.valueOf(s));
                    } catch (IllegalArgumentException e) {
                        throw new LuaError("Invalid color");
                    }
                }));
                bbTable.set("progress", getterAndSetter("progress", bossBar::getProgress, bossBar::setProgress));
                bbTable.set("visible", getterAndSetter("visible", bossBar::isVisible, bossBar::setVisible));
                bbTable.set("hasFlag", new OneArgFunction() {
                    @Override
                    public LuaValue call(LuaValue arg) {
                        try {
                            return LuaValue.valueOf(bossBar.hasFlag(BarFlag.valueOf(arg.checkjstring())));
                        } catch (IllegalArgumentException e) {
                            throw new LuaError("Invalid flag");
                        }
                    }
                });

                bbTable.set("addFlag", new OneArgFunction() {
                    @Override
                    public LuaValue call(LuaValue arg) {
                        try {
                            bossBar.addFlag(BarFlag.valueOf(arg.checkjstring()));
                            return LuaValue.TRUE;
                        } catch (IllegalArgumentException e) {
                            throw new LuaError("Invalid flag");
                        }
                    }
                });

                bbTable.set("removeFlag", new OneArgFunction() {
                    @Override
                    public LuaValue call(LuaValue arg) {
                        try {
                            bossBar.removeFlag(BarFlag.valueOf(arg.checkjstring()));
                            return LuaValue.TRUE;
                        } catch (IllegalArgumentException e) {
                            throw new LuaError("Invalid flag");
                        }
                    }
                });
                bbTable.set("destroy", new ZeroArgFunction() {
                    @Override
                    public LuaValue call() {
                        bossBar.removeAll();
                        return LuaValue.NIL;
                    }
                });

                return bbTable;
            }
        });

        return table;
    }
}
