package net.minecraft.client.renderer.entity;

import net.minecraft.client.renderer.Sheets;
import net.minecraft.client.resources.model.Material;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.Mob;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.world.phys.AABB;
import net.minecraft.core.Vec3i;
import net.minecraft.client.renderer.culling.Frustum;
import net.minecraft.core.BlockPos;
import net.minecraft.world.phys.Vec3;
import net.minecraft.client.renderer.entity.layers.RenderLayer;
import net.minecraft.client.renderer.entity.layers.ShulkerHeadLayer;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.client.model.ShulkerModel;
import net.minecraft.world.entity.monster.Shulker;

public class ShulkerRenderer extends MobRenderer<Shulker, ShulkerModel<Shulker>> {
    public static final ResourceLocation DEFAULT_TEXTURE_LOCATION;
    public static final ResourceLocation[] TEXTURE_LOCATION;
    
    public ShulkerRenderer(final EntityRenderDispatcher egm) {
        super(egm, new ShulkerModel(), 0.0f);
        this.addLayer(new ShulkerHeadLayer(this));
    }
    
    public Vec3 getRenderOffset(final Shulker bch, final float float2) {
        final int integer4 = bch.getClientSideTeleportInterpolation();
        if (integer4 > 0 && bch.hasValidInterpolationPositions()) {
            final BlockPos fu5 = bch.getAttachPosition();
            final BlockPos fu6 = bch.getOldAttachPosition();
            double double7 = (integer4 - float2) / 6.0;
            double7 *= double7;
            final double double8 = (fu5.getX() - fu6.getX()) * double7;
            final double double9 = (fu5.getY() - fu6.getY()) * double7;
            final double double10 = (fu5.getZ() - fu6.getZ()) * double7;
            return new Vec3(-double8, -double9, -double10);
        }
        return super.getRenderOffset((T)bch, float2);
    }
    
    @Override
    public boolean shouldRender(final Shulker bch, final Frustum ees, final double double3, final double double4, final double double5) {
        if (super.shouldRender(bch, ees, double3, double4, double5)) {
            return true;
        }
        if (bch.getClientSideTeleportInterpolation() > 0 && bch.hasValidInterpolationPositions()) {
            final Vec3 dem10 = Vec3.atLowerCornerOf(bch.getAttachPosition());
            final Vec3 dem11 = Vec3.atLowerCornerOf(bch.getOldAttachPosition());
            if (ees.isVisible(new AABB(dem11.x, dem11.y, dem11.z, dem10.x, dem10.y, dem10.z))) {
                return true;
            }
        }
        return false;
    }
    
    @Override
    public ResourceLocation getTextureLocation(final Shulker bch) {
        if (bch.getColor() == null) {
            return ShulkerRenderer.DEFAULT_TEXTURE_LOCATION;
        }
        return ShulkerRenderer.TEXTURE_LOCATION[bch.getColor().getId()];
    }
    
    @Override
    protected void setupRotations(final Shulker bch, final PoseStack dhl, final float float3, final float float4, final float float5) {
        super.setupRotations(bch, dhl, float3, float4 + 180.0f, float5);
        dhl.translate(0.0, 0.5, 0.0);
        dhl.mulPose(bch.getAttachFace().getOpposite().getRotation());
        dhl.translate(0.0, -0.5, 0.0);
    }
    
    static {
        DEFAULT_TEXTURE_LOCATION = new ResourceLocation("textures/" + Sheets.DEFAULT_SHULKER_TEXTURE_LOCATION.texture().getPath() + ".png");
        final ResourceLocation resourceLocation;
        TEXTURE_LOCATION = Sheets.SHULKER_TEXTURE_LOCATION.stream().map(enk -> {
            new ResourceLocation("textures/" + enk.texture().getPath() + ".png");
            return resourceLocation;
        }).<ResourceLocation>toArray(ResourceLocation[]::new);
    }
}
