package net.minecraft.client.renderer.entity;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import net.minecraft.block.Block;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.model.ModelChicken;
import net.minecraft.client.model.ModelCow;
import net.minecraft.client.model.ModelHorse;
import net.minecraft.client.model.ModelOcelot;
import net.minecraft.client.model.ModelPig;
import net.minecraft.client.model.ModelSheep1;
import net.minecraft.client.model.ModelSheep2;
import net.minecraft.client.model.ModelSlime;
import net.minecraft.client.model.ModelSquid;
import net.minecraft.client.model.ModelWolf;
import net.minecraft.client.model.ModelZombie;
import net.minecraft.client.renderer.ItemRenderer;
import net.minecraft.client.renderer.OpenGlHelper;
import net.minecraft.client.renderer.RenderGlobal;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.client.renderer.texture.TextureManager;
import net.minecraft.client.renderer.tileentity.RenderEnderCrystal;
import net.minecraft.client.renderer.tileentity.RenderItemFrame;
import net.minecraft.client.renderer.tileentity.RenderWitherSkull;
import net.minecraft.client.settings.GameSettings;
import net.minecraft.crash.CrashReport;
import net.minecraft.crash.CrashReportCategory;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLeashKnot;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.ai.EntityMinecartMobSpawner;
import net.minecraft.entity.boss.EntityDragon;
import net.minecraft.entity.boss.EntityWither;
import net.minecraft.entity.effect.EntityLightningBolt;
import net.minecraft.entity.item.EntityBoat;
import net.minecraft.entity.item.EntityEnderCrystal;
import net.minecraft.entity.item.EntityEnderEye;
import net.minecraft.entity.item.EntityEnderPearl;
import net.minecraft.entity.item.EntityExpBottle;
import net.minecraft.entity.item.EntityFallingBlock;
import net.minecraft.entity.item.EntityFireworkRocket;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.item.EntityItemFrame;
import net.minecraft.entity.item.EntityMinecart;
import net.minecraft.entity.item.EntityMinecartTNT;
import net.minecraft.entity.item.EntityPainting;
import net.minecraft.entity.item.EntityTNTPrimed;
import net.minecraft.entity.item.EntityXPOrb;
import net.minecraft.entity.monster.EntityBlaze;
import net.minecraft.entity.monster.EntityCaveSpider;
import net.minecraft.entity.monster.EntityCreeper;
import net.minecraft.entity.monster.EntityEnderman;
import net.minecraft.entity.monster.EntityGhast;
import net.minecraft.entity.monster.EntityGiantZombie;
import net.minecraft.entity.monster.EntityIronGolem;
import net.minecraft.entity.monster.EntityMagmaCube;
import net.minecraft.entity.monster.EntitySilverfish;
import net.minecraft.entity.monster.EntitySkeleton;
import net.minecraft.entity.monster.EntitySlime;
import net.minecraft.entity.monster.EntitySnowman;
import net.minecraft.entity.monster.EntitySpider;
import net.minecraft.entity.monster.EntityWitch;
import net.minecraft.entity.monster.EntityZombie;
import net.minecraft.entity.passive.EntityBat;
import net.minecraft.entity.passive.EntityChicken;
import net.minecraft.entity.passive.EntityCow;
import net.minecraft.entity.passive.EntityHorse;
import net.minecraft.entity.passive.EntityMooshroom;
import net.minecraft.entity.passive.EntityOcelot;
import net.minecraft.entity.passive.EntityPig;
import net.minecraft.entity.passive.EntitySheep;
import net.minecraft.entity.passive.EntitySquid;
import net.minecraft.entity.passive.EntityVillager;
import net.minecraft.entity.passive.EntityWolf;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityArrow;
import net.minecraft.entity.projectile.EntityEgg;
import net.minecraft.entity.projectile.EntityFishHook;
import net.minecraft.entity.projectile.EntityLargeFireball;
import net.minecraft.entity.projectile.EntityPotion;
import net.minecraft.entity.projectile.EntitySmallFireball;
import net.minecraft.entity.projectile.EntitySnowball;
import net.minecraft.entity.projectile.EntityWitherSkull;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.MathHelper;
import net.minecraft.util.ReportedException;
import net.minecraft.world.World;
import org.lwjgl.opengl.GL11;

