package de.winniepat.citrus.cosmetics;

import com.google.gson.*;
import de.winniepat.citrus.Client;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class ClientCosmeticManager {
    private static final File CONFIG_FILE = new File(Client.CONFIG_DIR, "citrus_cosmetics.json");
    private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
    private static Map<UUID, PlayerCosmeticData> cosmeticDataMap = new HashMap<>();

    public static class PlayerCosmeticData {
        public String activeWings = "none";
        public boolean wingsVisible = true;
        public String activeHat = "none";
        public boolean hatVisible = true;
        public String activeHalo = "none";
        public boolean haloVisible = true;
    }

    static {
        load();
    }

    private static void load() {
        if (CONFIG_FILE.exists()) {
            try (FileReader reader = new FileReader(CONFIG_FILE)) {
                JsonObject json = JsonParser.parseReader(reader).getAsJsonObject();
                cosmeticDataMap.clear();

                for (Map.Entry<String, JsonElement> entry : json.entrySet()) {
                    UUID uuid = UUID.fromString(entry.getKey());
                    JsonObject data = entry.getValue().getAsJsonObject();
                    PlayerCosmeticData cosmeticData = new PlayerCosmeticData();

                    if (data.has("activeWings")) {
                        cosmeticData.activeWings = data.get("activeWings").getAsString();
                    }
                    if (data.has("wingsVisible")) {
                        cosmeticData.wingsVisible = data.get("wingsVisible").getAsBoolean();
                    }
                    if (data.has("activeHat")) {
                        cosmeticData.activeHat = data.get("activeHat").getAsString();
                    }
                    if (data.has("hatVisible")) {
                        cosmeticData.hatVisible = data.get("hatVisible").getAsBoolean();
                    }
                    if (data.has("activeHalo")) {
                        cosmeticData.activeHalo = data.get("activeHalo").getAsString();
                    }
                    if (data.has("haloVisible")) {
                        cosmeticData.haloVisible = data.get("haloVisible").getAsBoolean();
                    }

                    cosmeticDataMap.put(uuid, cosmeticData);
                }
            } catch (Exception e) {
                System.err.println("Failed to load cosmetic data: " + e.getMessage());
            }
        }
    }

    public static void save() {
        try {
            if (!CONFIG_FILE.getParentFile().exists()) {
                CONFIG_FILE.getParentFile().mkdirs();
            }

            try (FileWriter writer = new FileWriter(CONFIG_FILE)) {
                JsonObject json = new JsonObject();
                for (Map.Entry<UUID, PlayerCosmeticData> entry : cosmeticDataMap.entrySet()) {
                    JsonObject data = new JsonObject();
                    data.addProperty("activeWings", entry.getValue().activeWings);
                    data.addProperty("wingsVisible", entry.getValue().wingsVisible);
                    data.addProperty("activeHat", entry.getValue().activeHat);
                    data.addProperty("hatVisible", entry.getValue().hatVisible);
                    data.addProperty("activeHalo", entry.getValue().activeHalo);
                    data.addProperty("haloVisible", entry.getValue().haloVisible);
                    json.add(entry.getKey().toString(), data);
                }
                GSON.toJson(json, writer);
            }
        } catch (Exception e) {
            System.err.println("Failed to save cosmetic data: " + e.getMessage());
        }
    }

    public static PlayerCosmeticData getData(UUID playerUUID) {
        return cosmeticDataMap.computeIfAbsent(playerUUID, uuid -> new PlayerCosmeticData());
    }

    public static String getActiveWings(UUID playerUUID) {
        return getData(playerUUID).activeWings;
    }

    public static boolean areWingsVisible(UUID playerUUID) {
        return getData(playerUUID).wingsVisible;
    }

    public static void setActiveWings(UUID playerUUID, String wingId) {
        PlayerCosmeticData data = getData(playerUUID);
        data.activeWings = wingId.toLowerCase();
        save();
        CosmeticSyncManager.sendCosmeticsToServer(playerUUID, data);
    }

    public static void toggleWings(UUID playerUUID) {
        PlayerCosmeticData data = getData(playerUUID);
        data.wingsVisible = !data.wingsVisible;
        save();
        CosmeticSyncManager.sendCosmeticsToServer(playerUUID, data);
    }

    public static String getActiveHat(UUID playerUUID) {
        return getData(playerUUID).activeHat;
    }

    public static boolean isHatVisible(UUID playerUUID) {
        return getData(playerUUID).hatVisible;
    }

    public static void setActiveHat(UUID playerUUID, String hatId) {
        PlayerCosmeticData data = getData(playerUUID);
        data.activeHat = hatId.toLowerCase();
        save();
        CosmeticSyncManager.sendCosmeticsToServer(playerUUID, data);
    }

    public static void toggleHat(UUID playerUUID) {
        PlayerCosmeticData data = getData(playerUUID);
        data.hatVisible = !data.hatVisible;
        save();
        CosmeticSyncManager.sendCosmeticsToServer(playerUUID, data);
    }

    public static String getActiveHalo(UUID playerUUID) {
        return getData(playerUUID).activeHalo;
    }

    public static boolean isHaloVisible(UUID playerUUID) {
        return getData(playerUUID).haloVisible;
    }

    public static void setActiveHalo(UUID playerUUID, String haloId) {
        PlayerCosmeticData data = getData(playerUUID);
        data.activeHalo = haloId.toLowerCase();
        save();
        CosmeticSyncManager.sendCosmeticsToServer(playerUUID, data);
    }

    public static void toggleHalo(UUID playerUUID) {
        PlayerCosmeticData data = getData(playerUUID);
        data.haloVisible = !data.haloVisible;
        save();
        CosmeticSyncManager.sendCosmeticsToServer(playerUUID, data);
    }
}