package de.viper.survivalplus.listeners;

import de.viper.survivalplus.fun.FunChallenge;
import de.viper.survivalplus.fun.FunChallengeManager;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;

public class ChallengeCollectListener implements Listener {

    private final FunChallengeManager challengeManager;
    private final NamespacedKey droppedKey;

    public ChallengeCollectListener(FunChallengeManager challengeManager, NamespacedKey droppedKey) {
        this.challengeManager = challengeManager;
        this.droppedKey = droppedKey;
    }

    @EventHandler
    public void onItemDrop(PlayerDropItemEvent event) {
        ItemStack dropped = event.getItemDrop().getItemStack();
        ItemMeta meta = dropped.getItemMeta();
        if (meta != null) {
            meta.getPersistentDataContainer().set(droppedKey, PersistentDataType.INTEGER, 1);
            dropped.setItemMeta(meta);
        }
    }

    @EventHandler
    public void onPlayerPickup(PlayerPickupItemEvent event) {
        FunChallenge challenge = challengeManager.getActiveChallenge();
        if (challenge == null) return;

        Player player = event.getPlayer();
        ItemStack item = event.getItem().getItemStack();
        ItemMeta meta = item.getItemMeta();

        if (meta != null && meta.getPersistentDataContainer().has(droppedKey, PersistentDataType.INTEGER)) {
            return; // Item aus Inventar gedroppt, nicht zählen
        }

        if (item.getType().name().equalsIgnoreCase(challenge.getItem())) {
            int current = challengeManager.getGlobalProgress(challenge.getName());
            current += item.getAmount();
            challengeManager.setGlobalProgress(challenge.getName(), current);

            if (current >= challenge.getAmount()) {
                player.getServer().broadcastMessage("§aChallenge \"" + challenge.getName() + "\" geschafft!");
                giveReward(player, challenge.getReward());
                challenge.setActive(false);
                challengeManager.resetProgress(challenge.getName());
            } else {
                player.sendMessage("§eFortschritt: " + current + "/" + challenge.getAmount() + " " + challenge.getItem());
            }
        }
    }

    private void giveReward(Player player, String reward) {
        if (reward == null || reward.isEmpty()) return;

        String[] parts = reward.split(",");
        if (parts.length != 2) return;

        String itemName = parts[0];
        int amount;
        try {
            amount = Integer.parseInt(parts[1]);
        } catch (NumberFormatException e) {
            return;
        }

        Material material = Material.matchMaterial(itemName);
        if (material == null) return;

        ItemStack rewardItem = new ItemStack(material, amount);
        player.getInventory().addItem(rewardItem);
        player.sendMessage("§aDu hast deine Belohnung erhalten: " + amount + "x " + itemName);
    }
}
