package com.tiedup.remake.entities.skins;

import com.tiedup.remake.core.TiedUpMod;
import java.util.*;
import net.minecraft.util.RandomSource;

/**
 * Registry for entity skins of a specific entity type.
 *
 * Each entity type (kidnapper, damsel, etc.) has its own registry instance.
 * Skins are loaded from JSON files and stored here for quick access.
 *
 */
public class EntitySkinRegistry {

    private final String entityType;
    private final Map<String, SkinDefinition> skins = new LinkedHashMap<>();
    private final List<SkinDefinition> skinsList = new ArrayList<>();
    private final RandomSource random = RandomSource.create();

    /**
     * Create a new skin registry for an entity type.
     *
     * @param entityType Entity type identifier (e.g., "kidnapper", "damsel")
     */
    public EntitySkinRegistry(String entityType) {
        this.entityType = entityType;
    }

    /**
     * Register a skin definition.
     *
     * If a skin with the same ID already exists, it will be skipped
     * (datapacks can override by having higher priority).
     *
     * @param skin Skin definition to register
     */
    public void register(SkinDefinition skin) {
        if (skin == null) {
            TiedUpMod.LOGGER.warn(
                "[EntitySkinRegistry] Attempted to register null skin for {}",
                entityType
            );
            return;
        }

        if (skins.containsKey(skin.id())) {
            TiedUpMod.LOGGER.debug(
                "[EntitySkinRegistry] Skin {} already registered for {}, skipping",
                skin.id(),
                entityType
            );
            return;
        }

        skins.put(skin.id(), skin);
        skinsList.add(skin);

        TiedUpMod.LOGGER.debug(
            "[EntitySkinRegistry] Registered skin {} for {}",
            skin.id(),
            entityType
        );
    }

    /**
     * Get a skin by ID.
     *
     * @param id Skin ID
     * @return SkinDefinition or null if not found
     */
    public SkinDefinition get(String id) {
        return skins.get(id);
    }

    /**
     * Get a random skin from this registry.
     *
     * @return Random SkinDefinition
     * @throws IllegalStateException if no skins are registered
     */
    public SkinDefinition getRandom() {
        if (skinsList.isEmpty()) {
            throw new IllegalStateException(
                "No skins registered for entity type: " + entityType
            );
        }
        return skinsList.get(random.nextInt(skinsList.size()));
    }

    /**
     * Get all registered skins.
     *
     * @return Unmodifiable collection of all skins
     */
    public Collection<SkinDefinition> getAllSkins() {
        return Collections.unmodifiableCollection(skins.values());
    }

    /**
     * Get the number of registered skins.
     *
     * @return Number of skins
     */
    public int count() {
        return skins.size();
    }

    /**
     * Clear all registered skins.
     * Used before reloading from datapacks.
     */
    public void clear() {
        skins.clear();
        skinsList.clear();
        TiedUpMod.LOGGER.debug(
            "[EntitySkinRegistry] Cleared all skins for {}",
            entityType
        );
    }

    /**
     * Get the entity type identifier.
     *
     * @return Entity type string
     */
    public String getEntityType() {
        return entityType;
    }
}
