package cz.jzitnik.tui;

import java.util.EnumMap;
import java.util.HashMap;

/**
 * A container class that holds a mapping from enum keys to their corresponding {@link Sprite} instances.
 * Ensures that a sprite is defined for every enum constant.
 *
 * @param <E> Enum type representing different sprite categories or states.
 */
public class SpriteList<E extends Enum<E>> {
    private final EnumMap<E, Sprite> sprites;

    /**
     * Constructs a {@code SpriteList} and initializes it with the provided sprite mapping.
     * Throws a {@link RuntimeException} if any enum constant is missing in the initial map.
     *
     * @param enumClass  The enum class used for sprite keys.
     * @param initialMap A map containing sprite instances for each enum constant.
     */
    public SpriteList(Class<E> enumClass, HashMap<E, Sprite> initialMap) {
        sprites = new EnumMap<>(enumClass);

        for (E key : enumClass.getEnumConstants()) {
            if (!initialMap.containsKey(key)) {
                throw new RuntimeException("Error: Missing sprite: " + key);
            }
            Sprite value = initialMap.get(key);
            sprites.put(key, value);
        }
    }

    /**
     * Returns the sprite associated with the given enum key.
     *
     * @param key Enum key representing the sprite category.
     * @return The corresponding {@link Sprite} instance.
     */
    public Sprite getSprite(E key) {
        return sprites.get(key);
    }
}
