package com.tiedup.remake.client.model;

import java.util.List;
import net.minecraft.client.renderer.RenderType;
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.client.ChunkRenderTypeSet;
import net.minecraftforge.client.model.IDynamicBakedModel;
import net.minecraftforge.client.model.data.ModelData;
import net.minecraftforge.client.model.data.ModelProperty;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * Dynamic baked model for CellCore that supports camouflage.
 *
 * When a disguise BlockState is provided via ModelData, renders
 * the disguise block's quads instead of the default cell_core model.
 * This makes the Cell Core visually blend into surrounding walls.
 */
public class CellCoreBakedModel implements IDynamicBakedModel {

    /** @deprecated Use {@link com.tiedup.remake.blocks.entity.CellCoreBlockEntity#DISGUISE_PROPERTY} */
    @Deprecated
    public static final ModelProperty<BlockState> DISGUISE_PROPERTY =
        com.tiedup.remake.blocks.entity.CellCoreBlockEntity.DISGUISE_PROPERTY;

    private final BakedModel original;

    public CellCoreBakedModel(BakedModel original) {
        this.original = original;
    }

    @Override
    public @NotNull List<BakedQuad> getQuads(
        @Nullable BlockState state,
        @Nullable Direction side,
        @NotNull RandomSource rand,
        @NotNull ModelData modelData,
        @Nullable RenderType renderType
    ) {
        BlockState disguise = modelData.get(DISGUISE_PROPERTY);
        if (disguise != null) {
            BakedModel disguiseModel =
                net.minecraft.client.Minecraft.getInstance()
                    .getBlockRenderer()
                    .getBlockModel(disguise);
            return disguiseModel.getQuads(
                disguise,
                side,
                rand,
                ModelData.EMPTY,
                renderType
            );
        }
        return original.getQuads(
            state,
            side,
            rand,
            ModelData.EMPTY,
            renderType
        );
    }

    @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();
    }

    @Override
    public TextureAtlasSprite getParticleIcon(@NotNull ModelData modelData) {
        BlockState disguise = modelData.get(DISGUISE_PROPERTY);
        if (disguise != null) {
            BakedModel disguiseModel =
                net.minecraft.client.Minecraft.getInstance()
                    .getBlockRenderer()
                    .getBlockModel(disguise);
            return disguiseModel.getParticleIcon(ModelData.EMPTY);
        }
        return original.getParticleIcon();
    }

    @Override
    public ItemOverrides getOverrides() {
        return original.getOverrides();
    }

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

    @Override
    public @NotNull ChunkRenderTypeSet getRenderTypes(
        @NotNull BlockState state,
        @NotNull RandomSource rand,
        @NotNull ModelData data
    ) {
        BlockState disguise = data.get(DISGUISE_PROPERTY);
        if (disguise != null) {
            BakedModel disguiseModel =
                net.minecraft.client.Minecraft.getInstance()
                    .getBlockRenderer()
                    .getBlockModel(disguise);
            return disguiseModel.getRenderTypes(
                disguise,
                rand,
                ModelData.EMPTY
            );
        }
        return original.getRenderTypes(state, rand, ModelData.EMPTY);
    }
}
