package de.viper.survivalplus.listeners;

import de.viper.survivalplus.SurvivalPlus;
import de.viper.survivalplus.tasks.AFKManager;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.event.player.*;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.scheduler.BukkitRunnable;

public class AFKListener implements Listener {

    private final SurvivalPlus plugin;
    private AFKManager afkManager;
    private boolean afkEnabled;
    private FileConfiguration config;

    private BukkitRunnable afkTask;

    public AFKListener(SurvivalPlus plugin) {
        this.plugin = plugin;
        this.config = plugin.getConfig();
        loadSettings();
        if (afkEnabled) {
            startTask();
        }
    }

    private void loadSettings() {
        this.afkEnabled = config.getBoolean("afk.enabled", true);
        long afkAfter = config.getLong("afk.afk-after-seconds", 300);
        long kickAfter = config.getLong("afk.kick-after-seconds", 0);
        this.afkManager = new AFKManager(plugin, afkAfter, kickAfter);
    }

    private void startTask() {
        if (afkTask != null) {
            afkTask.cancel();
        }
        afkTask = new BukkitRunnable() {
            @Override
            public void run() {
                for (Player player : Bukkit.getOnlinePlayers()) {
                    afkManager.checkAFKStatus(player);
                }
            }
        };
        afkTask.runTaskTimer(plugin, 60L, 100L);
    }

    private void handleActivity(Player player) {
        if (!afkEnabled) return;
        afkManager.updateActivity(player);
    }

    @org.bukkit.event.EventHandler
    public void onMove(PlayerMoveEvent event) {
        if (!event.getFrom().toVector().equals(event.getTo().toVector())) {
            handleActivity(event.getPlayer());
        }
    }

    @org.bukkit.event.EventHandler
    public void onChat(AsyncPlayerChatEvent event) {
        handleActivity(event.getPlayer());
    }

    @org.bukkit.event.EventHandler
    public void onInteract(PlayerInteractEvent event) {
        handleActivity(event.getPlayer());
    }

    @org.bukkit.event.EventHandler
    public void onInventoryClick(InventoryClickEvent event) {
        if (event.getWhoClicked() instanceof Player player) {
            handleActivity(player);
        }
    }

    @org.bukkit.event.EventHandler
    public void onBlockPlace(BlockPlaceEvent event) {
        handleActivity(event.getPlayer());
    }

    @org.bukkit.event.EventHandler
    public void onBlockBreak(BlockBreakEvent event) {
        handleActivity(event.getPlayer());
    }

    @org.bukkit.event.EventHandler
    public void onJoin(PlayerJoinEvent event) {
        afkManager.updateActivity(event.getPlayer());
    }

    @org.bukkit.event.EventHandler
    public void onQuit(PlayerQuitEvent event) {
        afkManager.reset(event.getPlayer());
    }

    public void reloadConfig(FileConfiguration config) {
        this.config = config;
        loadSettings();
        if (afkTask != null) {
            afkTask.cancel();
            afkTask = null;
        }
        if (afkEnabled) {
            startTask();
        }
    }
}
