package com.zivilon.cinder_loe.client.render;

import com.zivilon.cinder_loe.client.model.ModelNex;
import com.zivilon.cinder_loe.entity.Nex;
import lotr.client.render.entity.LOTRRandomSkins;
import lotr.common.entity.LOTRRandomSkinEntity;
import lotr.common.entity.npc.LOTREntityBalrog;
import net.minecraft.client.model.ModelBase;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.entity.RenderLiving;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.ItemStack;
import net.minecraft.util.MathHelper;
import net.minecraft.util.ResourceLocation;
import org.lwjgl.opengl.GL11;

public class RenderNex extends RenderLiving {
  private static LOTRRandomSkins balrogSkins;

  private static LOTRRandomSkins balrogSkinsBright;

  private static final ResourceLocation fireTexture = new ResourceLocation("lotr:mob/balrog/fire.png");

  private ModelNex balrogModel;

  private ModelNex balrogModelBright;

  private ModelNex fireModel;

  public RenderNex() {
    super((ModelBase)new ModelNex(), 0.5F);
    this.balrogModel = (ModelNex)this.mainModel;
    this.balrogModelBright = new ModelNex(0.05F);
    this.fireModel = new ModelNex(0.0F);
    this.fireModel.setFireModel();
    balrogSkins = LOTRRandomSkins.loadSkinsList("lotr:mob/balrog/balrog");
    balrogSkinsBright = LOTRRandomSkins.loadSkinsList("lotr:mob/balrog/balrog_bright");
  }

  protected ResourceLocation getEntityTexture(Entity entity) {
    return balrogSkins.getRandomSkin((LOTRRandomSkinEntity)entity);
  }

  public void doRender(Entity entity, double d, double d1, double d2, float f, float f1) {
    Nex balrog = (Nex)entity;
    ItemStack heldItem = balrog.getHeldItem();
    this.fireModel.heldItemRight = (heldItem == null) ? 0 : 2;
    doRender((EntityLiving)balrog, d, d1, d2, f, f1);
  }

  protected void preRenderCallback(EntityLivingBase entity, float f) {
    Nex balrog = (Nex)entity;
    float scale = 2.0F;
    GL11.glScalef(scale, scale, scale);
  }

  private void setupFullBright() {
    int light = 15728880;
    int lx = light % 65536;
    int ly = light / 65536;
    OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit, lx / 1.0F, ly / 1.0F);
    GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
    GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
  }

  protected int shouldRenderPass(EntityLivingBase entity, int pass, float f) {
    Nex balrog = (Nex)entity;
    if (pass == 1) {
    float alpha;
    OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit, 240.0F, 240.0F);
    GL11.glMatrixMode(5890);
    GL11.glLoadIdentity();
    float f1 = balrog.ticksExisted + f;
    float f2 = f1 * 0.01F;
    float f3 = f1 * 0.01F;
    GL11.glTranslatef(f2, f3, 0.0F);
    GL11.glMatrixMode(5888);
    GL11.glAlphaFunc(516, 0.01F);
    GL11.glEnable(3042);
    GL11.glBlendFunc(1, 1);
    alpha = 0.3F + MathHelper.sin(f1 * 0.05F) * 0.15F;
    GL11.glColor4f(alpha, alpha, alpha, 1.0F);
    GL11.glDisable(2896);
    GL11.glDepthMask(false);
    setRenderPassModel((ModelBase)this.fireModel);
    bindTexture(fireTexture);
    return 1;
    }
    if (pass == 2) {
    GL11.glMatrixMode(5890);
    GL11.glLoadIdentity();
    GL11.glMatrixMode(5888);
    GL11.glAlphaFunc(516, 0.1F);
    GL11.glDisable(3042);
    GL11.glEnable(2896);
    GL11.glDepthMask(true);
    GL11.glDisable(2896);
    setupFullBright();
    setRenderPassModel((ModelBase)this.balrogModelBright);
    bindTexture(balrogSkinsBright.getRandomSkin((LOTRRandomSkinEntity)balrog));
    GL11.glEnable(3042);
    GL11.glBlendFunc(770, 771);
    GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
    return 1;
    }
    if (pass == 3) {
    GL11.glEnable(2896);
    GL11.glDisable(3042);
    GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
    }
    return -1;
  }

  protected void renderEquippedItems(EntityLivingBase entity, float f) {
    GL11.glColor3f(1.0F, 1.0F, 1.0F);
    ItemStack heldItem = entity.getHeldItem();
    if (heldItem != null) {
      GL11.glPushMatrix();
      this.balrogModel.body.postRender(0.0625F);
      this.balrogModel.rightArm.postRender(0.0625F);
      GL11.glTranslatef(-0.25F, 1.5F, -0.125F);
      float scale = 1.25F;
      GL11.glScalef(scale, -scale, scale);
      GL11.glRotatef(-100.0F, 1.0F, 0.0F, 0.0F);
      GL11.glRotatef(45.0F, 0.0F, 1.0F, 0.0F);
      this.renderManager.itemRenderer.renderItem(entity, heldItem, 0);
      if (heldItem.getItem().requiresMultipleRenderPasses())
        for (int x = 1; x < heldItem.getItem().getRenderPasses(heldItem.getItemDamage()); x++)
          this.renderManager.itemRenderer.renderItem(entity, heldItem, x);
      GL11.glPopMatrix();
    }
  }
}
