package com.loganmagnan.eventcore.managers.hotbar;

import com.loganmagnan.eventcore.EventCore;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.entity.Player;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Getter
@Setter
public abstract class HotBar {

    private EventCore main = EventCore.getInstance();

    private Map<String, ClickableItem> clickableItems = new ConcurrentHashMap<String, ClickableItem>();

    private String id;

    public HotBar(String id) {
        this.id = id;
    }

    public ClickableItem getClickableItem(String id) {
        return this.clickableItems.get(id);
    }

    public void addClickableItem(String id, ClickableItem clickableItem) {
        this.clickableItems.put(id, clickableItem);
    }

    public void removeClickableItem(String id) {
        this.clickableItems.remove(id);
    }

    public void applyToPlayer(Player player, ClickableItem clickableItem, int slot) {
        player.getInventory().setItem(slot, null);
        player.getInventory().setItem(slot, clickableItem.getItemStack());
    }

    public void applyToPlayer(Player player, boolean clearHotBar) {
        if (clearHotBar) {
            for (int i = 0; i < 8; i++) {
                player.getInventory().setItem(i, null);
            }
        }

        Map<Integer, ClickableItem> clickableItemsToApply = this.getClickableItemsToApply(player);

        for (int slot : clickableItemsToApply.keySet()) {
            player.getInventory().setItem(slot, clickableItemsToApply.get(slot).getItemStack());
        }
    }

    public abstract Map<Integer, ClickableItem> getClickableItemsToApply(Player player);
}
