package com.tiedup.remake.client.state;

import java.util.Collections;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

/**
 * Client-side cache for collar registry data.
 *
 * This cache stores the UUIDs of slaves (collar wearers) owned by the local player.
 * It is synchronized from the server via PacketSyncCollarRegistry.
 *
 * Used by SlaveManagementScreen to display owned slaves without spatial queries.
 *
 */
@OnlyIn(Dist.CLIENT)
public class CollarRegistryClient {

    // Set of slave (collar wearer) UUIDs owned by the local player
    private static final Set<UUID> ownedSlaves = ConcurrentHashMap.newKeySet();

    // ==================== SYNC FROM SERVER ====================

    /**
     * Full sync: Replace all known slaves with the provided set.
     * Called when player logs in or for full registry refresh.
     */
    public static void setSlaves(Set<UUID> slaveUUIDs) {
        ownedSlaves.clear();
        ownedSlaves.addAll(slaveUUIDs);
    }

    /**
     * Incremental update: Add and remove specific slaves.
     * Called when collar ownership changes.
     */
    public static void updateSlaves(Set<UUID> toAdd, Set<UUID> toRemove) {
        ownedSlaves.removeAll(toRemove);
        ownedSlaves.addAll(toAdd);
    }

    /**
     * Add a single slave to the cache.
     */
    public static void addSlave(UUID slaveUUID) {
        ownedSlaves.add(slaveUUID);
    }

    /**
     * Remove a single slave from the cache.
     */
    public static void removeSlave(UUID slaveUUID) {
        ownedSlaves.remove(slaveUUID);
    }

    /**
     * Clear all slaves from the cache.
     * Called on logout or dimension change.
     */
    public static void clear() {
        ownedSlaves.clear();
    }

    // ==================== QUERIES ====================

    /**
     * Get all slave UUIDs owned by the local player.
     * Returns an unmodifiable view.
     */
    public static Set<UUID> getSlaves() {
        return Collections.unmodifiableSet(ownedSlaves);
    }

    /**
     * Check if the local player owns a specific slave.
     */
    public static boolean ownsSlave(UUID slaveUUID) {
        return ownedSlaves.contains(slaveUUID);
    }

    /**
     * Get the number of slaves owned by the local player.
     */
    public static int getSlaveCount() {
        return ownedSlaves.size();
    }

    /**
     * Check if the local player has any slaves.
     */
    public static boolean hasSlaves() {
        return !ownedSlaves.isEmpty();
    }
}
