/*
 *  Copyright 2025-2026 R3944Realms
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package top.r3944realms.eroticdungeongame.core.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.LinkedHashMap;
import java.util.Map;

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

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

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

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

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

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