package de.winniepat.citrus.cosmetics.wings;

import com.google.gson.*;
import de.winniepat.citrus.Client;
import de.winniepat.citrus.cosmetics.wings.sync.WingSyncManager;
import net.minecraft.client.MinecraftClient;

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

public class ClientWingManager {
    private static final File CONFIG_FILE = new File(Client.CONFIG_DIR, "citrus_wings.json");

    private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();
    private static Map<UUID, WingData> wingDataMap = new HashMap<>();

    public static class WingData {
        public String activeWings = "none";
        public boolean wingsVisible = true;
    }

    static {
        load();
    }

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

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

                    if (data.has("activeWings")) {
                        wingData.activeWings = data.get("activeWings").getAsString();
                    }
                    if (data.has("wingsVisible")) {
                        wingData.wingsVisible = data.get("wingsVisible").getAsBoolean();
                    }

                    wingDataMap.put(uuid, wingData);
                }
            } catch (Exception e) {
                System.err.println("Failed to load wing 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, WingData> entry : wingDataMap.entrySet()) {
                    JsonObject data = new JsonObject();
                    data.addProperty("activeWings", entry.getValue().activeWings);
                    data.addProperty("wingsVisible", entry.getValue().wingsVisible);
                    json.add(entry.getKey().toString(), data);
                }
                GSON.toJson(json, writer);
            }
        } catch (Exception e) {
            System.err.println("Failed to save wing data: " + e.getMessage());
        }
    }

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

    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) {
        WingData data = getData(playerUUID);
        data.activeWings = wingId.toLowerCase();
        save();

        if (MinecraftClient.getInstance().player != null) {
            String playerName = MinecraftClient.getInstance().player.getName().getString();
            WingSyncManager.sendWingsToServer(playerUUID, data, playerName);
        }
    }


    public static void toggleWings(UUID playerUUID) {
        WingData data = getData(playerUUID);
        data.wingsVisible = !data.wingsVisible;
        save();

        if (MinecraftClient.getInstance().player != null) {
            String playerName = MinecraftClient.getInstance().player.getName().getString();
            de.winniepat.citrus.cosmetics.wings.sync.WingSyncManager.sendWingsToServer(playerUUID, data, playerName);
        }
    }

    public static void setWingsVisible(UUID playerUUID, boolean visible) {
        WingData data = getData(playerUUID);
        data.wingsVisible = visible;
        save();

        if (MinecraftClient.getInstance().player != null) {
            String playerName = MinecraftClient.getInstance().player.getName().getString();
            WingSyncManager.sendWingsToServer(playerUUID, data, playerName);
        }
    }
}