package com.yaohun.demonskills.manage;

import com.yaohun.demonskills.SkillsMain;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class ComboManager {
    private static final Map<UUID, Integer> comboStage = new HashMap<>();
    private static final Map<UUID, BukkitRunnable> resetTaskMap = new HashMap<>();
    private static final int COMBO_TIMEOUT_TICKS = 20 * 3; // 2秒未连段自动重置

    public static int getComboStage(Player player) {
        return comboStage.getOrDefault(player.getUniqueId(), 0);
    }

    public static void nextComboStage(Player player) {
        int current = getComboStage(player);
        comboStage.put(player.getUniqueId(), current + 1);
        resetComboTimeout(player);
    }

    public static void resetCombo(Player player) {
        comboStage.remove(player.getUniqueId());
        BukkitRunnable task = resetTaskMap.remove(player.getUniqueId());
        if (task != null) task.cancel();
    }

    private static void resetComboTimeout(Player player) {
        // 清理旧任务
        BukkitRunnable oldTask = resetTaskMap.remove(player.getUniqueId());
        if (oldTask != null) oldTask.cancel();

        // 新任务
        BukkitRunnable task = new BukkitRunnable() {
            @Override
            public void run() {
                resetCombo(player);
            }
        };
        task.runTaskLater(SkillsMain.inst(), COMBO_TIMEOUT_TICKS);
        resetTaskMap.put(player.getUniqueId(), task);
    }
}