@SideOnly(Side.CLIENT)
public class RenderManager
{
	public Map entityRenderMap = new HashMap();
	public static RenderManager instance = new RenderManager();
	private FontRenderer fontRenderer;
	public static double renderPosX;
	public static double renderPosY;
	public static double renderPosZ;
	public TextureManager renderEngine;
	public ItemRenderer itemRenderer;
	public World worldObj;
	public EntityLivingBase livingPlayer;
	public Entity field_147941_i;
	public float playerViewY;
	public float playerViewX;
	public GameSettings options;
	public double viewerPosX;
	public double viewerPosY;
	public double viewerPosZ;
	public static boolean debugBoundingBox;
	private static final String __OBFID = "CL_00000991";

	private RenderManager()
	{
		this.entityRenderMap.put(EntityCaveSpider.class, new RenderCaveSpider());
		this.entityRenderMap.put(EntitySpider.class, new RenderSpider());
		this.entityRenderMap.put(EntityPig.class, new RenderPig(new ModelPig(), new ModelPig(0.5F), 0.7F));
		this.entityRenderMap.put(EntitySheep.class, new RenderSheep(new ModelSheep2(), new ModelSheep1(), 0.7F));
		this.entityRenderMap.put(EntityCow.class, new RenderCow(new ModelCow(), 0.7F));
		this.entityRenderMap.put(EntityMooshroom.class, new RenderMooshroom(new ModelCow(), 0.7F));
		this.entityRenderMap.put(EntityWolf.class, new RenderWolf(new ModelWolf(), new ModelWolf(), 0.5F));
		this.entityRenderMap.put(EntityChicken.class, new RenderChicken(new ModelChicken(), 0.3F));
		this.entityRenderMap.put(EntityOcelot.class, new RenderOcelot(new ModelOcelot(), 0.4F));
		this.entityRenderMap.put(EntitySilverfish.class, new RenderSilverfish());
		this.entityRenderMap.put(EntityCreeper.class, new RenderCreeper());
		this.entityRenderMap.put(EntityEnderman.class, new RenderEnderman());
		this.entityRenderMap.put(EntitySnowman.class, new RenderSnowMan());
		this.entityRenderMap.put(EntitySkeleton.class, new RenderSkeleton());
		this.entityRenderMap.put(EntityWitch.class, new RenderWitch());
		this.entityRenderMap.put(EntityBlaze.class, new RenderBlaze());
		this.entityRenderMap.put(EntityZombie.class, new RenderZombie());
		this.entityRenderMap.put(EntitySlime.class, new RenderSlime(new ModelSlime(16), new ModelSlime(0), 0.25F));
		this.entityRenderMap.put(EntityMagmaCube.class, new RenderMagmaCube());
		this.entityRenderMap.put(EntityPlayer.class, new RenderPlayer());
		this.entityRenderMap.put(EntityGiantZombie.class, new RenderGiantZombie(new ModelZombie(), 0.5F, 6.0F));
		this.entityRenderMap.put(EntityGhast.class, new RenderGhast());
		this.entityRenderMap.put(EntitySquid.class, new RenderSquid(new ModelSquid(), 0.7F));
		this.entityRenderMap.put(EntityVillager.class, new RenderVillager());
		this.entityRenderMap.put(EntityIronGolem.class, new RenderIronGolem());
		this.entityRenderMap.put(EntityBat.class, new RenderBat());
		this.entityRenderMap.put(EntityDragon.class, new RenderDragon());
		this.entityRenderMap.put(EntityEnderCrystal.class, new RenderEnderCrystal());
		this.entityRenderMap.put(EntityWither.class, new RenderWither());
		this.entityRenderMap.put(Entity.class, new RenderEntity());
		this.entityRenderMap.put(EntityPainting.class, new RenderPainting());
		this.entityRenderMap.put(EntityItemFrame.class, new RenderItemFrame());
		this.entityRenderMap.put(EntityLeashKnot.class, new RenderLeashKnot());
		this.entityRenderMap.put(EntityArrow.class, new RenderArrow());
		this.entityRenderMap.put(EntitySnowball.class, new RenderSnowball(Items.snowball));
		this.entityRenderMap.put(EntityEnderPearl.class, new RenderSnowball(Items.ender_pearl));
		this.entityRenderMap.put(EntityEnderEye.class, new RenderSnowball(Items.ender_eye));
		this.entityRenderMap.put(EntityEgg.class, new RenderSnowball(Items.egg));
		this.entityRenderMap.put(EntityPotion.class, new RenderSnowball(Items.potionitem, 16384));
		this.entityRenderMap.put(EntityExpBottle.class, new RenderSnowball(Items.experience_bottle));
		this.entityRenderMap.put(EntityFireworkRocket.class, new RenderSnowball(Items.fireworks));
		this.entityRenderMap.put(EntityLargeFireball.class, new RenderFireball(2.0F));
		this.entityRenderMap.put(EntitySmallFireball.class, new RenderFireball(0.5F));
		this.entityRenderMap.put(EntityWitherSkull.class, new RenderWitherSkull());
		this.entityRenderMap.put(EntityItem.class, new RenderItem());
		this.entityRenderMap.put(EntityXPOrb.class, new RenderXPOrb());
		this.entityRenderMap.put(EntityTNTPrimed.class, new RenderTNTPrimed());
		this.entityRenderMap.put(EntityFallingBlock.class, new RenderFallingBlock());
		this.entityRenderMap.put(EntityMinecartTNT.class, new RenderTntMinecart());
		this.entityRenderMap.put(EntityMinecartMobSpawner.class, new RenderMinecartMobSpawner());
		this.entityRenderMap.put(EntityMinecart.class, new RenderMinecart());
		this.entityRenderMap.put(EntityBoat.class, new RenderBoat());
		this.entityRenderMap.put(EntityFishHook.class, new RenderFish());
		this.entityRenderMap.put(EntityHorse.class, new RenderHorse(new ModelHorse(), 0.75F));
		this.entityRenderMap.put(EntityLightningBolt.class, new RenderLightningBolt());
		Iterator iterator = this.entityRenderMap.values().iterator();

		while (iterator.hasNext())
		{
			Render render = (Render)iterator.next();
			render.setRenderManager(this);
		}
	}

