package de.viper.survivalplus.Manager;

import de.viper.survivalplus.SurvivalPlus;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class WarpManager {

    private final SurvivalPlus plugin;
    private File warpsFile;
    private FileConfiguration warpsConfig;

    private final Map<String, Warp> warps = new HashMap<>(); // key: owner:name

    public WarpManager(SurvivalPlus plugin) {
        this.plugin = plugin;
        loadWarpsFile();
        loadWarps();
    }

    private void loadWarpsFile() {
        warpsFile = new File(plugin.getDataFolder(), "warps.yml");
        if (!warpsFile.exists()) {
            plugin.saveResource("warps.yml", false);
        }
        warpsConfig = YamlConfiguration.loadConfiguration(warpsFile);
    }

    public void saveWarpsFile() {
        try {
            warpsConfig.save(warpsFile);
        } catch (IOException e) {
            plugin.getLogger().severe("Konnte warps.yml nicht speichern!");
            e.printStackTrace();
        }
    }

    private void loadWarps() {
        warps.clear();
        if (warpsConfig.contains("warps")) {
            for (String key : warpsConfig.getConfigurationSection("warps").getKeys(false)) {
                String path = "warps." + key;
                String owner = warpsConfig.getString(path + ".owner");
                String name = warpsConfig.getString(path + ".name");
                ItemStack item = warpsConfig.getItemStack(path + ".item");
                double x = warpsConfig.getDouble(path + ".location.x");
                double y = warpsConfig.getDouble(path + ".location.y");
                double z = warpsConfig.getDouble(path + ".location.z");
                String worldName = warpsConfig.getString(path + ".location.world");

                Warp warp = new Warp(owner, name, item, worldName, x, y, z);
                warps.put(owner + ":" + name, warp);
            }
        }
    }

    public void addWarp(Warp warp) {
        warps.put(warp.getOwner() + ":" + warp.getName(), warp);
        saveWarpToConfig(warp);
        saveWarpsFile();
    }

    private void saveWarpToConfig(Warp warp) {
        String path = "warps." + warp.getOwner() + "_" + warp.getName();
        warpsConfig.set(path + ".owner", warp.getOwner());
        warpsConfig.set(path + ".name", warp.getName());
        warpsConfig.set(path + ".item", warp.getItem());
        warpsConfig.set(path + ".location.x", warp.getX());
        warpsConfig.set(path + ".location.y", warp.getY());
        warpsConfig.set(path + ".location.z", warp.getZ());
        warpsConfig.set(path + ".location.world", warp.getWorldName());
    }

    public Map<String, Warp> getWarps() {
        return warps;
    }

    public Warp getWarp(String owner, String name) {
        return warps.get(owner + ":" + name);
    }

    // Neu hinzugefügt: Anzahl der Warps eines Spielers zählen
    public int getWarpCountForPlayer(String playerName) {
        int count = 0;
        for (Warp warp : warps.values()) {
            if (warp.getOwner().equalsIgnoreCase(playerName)) {
                count++;
            }
        }
        return count;
    }

    // Neu hinzugefügt: Warp eines Spielers löschen
    public boolean removeWarp(String owner, String name) {
        String key = owner + ":" + name;
        if (!warps.containsKey(key)) {
            return false;
        }
        warps.remove(key);

        String path = "warps." + owner + "_" + name;
        warpsConfig.set(path, null); // Eintrag in Config entfernen
        saveWarpsFile();

        return true;
    }
}
