package de.viper.survivalplus.commands;

import de.viper.survivalplus.Manager.StatsManager;
import de.viper.survivalplus.SurvivalPlus;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class StatsCommand implements CommandExecutor {

    private final SurvivalPlus plugin;
    private final StatsManager statsManager;

    public StatsCommand(SurvivalPlus plugin, StatsManager statsManager) {
        this.plugin = plugin;
        this.statsManager = statsManager;
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

        if (!(sender instanceof Player)) {
            sender.sendMessage(plugin.getMessage("stats.only_player"));
            return true;
        }

        Player player = (Player) sender;

        long time = statsManager.getPlayTime(player.getUniqueId());
        int kills = statsManager.getKills(player.getUniqueId());
        int deaths = statsManager.getDeaths(player.getUniqueId());
        int placed = statsManager.getBlocksPlaced(player.getUniqueId());
        int broken = statsManager.getBlocksBroken(player.getUniqueId());

        if (time == 0 && kills == 0 && deaths == 0 && placed == 0 && broken == 0) {
            player.sendMessage(plugin.getMessage("stats.no_stats"));
            return true;
        }

        // K/D Ratio berechnen (vermeide Division durch 0)
        String kdRatio = deaths > 0 ? String.format("%.2f", (double) kills / deaths) : "∞";

        // Zusätzliche Minecraft Statistik
        int jumps = player.getStatistic(org.bukkit.Statistic.JUMP);
        double walkKm = player.getStatistic(org.bukkit.Statistic.WALK_ONE_CM) / 100_000.0; // cm zu km

        player.sendMessage(plugin.getMessage("stats.header").replace("{player}", player.getName()));
        player.sendMessage(plugin.getMessage("stats.playtime").replace("{time}", formatPlayTime(time)));
        player.sendMessage(plugin.getMessage("stats.kills").replace("{kills}", String.valueOf(kills)));
        player.sendMessage(plugin.getMessage("stats.deaths").replace("{deaths}", String.valueOf(deaths)));
        player.sendMessage(plugin.getMessage("stats.kd_ratio").replace("{kd}", kdRatio));
        player.sendMessage(plugin.getMessage("stats.blocks_placed").replace("{placed}", String.valueOf(placed)));
        player.sendMessage(plugin.getMessage("stats.blocks_broken").replace("{broken}", String.valueOf(broken)));
        player.sendMessage(plugin.getMessage("stats.jumps").replace("{jumps}", String.valueOf(jumps)));
        player.sendMessage(plugin.getMessage("stats.walked").replace("{walked}", String.format("%.2f", walkKm)));
        player.sendMessage(plugin.getMessage("stats.footer"));

        return true;
    }

    private String formatPlayTime(long seconds) {
        long hours = seconds / 3600;
        long minutes = (seconds % 3600) / 60;
        long secs = seconds % 60;

        return String.format("%02dh %02dm %02ds", hours, minutes, secs);
    }
}
