/*
 *   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.inventory;

import de.steamwar.core.Core;
import net.wesjd.anvilgui.AnvilGUI;
import org.bukkit.Material;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;

public class SWAnvilInv {

    private final AnvilGUI.Builder builder;
    private final Player player;
    private final String defaultText;

    private Runnable leftCallback;
    private Consumer<String> callback;

    public SWAnvilInv(Player p, String t) {
        this(p, t, "");
    }

    public SWAnvilInv(Player p, String t, String defaultText) {
        builder = new AnvilGUI.Builder().plugin(Core.getInstance()).title(t).text("»" + defaultText).onClick(this::onResult);
        this.defaultText = defaultText;
        player = p;
    }

    public void setItem(Material m) {
        setItem(m, new ArrayList<>(), false);
    }

    public void setItem(Material m, byte meta) {
        setItem(m, meta, new ArrayList<>(), false);
    }

    public void setItem(Material m, List<String> lore, boolean e) {
        setItem(m, (byte)0, lore, e);
    }

    public void setItem(Material m, byte meta, List<String> lore, boolean e) {
        builder.itemLeft(new SWItem(m, meta, "»" + defaultText, lore, e, null).getItemStack());
    }

    public void setCallback(Consumer<String> callback) {
        this.callback = callback;
    }

    public void addLeftCallback(Runnable callback) {
        leftCallback = callback;
    }

    public void addCloseCallback(Runnable callback) {
        builder.onClose(p -> callback.run());
    }

    public void open() {
        player.setLevel(1);
        builder.open(player);
    }

    private List<AnvilGUI.ResponseAction> onResult(Integer slot, AnvilGUI.StateSnapshot state) {
        if(slot != AnvilGUI.Slot.OUTPUT) {
            if(slot == AnvilGUI.Slot.INPUT_LEFT && leftCallback != null)
                leftCallback.run();

            return Collections.emptyList();
        }

        String s = state.getText();
        if(s.startsWith("»"))
            s = s.substring(1);
        callback.accept(s);
        player.setLevel(0);
        return Collections.singletonList(AnvilGUI.ResponseAction.close());
    }
}
