package com.tiedup.remake.client.renderer;

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Axis;
import com.tiedup.remake.blocks.ModBlocks;
import com.tiedup.remake.entities.EntityKidnapBomb;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.block.BlockRenderDispatcher;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.client.renderer.entity.EntityRendererProvider;
import net.minecraft.client.renderer.entity.TntMinecartRenderer;
import net.minecraft.client.renderer.texture.TextureAtlas;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Mth;

/**
 * Renderer for EntityKidnapBomb.
 *
 *
 * Renders the primed kidnap bomb using our custom block texture.
 */
public class KidnapBombRenderer extends EntityRenderer<EntityKidnapBomb> {

    private final BlockRenderDispatcher blockRenderer;

    public KidnapBombRenderer(EntityRendererProvider.Context context) {
        super(context);
        this.shadowRadius = 0.5F;
        this.blockRenderer = context.getBlockRenderDispatcher();
    }

    @Override
    public void render(
        EntityKidnapBomb entity,
        float entityYaw,
        float partialTicks,
        PoseStack poseStack,
        MultiBufferSource buffer,
        int packedLight
    ) {
        poseStack.pushPose();
        poseStack.translate(0.0F, 0.5F, 0.0F);

        int fuse = entity.getFuse();
        if ((float) fuse - partialTicks + 1.0F < 10.0F) {
            float scale = 1.0F - ((float) fuse - partialTicks + 1.0F) / 10.0F;
            scale = Mth.clamp(scale, 0.0F, 1.0F);
            scale *= scale;
            scale *= scale;
            float expand = 1.0F + scale * 0.3F;
            poseStack.scale(expand, expand, expand);
        }

        poseStack.mulPose(Axis.YP.rotationDegrees(-90.0F));
        poseStack.translate(-0.5F, -0.5F, 0.5F);
        poseStack.mulPose(Axis.YP.rotationDegrees(90.0F));

        // Render our custom block instead of vanilla TNT
        TntMinecartRenderer.renderWhiteSolidBlock(
            this.blockRenderer,
            ModBlocks.KIDNAP_BOMB.get().defaultBlockState(),
            poseStack,
            buffer,
            packedLight,
            (fuse / 5) % 2 == 0
        );

        poseStack.popPose();
        super.render(
            entity,
            entityYaw,
            partialTicks,
            poseStack,
            buffer,
            packedLight
        );
    }

    @Override
    @SuppressWarnings("deprecation")
    public ResourceLocation getTextureLocation(EntityKidnapBomb entity) {
        return TextureAtlas.LOCATION_BLOCKS;
    }
}
