package net.minecraft.client.resources.model;

import net.minecraft.client.renderer.texture.TextureAtlas;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.util.profiling.ProfilerFiller;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.world.level.block.state.BlockState;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import net.minecraft.client.color.block.BlockColors;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.client.renderer.block.BlockModelShaper;
import net.minecraft.client.renderer.texture.AtlasSet;
import net.minecraft.resources.ResourceLocation;
import java.util.Map;
import net.minecraft.server.packs.resources.SimplePreparableReloadListener;

public class ModelManager extends SimplePreparableReloadListener<ModelBakery> implements AutoCloseable {
    private Map<ResourceLocation, BakedModel> bakedRegistry;
    private AtlasSet atlases;
    private final BlockModelShaper blockModelShaper;
    private final TextureManager textureManager;
    private final BlockColors blockColors;
    private int maxMipmapLevels;
    private BakedModel missingModel;
    private Object2IntMap<BlockState> modelGroups;
    
    public ModelManager(final TextureManager eak, final BlockColors dcb, final int integer) {
        this.textureManager = eak;
        this.blockColors = dcb;
        this.maxMipmapLevels = integer;
        this.blockModelShaper = new BlockModelShaper(this);
    }
    
    public BakedModel getModel(final ModelResourceLocation ecb) {
        return this.bakedRegistry.getOrDefault(ecb, this.missingModel);
    }
    
    public BakedModel getMissingModel() {
        return this.missingModel;
    }
    
    public BlockModelShaper getBlockModelShaper() {
        return this.blockModelShaper;
    }
    
    @Override
    protected ModelBakery prepare(final ResourceManager zb, final ProfilerFiller aio) {
        aio.startTick();
        final ModelBakery ebz4 = new ModelBakery(zb, this.blockColors, aio, this.maxMipmapLevels);
        aio.endTick();
        return ebz4;
    }
    
    @Override
    protected void apply(final ModelBakery ebz, final ResourceManager zb, final ProfilerFiller aio) {
        aio.startTick();
        aio.push("upload");
        if (this.atlases != null) {
            this.atlases.close();
        }
        this.atlases = ebz.uploadTextures(this.textureManager, aio);
        this.bakedRegistry = ebz.getBakedTopLevelModels();
        this.modelGroups = ebz.getModelGroups();
        this.missingModel = this.bakedRegistry.get(ModelBakery.MISSING_MODEL_LOCATION);
        aio.popPush("cache");
        this.blockModelShaper.rebuildCache();
        aio.pop();
        aio.endTick();
    }
    
    public boolean requiresRender(final BlockState byj1, final BlockState byj2) {
        if (byj1 == byj2) {
            return false;
        }
        final int integer4 = this.modelGroups.getInt(byj1);
        if (integer4 != -1) {
            final int integer5 = this.modelGroups.getInt(byj2);
            if (integer4 == integer5) {
                final FluidState coj6 = byj1.getFluidState();
                final FluidState coj7 = byj2.getFluidState();
                return coj6 != coj7;
            }
        }
        return true;
    }
    
    public TextureAtlas getAtlas(final ResourceLocation sm) {
        return this.atlases.getAtlas(sm);
    }
    
    @Override
    public void close() {
        this.atlases.close();
    }
    
    public void updateMaxMipLevel(final int integer) {
        this.maxMipmapLevels = integer;
    }
}
