/*
    This file is a part of the SteamWar software.

    Copyright (C) 2022  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.core.Core;
import de.steamwar.fightsystem.ArenaMode;
import de.steamwar.fightsystem.Config;
import de.steamwar.fightsystem.FightSystem;
import de.steamwar.fightsystem.commands.GUI;
import de.steamwar.fightsystem.listener.PersonalKitCreator;
import de.steamwar.fightsystem.states.FightState;
import de.steamwar.fightsystem.states.StateDependentListener;
import de.steamwar.fightsystem.states.StateDependentTask;
import de.steamwar.fightsystem.utils.ItemBuilder;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.potion.PotionEffect;

import java.util.Collection;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
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);
    }

    private static final int HOTBAR_SIZE = 9;

    private final String[] nameTags;
    private 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.GameName));
                getInventory()[i].setItemMeta(meta);
            }
        }
        super.loadToPlayer(player);
    }

    public static class HotbarKitListener implements Listener {

        private static final Set<Player> clicked = new HashSet<>();

        public HotbarKitListener() {
            new StateDependentListener(ArenaMode.AntiReplay, FightState.All, this);
            new StateDependentTask(ArenaMode.AntiReplay, FightState.All, clicked::clear, 10, 10);
        }

        @EventHandler
        public void handlePlayerInteract(PlayerInteractEvent event) {
            if(event.getAction() == Action.PHYSICAL || (Core.getVersion() > 8 && event.getHand() != EquipmentSlot.HAND))
                return;

            Player player = event.getPlayer();
            int slot = player.getInventory().getHeldItemSlot();
            Kit activeKit = activeKits.get(player);
            if(!(activeKit instanceof HotbarKit) || PersonalKitCreator.inKitCreator(player) || activeKit.getInventory()[slot] == null)
                return;

            event.setCancelled(true);
            if(!clicked.add(player))
                return;

            ((HotbarKit)activeKit).onClicks[slot].accept(player);
        }
    }
}
