package de.viper.survivalplus.Manager;

import de.viper.survivalplus.SurvivalPlus;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class StatsManager {

    private final SurvivalPlus plugin;
    private File statsFile;
    private FileConfiguration statsConfig;

    public StatsManager(SurvivalPlus plugin) {
        this.plugin = plugin;
        createStatsFile();
    }

    private void createStatsFile() {
        statsFile = new File(plugin.getDataFolder(), "stats.yml");
        if (!statsFile.exists()) {
            plugin.saveResource("stats.yml", false);
        }
        statsConfig = YamlConfiguration.loadConfiguration(statsFile);
    }

    public void saveStats() {
        try {
            statsConfig.save(statsFile);
        } catch (IOException e) {
            plugin.getLogger().severe("Fehler beim Speichern der stats.yml: " + e.getMessage());
        }
    }

    // Spielzeit in Sekunden addieren
    public void addPlayTime(UUID playerUUID, long seconds) {
        long current = statsConfig.getLong(playerUUID + ".playtime", 0);
        statsConfig.set(playerUUID + ".playtime", current + seconds);
    }

    // Kills addieren
    public void addKills(UUID playerUUID, int amount) {
        int current = statsConfig.getInt(playerUUID + ".kills", 0);
        statsConfig.set(playerUUID + ".kills", current + amount);
    }

    // Tode addieren
    public void addDeaths(UUID playerUUID, int amount) {
        int current = statsConfig.getInt(playerUUID + ".deaths", 0);
        statsConfig.set(playerUUID + ".deaths", current + amount);
    }

    // Abgebaute Blöcke addieren
    public void addBlocksBroken(UUID playerUUID, int amount) {
        int current = statsConfig.getInt(playerUUID + ".blocks_broken", 0);
        statsConfig.set(playerUUID + ".blocks_broken", current + amount);
    }

    // Platzierte Blöcke addieren
    public void addBlocksPlaced(UUID playerUUID, int amount) {
        int current = statsConfig.getInt(playerUUID + ".blocks_placed", 0);
        statsConfig.set(playerUUID + ".blocks_placed", current + amount);
    }

    // Getter für Statistikwerte

    public long getPlayTime(UUID playerUUID) {
        return statsConfig.getLong(playerUUID + ".playtime", 0);
    }

    public int getKills(UUID playerUUID) {
        return statsConfig.getInt(playerUUID + ".kills", 0);
    }

    public int getDeaths(UUID playerUUID) {
        return statsConfig.getInt(playerUUID + ".deaths", 0);
    }

    public int getBlocksBroken(UUID playerUUID) {
        return statsConfig.getInt(playerUUID + ".blocks_broken", 0);
    }

    public int getBlocksPlaced(UUID playerUUID) {
        return statsConfig.getInt(playerUUID + ".blocks_placed", 0);
    }

}
