package com.tiedup.remake.v2.client;

import java.util.List;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.block.model.ItemOverrides;
import net.minecraft.client.renderer.block.model.ItemTransforms;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.core.Direction;
import net.minecraft.util.RandomSource;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.jetbrains.annotations.Nullable;

/**
 * Wrapper around a standard {@link BakedModel} that replaces {@link #getOverrides()}
 * with a {@link DataDrivenIconOverrides} instance for NBT-based icon switching.
 *
 * <p>All other model properties (quads, ambient occlusion, transforms, particle icon)
 * are delegated to the wrapped original model. This ensures the default appearance
 * is preserved when no icon override is active.</p>
 *
 * <p>Used for both {@code tiedup:data_driven_item} and {@code tiedup:furniture_placer}
 * item models.</p>
 */
@OnlyIn(Dist.CLIENT)
public class DataDrivenIconBakedModel implements BakedModel {

    private final BakedModel original;
    private final DataDrivenIconOverrides overrides;

    /**
     * @param original  the original baked model to wrap (provides quads, transforms, etc.)
     * @param overrides the custom overrides that resolve icon models from NBT
     */
    public DataDrivenIconBakedModel(
        BakedModel original,
        DataDrivenIconOverrides overrides
    ) {
        this.original = original;
        this.overrides = overrides;
    }

    /**
     * Get the custom icon overrides. This is the key method that enables
     * per-stack model switching.
     */
    @Override
    public ItemOverrides getOverrides() {
        return overrides;
    }

    /**
     * Get the custom overrides for cache management (clearing on reload).
     */
    public DataDrivenIconOverrides getIconOverrides() {
        return overrides;
    }

    // ===== Delegated methods =====

    @Override
    public List<BakedQuad> getQuads(
        @Nullable BlockState state,
        @Nullable Direction direction,
        RandomSource random
    ) {
        return original.getQuads(state, direction, random);
    }

    @Override
    public boolean useAmbientOcclusion() {
        return original.useAmbientOcclusion();
    }

    @Override
    public boolean isGui3d() {
        return original.isGui3d();
    }

    @Override
    public boolean usesBlockLight() {
        return original.usesBlockLight();
    }

    @Override
    public boolean isCustomRenderer() {
        return original.isCustomRenderer();
    }

    @Override
    public TextureAtlasSprite getParticleIcon() {
        return original.getParticleIcon();
    }

    @SuppressWarnings("deprecation")
    @Override
    public ItemTransforms getTransforms() {
        return original.getTransforms();
    }
}
