package top.r3944realms.eroticdungeongame.content.device;

import net.minecraft.world.entity.EntityType;
import net.minecraftforge.registries.RegistryObject;
import org.jetbrains.annotations.NotNull;
import top.r3944realms.eroticdungeongame.content.entity.SeatEntity;

import java.util.EnumMap;
import java.util.Map;

/**
 * 座位类型映射管理器
 */
@SuppressWarnings("unused")
public class SeatTypeRegistry {
    private final Map<SeatType, RegistryObject<EntityType<SeatEntity>>> entityTypeMap = new EnumMap<>(SeatType.class);
    private final Map<SeatType, EntityType<SeatEntity>> cachedEntityTypes = new EnumMap<>(SeatType.class);

    /**
     * 注册座位类型与实体类型的映射
     */
    public void register(@NotNull SeatType seatType, @NotNull RegistryObject<EntityType<SeatEntity>> entityType) {
        entityTypeMap.put(seatType, entityType);
    }

    /**
     * 获取座位类型对应的实体类型
     */
    @NotNull
    public EntityType<SeatEntity> getEntityType(@NotNull SeatType seatType) {
        // 使用缓存避免重复获取
        return cachedEntityTypes.computeIfAbsent(seatType, st -> {
            RegistryObject<EntityType<SeatEntity>> registryObject = entityTypeMap.get(st);
            if (registryObject == null) {
                throw new IllegalStateException("No entity type registered for seat type: " + st.name());
            }
            if (!registryObject.isPresent()) {
                throw new IllegalStateException("Entity type not yet registered for seat type: " + st.name());
            }
            return registryObject.get();
        });
    }

    /**
     * 检查座位类型是否已注册实体类型
     */
    public boolean isRegistered(@NotNull SeatType seatType) {
        return entityTypeMap.containsKey(seatType) && entityTypeMap.get(seatType).isPresent();
    }

    /**
     * 获取所有已注册的座位类型
     */
    public @NotNull Iterable<SeatType> getRegisteredSeatTypes() {
        return entityTypeMap.keySet();
    }

    /**
     * 清空缓存（用于开发重载）
     */
    public void clearCache() {
        cachedEntityTypes.clear();
    }
}
