package studio.halbear.hem_custom.vehicles.Renderer;

import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.vertex.IVertexBuilder;
import net.minecraft.client.renderer.IRenderTypeBuffer;
import net.minecraft.client.renderer.culling.ClippingHelper;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.client.renderer.entity.EntityRendererManager;
import net.minecraft.client.renderer.entity.model.EntityModel;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.vector.Vector3f;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import studio.halbear.hem_custom.registry.ModStatics;
import studio.halbear.hem_custom.vehicles.Entity.Aeroplane;
import studio.halbear.hem_custom.vehicles.Renderer.Models.AeroplaneModel;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.List;

@OnlyIn(Dist.CLIENT)
public class AeroplaneRenderer extends EntityRenderer<Aeroplane> {
    private AeroplaneModel model = new AeroplaneModel();

    public AeroplaneRenderer(EntityRendererManager renderManagerIn) {
        super(renderManagerIn );
    }

    @Override
    public void render(Aeroplane entity, float entityYaw, float partialTicks, MatrixStack matrixStack, IRenderTypeBuffer buffer, int packedLight) {
        matrixStack.push();

        matrixStack.rotate(Vector3f.YP.rotationDegrees(180.0F - entityYaw));
        matrixStack.scale(-1.0F, -1.0F, 1.0F);
        matrixStack.translate(0.0D, -1.5D, 0.0D);

        IVertexBuilder vertexBuilder = buffer.getBuffer(this.model.getRenderType(getEntityTexture(entity)));

        this.model.setRotationAngles(entity, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F);
        this.model.render(matrixStack, vertexBuilder, packedLight, OverlayTexture.NO_OVERLAY, 1.0F, 1.0F, 1.0F, 1.0F);

        matrixStack.pop();
        super.render(entity, entityYaw, partialTicks, matrixStack, buffer, packedLight);
    }

    @Override
    @Nonnull
    public ResourceLocation getEntityTexture(Aeroplane entity) {
        return new ResourceLocation(ModStatics.MOD_ID,"textures/entities/biplaneupdated.png");
    }

   /* @Override
    public boolean shouldRender(Aeroplane livingEntity, ClippingHelper camera, double camX, double camY, double camZ) {
       return true;
    }*/

    //i can use this to render mutliple models on one entity
    /*private List<EntityModel<Aeroplane>> modelParts = new ArrayList<>();

    public AeroplaneRenderer(EntityRendererManager renderManagerIn) {
        super(renderManagerIn );
    }

    @Override
    public void render(Aeroplane entity, float entityYaw, float partialTicks, MatrixStack matrixStack, IRenderTypeBuffer buffer, int packedLight) {

        modelParts.add(new AeroplaneModel());
        modelParts.add(new AeroplaneModel());
        modelParts.add(new AeroplaneModel());
        modelParts.add(new AeroplaneModel());

        matrixStack.push();

        matrixStack.rotate(Vector3f.YP.rotationDegrees(180.0F - entityYaw));
        matrixStack.scale(-1.0F, -1.0F, 1.0F);
        matrixStack.translate(0.0D, -1.5D, 0.0D);


        for(int i = 0; i < modelParts.toArray().length; i++) {
            IVertexBuilder vertexBuilder = buffer.getBuffer(this.modelParts.get(i).getRenderType(getEntityTexture(entity)));
            this.modelParts.get(i).setRotationAngles(entity, 0.0F, 0.0F, 0.0F, 0.0F, 0.0F);
            this.modelParts.get(i).render(matrixStack, vertexBuilder, packedLight, OverlayTexture.NO_OVERLAY, 1.0F, 1.0F, 1.0F, 1.0F);
        }
        matrixStack.pop();
        super.render(entity, entityYaw, partialTicks, matrixStack, buffer, packedLight);
    }*/
}
