package de.winniepat.citrus.cosmetics;

import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.NbtCompound;

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

public class CosmeticManager {
    private static final Map<UUID, PlayerCosmeticData> playerData = new HashMap<>();

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

        public void setActiveWings(String wingId) {
            this.activeWings = wingId;
        }

        public String getActiveWings() {
            return activeWings;
        }

        public void setWingsVisible(boolean visible) {
            this.wingsVisible = visible;
        }

        public boolean areWingsVisible() {
            return wingsVisible;
        }

        public void setActiveHat(String hatId) {
            this.activeHat = hatId;
        }

        public String getActiveHat() {
            return activeHat;
        }

        public void setHatVisible(boolean visible) {
            this.hatVisible = visible;
        }

        public boolean isHatVisible() {
            return hatVisible;
        }

        public void setActiveHalo(String haloId) {
            this.activeHalo = haloId;
        }

        public String getActiveHalo() {
            return activeHalo;
        }

        public void setHaloVisible(boolean visible) {
            this.haloVisible = visible;
        }

        public boolean isHaloVisible() {
            return haloVisible;
        }

        public NbtCompound toNbt() {
            NbtCompound nbt = new NbtCompound();
            nbt.putString("activeWings", activeWings);
            nbt.putBoolean("wingsVisible", wingsVisible);
            nbt.putString("activeHat", activeHat);
            nbt.putBoolean("hatVisible", hatVisible);
            nbt.putString("activeHalo", activeHalo);
            nbt.putBoolean("haloVisible", haloVisible);
            return nbt;
        }

        public static PlayerCosmeticData fromNbt(NbtCompound nbt) {
            PlayerCosmeticData data = new PlayerCosmeticData();
            if (nbt.contains("activeWings")) {
                data.activeWings = nbt.getString("activeWings");
            }
            if (nbt.contains("wingsVisible")) {
                data.wingsVisible = nbt.getBoolean("wingsVisible");
            }
            if (nbt.contains("activeHat")) {
                data.activeHat = nbt.getString("activeHat");
            }
            if (nbt.contains("hatVisible")) {
                data.hatVisible = nbt.getBoolean("hatVisible");
            }
            if (nbt.contains("activeHalo")) {
                data.activeHalo = nbt.getString("activeHalo");
            }
            if (nbt.contains("haloVisible")) {
                data.haloVisible = nbt.getBoolean("haloVisible");
            }
            return data;
        }
    }

    public static PlayerCosmeticData getData(PlayerEntity player) {
        return playerData.computeIfAbsent(player.getUuid(), uuid -> new PlayerCosmeticData());
    }

    public static void setWings(PlayerEntity player, String wingId) {
        getData(player).setActiveWings(wingId);
    }

    public static void toggleWings(PlayerEntity player) {
        PlayerCosmeticData data = getData(player);
        data.setWingsVisible(!data.areWingsVisible());
    }

    public static void setHat(PlayerEntity player, String hatId) {
        getData(player).setActiveHat(hatId);
    }

    public static void toggleHat(PlayerEntity player) {
        PlayerCosmeticData data = getData(player);
        data.setHatVisible(!data.isHatVisible());
    }

    public static void setHalo(PlayerEntity player, String haloId) {
        getData(player).setActiveHalo(haloId);
    }

    public static void toggleHalo(PlayerEntity player) {
        PlayerCosmeticData data = getData(player);
        data.setHaloVisible(!data.isHaloVisible());
    }
}