	public Render getEntityClassRenderObject(Class par1Class)
	{
		Render render = (Render)this.entityRenderMap.get(par1Class);

		if (render == null && par1Class != Entity.class)
		{
			render = this.getEntityClassRenderObject(par1Class.getSuperclass());
			this.entityRenderMap.put(par1Class, render);
		}

		return render;
	}

	public Render getEntityRenderObject(Entity par1Entity)
	{
		return this.getEntityClassRenderObject(par1Entity.getClass());
	}

	public void cacheActiveRenderInfo(World p_147938_1_, TextureManager p_147938_2_, FontRenderer p_147938_3_, EntityLivingBase p_147938_4_, Entity p_147938_5_, GameSettings p_147938_6_, float p_147938_7_)
	{
		this.worldObj = p_147938_1_;
		this.renderEngine = p_147938_2_;
		this.options = p_147938_6_;
		this.livingPlayer = p_147938_4_;
		this.field_147941_i = p_147938_5_;
		this.fontRenderer = p_147938_3_;

		if (p_147938_4_.isPlayerSleeping())
		{
			Block block = p_147938_1_.getBlock(MathHelper.floor_double(p_147938_4_.posX), MathHelper.floor_double(p_147938_4_.posY), MathHelper.floor_double(p_147938_4_.posZ));
			int x = MathHelper.floor_double(p_147938_4_.posX);
			int y = MathHelper.floor_double(p_147938_4_.posY);
			int z = MathHelper.floor_double(p_147938_4_.posZ);

			if (block.isBed(p_147938_1_, x, y, z, p_147938_4_))
			{
				int j = block.getBedDirection(p_147938_1_, x, y, z);
				this.playerViewY = (float)(j * 90 + 180);
				this.playerViewX = 0.0F;
			}
		}
		else
		{
			this.playerViewY = p_147938_4_.prevRotationYaw + (p_147938_4_.rotationYaw - p_147938_4_.prevRotationYaw) * p_147938_7_;
			this.playerViewX = p_147938_4_.prevRotationPitch + (p_147938_4_.rotationPitch - p_147938_4_.prevRotationPitch) * p_147938_7_;
		}

		if (p_147938_6_.thirdPersonView == 2)
		{
			this.playerViewY += 180.0F;
		}

		this.viewerPosX = p_147938_4_.lastTickPosX + (p_147938_4_.posX - p_147938_4_.lastTickPosX) * (double)p_147938_7_;
		this.viewerPosY = p_147938_4_.lastTickPosY + (p_147938_4_.posY - p_147938_4_.lastTickPosY) * (double)p_147938_7_;
		this.viewerPosZ = p_147938_4_.lastTickPosZ + (p_147938_4_.posZ - p_147938_4_.lastTickPosZ) * (double)p_147938_7_;
	}

