package net.minecraft.client.renderer.entity;

import com.mojang.blaze3d.platform.GlStateManager;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.entity.layers.EnderDragonDeathLayer;
import net.minecraft.client.renderer.entity.layers.EnderDragonEyesLayer;
import net.minecraft.client.renderer.entity.model.EnderDragonModel;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.entity.boss.dragon.EnderDragonEntity;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

@OnlyIn(Dist.CLIENT)
public class EnderDragonRenderer extends MobRenderer<EnderDragonEntity, EnderDragonModel> {
   public static final ResourceLocation ENDERCRYSTAL_BEAM_TEXTURES = new ResourceLocation("textures/entity/end_crystal/end_crystal_beam.png");
   private static final ResourceLocation DRAGON_EXPLODING_TEXTURES = new ResourceLocation("textures/entity/enderdragon/dragon_exploding.png");
   private static final ResourceLocation DRAGON_TEXTURES = new ResourceLocation("textures/entity/enderdragon/dragon.png");

   public EnderDragonRenderer(EntityRendererManager renderManagerIn) {
      super(renderManagerIn, new EnderDragonModel(0.0F), 0.5F);
      this.addLayer(new EnderDragonEyesLayer(this));
      this.addLayer(new EnderDragonDeathLayer(this));
   }

   protected void applyRotations(EnderDragonEntity entityLiving, float ageInTicks, float rotationYaw, float partialTicks) {
      float f = (float)entityLiving.getMovementOffsets(7, partialTicks)[0];
      float f1 = (float)(entityLiving.getMovementOffsets(5, partialTicks)[1] - entityLiving.getMovementOffsets(10, partialTicks)[1]);
      GlStateManager.rotatef(-f, 0.0F, 1.0F, 0.0F);
      GlStateManager.rotatef(f1 * 10.0F, 1.0F, 0.0F, 0.0F);
      GlStateManager.translatef(0.0F, 0.0F, 1.0F);
      if (entityLiving.deathTime > 0) {
         float f2 = ((float)entityLiving.deathTime + partialTicks - 1.0F) / 20.0F * 1.6F;
         f2 = MathHelper.sqrt(f2);
         if (f2 > 1.0F) {
            f2 = 1.0F;
         }

         GlStateManager.rotatef(f2 * this.getDeathMaxRotation(entityLiving), 0.0F, 0.0F, 1.0F);
      }

   }

