package de.hessj.environmentex;

import java.util.HashMap;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.scheduler.BukkitTask;
import de.hessj.helper.Helper.Type;
import net.kyori.adventure.text.Component;

public class AFKListener implements Listener {
    public static HashMap<UUID, BukkitTask> timers = new HashMap<UUID, BukkitTask>();

    @EventHandler
    public void onMove(PlayerMoveEvent e) {
        resetTimer(e.getPlayer());
    }

    public void resetTimer(Player p) {
        timers.get(p.getUniqueId()).cancel();
        p.playerListName(Component.text(p.getName()));
        createAFKTimer(p);
    }

    public static void createAFKTimer(Player p) {
        BukkitTask i = Bukkit.getServer().getScheduler().runTaskLater(App.main, new Runnable() {
            @Override
            public void run() {
                p.playerListName(App.helper.R3SMessage(Type.ERROR, "[AFK] " + p.getName()));
            }
        }, (5 * 60 * 20));
        timers.put(p.getUniqueId(), i);
    }
}
