package com.tiedup.remake.client.renderer;

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.math.Axis;
import com.tiedup.remake.entities.NpcFishingBobber;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.client.renderer.entity.EntityRendererProvider;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.resources.ResourceLocation;
import org.joml.Matrix3f;
import org.joml.Matrix4f;

/**
 * Renderer for NpcFishingBobber.
 *
 * Renders a textured quad using the vanilla fishing hook texture.
 * Billboard-style: always faces the camera.
 */
public class NpcFishingBobberRenderer extends EntityRenderer<NpcFishingBobber> {

    private static final ResourceLocation TEXTURE = ResourceLocation.parse(
        "textures/entity/fishing_hook.png"
    );
    private static final RenderType RENDER_TYPE = RenderType.entityCutout(
        TEXTURE
    );

    public NpcFishingBobberRenderer(EntityRendererProvider.Context ctx) {
        super(ctx);
    }

    @Override
    public void render(
        NpcFishingBobber entity,
        float yaw,
        float partialTick,
        PoseStack poseStack,
        MultiBufferSource bufferSource,
        int packedLight
    ) {
        poseStack.pushPose();
        poseStack.scale(0.5F, 0.5F, 0.5F);
        poseStack.mulPose(this.entityRenderDispatcher.cameraOrientation());
        poseStack.mulPose(Axis.YP.rotationDegrees(180.0F));

        VertexConsumer vertexConsumer = bufferSource.getBuffer(RENDER_TYPE);
        PoseStack.Pose pose = poseStack.last();
        Matrix4f matrix4f = pose.pose();
        Matrix3f matrix3f = pose.normal();

        vertex(vertexConsumer, matrix4f, matrix3f, packedLight, 0.0F, 0, 0, 1);
        vertex(vertexConsumer, matrix4f, matrix3f, packedLight, 1.0F, 0, 1, 1);
        vertex(vertexConsumer, matrix4f, matrix3f, packedLight, 1.0F, 1, 1, 0);
        vertex(vertexConsumer, matrix4f, matrix3f, packedLight, 0.0F, 1, 0, 0);

        poseStack.popPose();
        super.render(
            entity,
            yaw,
            partialTick,
            poseStack,
            bufferSource,
            packedLight
        );
    }

    private static void vertex(
        VertexConsumer consumer,
        Matrix4f matrix4f,
        Matrix3f matrix3f,
        int packedLight,
        float x,
        int y,
        int u,
        int v
    ) {
        consumer
            .vertex(matrix4f, x - 0.5F, y - 0.5F, 0.0F)
            .color(255, 255, 255, 255)
            .uv((float) u, (float) v)
            .overlayCoords(OverlayTexture.NO_OVERLAY)
            .uv2(packedLight)
            .normal(matrix3f, 0.0F, 1.0F, 0.0F)
            .endVertex();
    }

    @Override
    public ResourceLocation getTextureLocation(NpcFishingBobber entity) {
        return TEXTURE;
    }
}