   /**
    * Renders the model in RenderLiving
    */
   protected void renderModel(EnderDragonEntity entitylivingbaseIn, float limbSwing, float limbSwingAmount, float ageInTicks, float netHeadYaw, float headPitch, float scaleFactor) {
      if (entitylivingbaseIn.deathTicks > 0) {
         float f = (float)entitylivingbaseIn.deathTicks / 200.0F;
         GlStateManager.depthFunc(515);
         GlStateManager.enableAlphaTest();
         GlStateManager.alphaFunc(516, f);
         this.bindTexture(DRAGON_EXPLODING_TEXTURES);
         this.entityModel.render(entitylivingbaseIn, limbSwing, limbSwingAmount, ageInTicks, netHeadYaw, headPitch, scaleFactor);
         GlStateManager.alphaFunc(516, 0.1F);
         GlStateManager.depthFunc(514);
      }

      this.bindEntityTexture(entitylivingbaseIn);
      this.entityModel.render(entitylivingbaseIn, limbSwing, limbSwingAmount, ageInTicks, netHeadYaw, headPitch, scaleFactor);
      if (entitylivingbaseIn.hurtTime > 0) {
         GlStateManager.depthFunc(514);
         GlStateManager.disableTexture();
         GlStateManager.enableBlend();
         GlStateManager.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA);
         GlStateManager.color4f(1.0F, 0.0F, 0.0F, 0.5F);
         this.entityModel.render(entitylivingbaseIn, limbSwing, limbSwingAmount, ageInTicks, netHeadYaw, headPitch, scaleFactor);
         GlStateManager.enableTexture();
         GlStateManager.disableBlend();
         GlStateManager.depthFunc(515);
      }

   }

   public void doRender(EnderDragonEntity entity, double x, double y, double z, float entityYaw, float partialTicks) {
      super.doRender(entity, x, y, z, entityYaw, partialTicks);
      if (entity.field_70992_bH != null) {
         this.bindTexture(ENDERCRYSTAL_BEAM_TEXTURES);
         float f = MathHelper.sin(((float)entity.field_70992_bH.ticksExisted + partialTicks) * 0.2F) / 2.0F + 0.5F;
         f = (f * f + f) * 0.2F;
         renderCrystalBeams(x, y, z, partialTicks, MathHelper.lerp((double)(1.0F - partialTicks), entity.posX, entity.prevPosX), MathHelper.lerp((double)(1.0F - partialTicks), entity.posY, entity.prevPosY), MathHelper.lerp((double)(1.0F - partialTicks), entity.posZ, entity.prevPosZ), entity.ticksExisted, entity.field_70992_bH.posX, (double)f + entity.field_70992_bH.posY, entity.field_70992_bH.posZ);
      }

   }

   public static void renderCrystalBeams(double p_188325_0_, double p_188325_2_, double p_188325_4_, float p_188325_6_, double p_188325_7_, double p_188325_9_, double p_188325_11_, int p_188325_13_, double p_188325_14_, double p_188325_16_, double p_188325_18_) {
      float f = (float)(p_188325_14_ - p_188325_7_);
      float f1 = (float)(p_188325_16_ - 1.0D - p_188325_9_);
      float f2 = (float)(p_188325_18_ - p_188325_11_);
      float f3 = MathHelper.sqrt(f * f + f2 * f2);
      float f4 = MathHelper.sqrt(f * f + f1 * f1 + f2 * f2);
      GlStateManager.pushMatrix();
      GlStateManager.translatef((float)p_188325_0_, (float)p_188325_2_ + 2.0F, (float)p_188325_4_);
      GlStateManager.rotatef((float)(-Math.atan2((double)f2, (double)f)) * (180F / (float)Math.PI) - 90.0F, 0.0F, 1.0F, 0.0F);
      GlStateManager.rotatef((float)(-Math.atan2((double)f3, (double)f1)) * (180F / (float)Math.PI) - 90.0F, 1.0F, 0.0F, 0.0F);
      Tessellator tessellator = Tessellator.getInstance();
      BufferBuilder bufferbuilder = tessellator.getBuffer();
      RenderHelper.disableStandardItemLighting();
      GlStateManager.disableCull();
      GlStateManager.shadeModel(7425);
      float f5 = 0.0F - ((float)p_188325_13_ + p_188325_6_) * 0.01F;
      float f6 = MathHelper.sqrt(f * f + f1 * f1 + f2 * f2) / 32.0F - ((float)p_188325_13_ + p_188325_6_) * 0.01F;
      bufferbuilder.begin(5, DefaultVertexFormats.POSITION_TEX_COLOR);
      int i = 8;

      for(int j = 0; j <= 8; ++j) {
         float f7 = MathHelper.sin((float)(j % 8) * ((float)Math.PI * 2F) / 8.0F) * 0.75F;
         float f8 = MathHelper.cos((float)(j % 8) * ((float)Math.PI * 2F) / 8.0F) * 0.75F;
         float f9 = (float)(j % 8) / 8.0F;
         bufferbuilder.pos((double)(f7 * 0.2F), (double)(f8 * 0.2F), 0.0D).tex((double)f9, (double)f5).color(0, 0, 0, 255).endVertex();
         bufferbuilder.pos((double)f7, (double)f8, (double)f4).tex((double)f9, (double)f6).color(255, 255, 255, 255).endVertex();
      }

      tessellator.draw();
      GlStateManager.enableCull();
      GlStateManager.shadeModel(7424);
      RenderHelper.enableStandardItemLighting();
      GlStateManager.popMatrix();
   }

   protected ResourceLocation getEntityTexture(EnderDragonEntity entity) {
      return DRAGON_TEXTURES;
   }
}