package com.tiedup.remake.client.renderer.layers;

import com.tiedup.remake.core.TiedUpMod;
import net.minecraft.client.model.PlayerModel;
import net.minecraft.client.model.geom.EntityModelSet;
import net.minecraft.client.model.geom.ModelLayers;
import net.minecraft.client.player.AbstractClientPlayer;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

/**
 * Cache for PlayerModel instances used for clothes rendering.
 *
 * <p>Clothes textures are standard Minecraft skins (64x64) and need to be
 * rendered using PlayerModel with correct UV mappings, NOT the bondage item models.
 *
 * <p>Two models are cached:
 * <ul>
 *   <li>Normal (Steve) - 4px wide arms</li>
 *   <li>Slim (Alex) - 3px wide arms</li>
 * </ul>
 *
 * <p>Initialize via {@link #init(EntityModelSet)} during AddLayers event.
 */
@OnlyIn(Dist.CLIENT)
public class ClothesModelCache {

    private static PlayerModel<AbstractClientPlayer> normalModel;
    private static PlayerModel<AbstractClientPlayer> slimModel;
    private static boolean initialized = false;

    /**
     * Initialize the model cache.
     * Must be called during EntityRenderersEvent.AddLayers.
     *
     * @param modelSet The entity model set from the event
     */
    public static void init(EntityModelSet modelSet) {
        if (initialized) {
            return;
        }

        normalModel = new PlayerModel<>(
            modelSet.bakeLayer(ModelLayers.PLAYER),
            false
        );
        slimModel = new PlayerModel<>(
            modelSet.bakeLayer(ModelLayers.PLAYER_SLIM),
            true
        );
        initialized = true;

        TiedUpMod.LOGGER.info(
            "[ClothesModelCache] Initialized normal and slim player models for clothes rendering"
        );
    }

    /**
     * Get the appropriate player model for clothes rendering.
     *
     * @param slim true for slim (Alex) arms, false for normal (Steve) arms
     * @return The cached PlayerModel
     */
    public static PlayerModel<AbstractClientPlayer> getModel(boolean slim) {
        if (!initialized) {
            TiedUpMod.LOGGER.warn(
                "[ClothesModelCache] Not initialized! Returning null."
            );
            return null;
        }
        return slim ? slimModel : normalModel;
    }

    /**
     * Check if the cache is initialized.
     */
    public static boolean isInitialized() {
        return initialized;
    }
}