	public boolean renderEntitySimple(Entity p_147937_1_, float p_147937_2_)
	{
		return this.renderEntityStatic(p_147937_1_, p_147937_2_, false);
	}

	public boolean renderEntityStatic(Entity p_147936_1_, float p_147936_2_, boolean p_147936_3_)
	{
		if (p_147936_1_.ticksExisted == 0)
		{
			p_147936_1_.lastTickPosX = p_147936_1_.posX;
			p_147936_1_.lastTickPosY = p_147936_1_.posY;
			p_147936_1_.lastTickPosZ = p_147936_1_.posZ;
		}

		double d0 = p_147936_1_.lastTickPosX + (p_147936_1_.posX - p_147936_1_.lastTickPosX) * (double)p_147936_2_;
		double d1 = p_147936_1_.lastTickPosY + (p_147936_1_.posY - p_147936_1_.lastTickPosY) * (double)p_147936_2_;
		double d2 = p_147936_1_.lastTickPosZ + (p_147936_1_.posZ - p_147936_1_.lastTickPosZ) * (double)p_147936_2_;
		float f1 = p_147936_1_.prevRotationYaw + (p_147936_1_.rotationYaw - p_147936_1_.prevRotationYaw) * p_147936_2_;
		int i = p_147936_1_.getBrightnessForRender(p_147936_2_);

		if (p_147936_1_.isBurning())
		{
			i = 15728880;
		}

		int j = i % 65536;
		int k = i / 65536;
		OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit, (float)j / 1.0F, (float)k / 1.0F);
		GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);
		return this.func_147939_a(p_147936_1_, d0 - renderPosX, d1 - renderPosY, d2 - renderPosZ, f1, p_147936_2_, p_147936_3_);
	}

	public boolean renderEntityWithPosYaw(Entity p_147940_1_, double p_147940_2_, double p_147940_4_, double p_147940_6_, float p_147940_8_, float p_147940_9_)
	{
		return this.func_147939_a(p_147940_1_, p_147940_2_, p_147940_4_, p_147940_6_, p_147940_8_, p_147940_9_, false);
	}

	public boolean func_147939_a(Entity p_147939_1_, double p_147939_2_, double p_147939_4_, double p_147939_6_, float p_147939_8_, float p_147939_9_, boolean p_147939_10_)
	{
		Render render = null;

		try
		{
			render = this.getEntityRenderObject(p_147939_1_);

			if (render != null && this.renderEngine != null)
			{
				if (!render.isStaticEntity() || p_147939_10_)
				{
					try
					{
						render.doRender(p_147939_1_, p_147939_2_, p_147939_4_, p_147939_6_, p_147939_8_, p_147939_9_);
					}
					catch (Throwable throwable2)
					{
						throw new ReportedException(CrashReport.makeCrashReport(throwable2, "Rendering entity in world"));
					}

					try
					{
						render.doRenderShadowAndFire(p_147939_1_, p_147939_2_, p_147939_4_, p_147939_6_, p_147939_8_, p_147939_9_);
					}
					catch (Throwable throwable1)
					{
						throw new ReportedException(CrashReport.makeCrashReport(throwable1, "Post-rendering entity in world"));
					}

					if (debugBoundingBox && !p_147939_1_.isInvisible() && !p_147939_10_)
					{
						try
						{
							this.renderDebugBoundingBox(p_147939_1_, p_147939_2_, p_147939_4_, p_147939_6_, p_147939_8_, p_147939_9_);
						}
						catch (Throwable throwable)
						{
							throw new ReportedException(CrashReport.makeCrashReport(throwable, "Rendering entity hitbox in world"));
						}
					}
				}
			}
			else if (this.renderEngine != null)
			{
				return false;
			}

			return true;
		}
		catch (Throwable throwable3)
		{
			CrashReport crashreport = CrashReport.makeCrashReport(throwable3, "Rendering entity in world");
			CrashReportCategory crashreportcategory = crashreport.makeCategory("Entity being rendered");
			p_147939_1_.addEntityCrashInfo(crashreportcategory);
			CrashReportCategory crashreportcategory1 = crashreport.makeCategory("Renderer details");
			crashreportcategory1.addCrashSection("Assigned renderer", render);
			crashreportcategory1.addCrashSection("Location", CrashReportCategory.func_85074_a(p_147939_2_, p_147939_4_, p_147939_6_));
			crashreportcategory1.addCrashSection("Rotation", Float.valueOf(p_147939_8_));
			crashreportcategory1.addCrashSection("Delta", Float.valueOf(p_147939_9_));
			throw new ReportedException(crashreport);
		}
	}

	private void renderDebugBoundingBox(Entity par1Entity, double par2, double par4, double par6, float par8, float par9)
	{
		GL11.glDepthMask(false);
		GL11.glDisable(GL11.GL_TEXTURE_2D);
		GL11.glDisable(GL11.GL_LIGHTING);
		GL11.glDisable(GL11.GL_CULL_FACE);
		GL11.glDisable(GL11.GL_BLEND);
		float f2 = par1Entity.width / 2.0F;
		AxisAlignedBB axisalignedbb = AxisAlignedBB.getAABBPool().getAABB(par2 - (double)f2, par4, par6 - (double)f2, par2 + (double)f2, par4 + (double)par1Entity.height, par6 + (double)f2);
		RenderGlobal.drawOutlinedBoundingBox(axisalignedbb, 16777215);
		GL11.glEnable(GL11.GL_TEXTURE_2D);
		GL11.glEnable(GL11.GL_LIGHTING);
		GL11.glEnable(GL11.GL_CULL_FACE);
		GL11.glDisable(GL11.GL_BLEND);
		GL11.glDepthMask(true);
	}

	public void set(World par1World)
	{
		this.worldObj = par1World;
	}

	public double getDistanceToCamera(double par1, double par3, double par5)
	{
		double d3 = par1 - this.viewerPosX;
		double d4 = par3 - this.viewerPosY;
		double d5 = par5 - this.viewerPosZ;
		return d3 * d3 + d4 * d4 + d5 * d5;
	}

	public FontRenderer getFontRenderer()
	{
		return this.fontRenderer;
	}

	public void updateIcons(IIconRegister par1IconRegister)
	{
		Iterator iterator = this.entityRenderMap.values().iterator();

		while (iterator.hasNext())
		{
			Render render = (Render)iterator.next();
			render.updateIcons(par1IconRegister);
		}
	}
}