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

import de.steamwar.fightsystem.Config;
import de.steamwar.fightsystem.FightSystem;
import de.steamwar.fightsystem.commands.GUI;
import de.steamwar.fightsystem.utils.ItemBuilder;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.potion.PotionEffect;

import java.util.Collection;
import java.util.Objects;
import java.util.function.Consumer;

public class HotbarKit extends Kit {

    public static final HotbarKit SPECTATOR_KIT = new HotbarKit();

    static {
        for (int i = 0; i < 9; i++) {
            SPECTATOR_KIT.setItem(i, "JOIN_REQUEST", new ItemBuilder(Material.PAPER).build(), GUI::joinRequest);
        }
    }

    protected static final int HOTBAR_SIZE = 9;

    private final String[] nameTags;
    protected final Consumer<Player>[] onClicks;

    protected HotbarKit(String name, ItemStack[] inventory, ItemStack[] armor, Collection<PotionEffect> effects, String[] nameTags, Consumer<Player>[] onClicks) {
        super(name, inventory, armor, effects);
        this.nameTags = nameTags;
        this.onClicks = onClicks;
    }

    public HotbarKit() {
        this(null, new ItemStack[HOTBAR_SIZE], null, null, new String[HOTBAR_SIZE], new Consumer[HOTBAR_SIZE]);
    }

    public HotbarKit(HotbarKit kit) {
        this(kit.getName(), kit.getInventory().clone(), kit.getArmor() != null ? kit.getArmor().clone() : null, kit.getEffects(), kit.nameTags.clone(), kit.onClicks.clone());
    }

    public void setItem(int id, String nameTag, ItemStack stack, Consumer<Player> onClick) {
        super.setItem(id, stack);
        nameTags[id] = nameTag;
        onClicks[id] = onClick;
    }

    @Override
    public synchronized void loadToPlayer(Player player) {
        for (int i = 0; i < HOTBAR_SIZE; i++) {
            if (nameTags[i] != null) {
                ItemMeta meta = Objects.requireNonNull(getInventory()[i].getItemMeta());
                meta.setDisplayName(FightSystem.getMessage().parse(nameTags[i], player, Config.GameModeConfig.GameName));
                getInventory()[i].setItemMeta(meta);
            }
        }
        super.loadToPlayer(player);
    }
}
