package hmggirlfront.render.drone;

import java.io.IOException;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;

import gvclib.mod_GVCLib;
import gvclib.entity.living.EntityGVCLivingBase;
import gvclib.event.gun.Layermat;
import gvclib.item.ItemGunBase;
import gvclib.render.ModelBase_Non;
import hmggirlfront.mod_GirlFront;
import hmggirlfront.entity.GFEntityID;
import hmggirlfront.entity.drone.EntityDrone;
import hmggirlfront.items.ItemDollAttachment;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.entity.Render;
import net.minecraft.client.renderer.entity.RenderLiving;
import net.minecraft.client.renderer.entity.RenderManager;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.client.resources.IResource;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import objmodel.AdvancedModelLoader;
import objmodel.IModelCustom;

@SideOnly(Side.CLIENT)
public class RenderDrone extends RenderLiving<EntityDrone> //extends RenderLiving<EntityDrone>
{
    private static final ResourceLocation drone = new ResourceLocation("hmggirlfront:textures/model/drone/doll_drone.png");
    private static final IModelCustom drone_model = AdvancedModelLoader.loadModel(new ResourceLocation("hmggirlfront:textures/model/drone/doll_drone.mqo"));
    
    
    
    public RenderDrone(RenderManager renderManagerIn)
    {
        super(renderManagerIn, new ModelBase_Non(),0.5F);
  //  	super(renderManagerIn);
        this.shadowSize = 0.5F;
    }

    public void rendermodel(EntityDrone entity, String model) {
    	IModelCustom mo = entity.model;
    	if(mo != null) {
    		mo.renderPart(model);
    	}else {
    		entity.model = AdvancedModelLoader.loadModel(new ResourceLocation(entity.getModel()));
    	}
    }
    
    /**
     * Returns the location of an entity's texture. Doesn't seem to be called unless you call Render.bindEntityTexture.
     */
    protected ResourceLocation getEntityTexture(EntityDrone entity)
    {
    	ResourceLocation dolltex = drone;
    	ResourceLocation te = new ResourceLocation(entity.getTex());
    	if(te != null) {
    		dolltex = te;
    	}
		return dolltex;
    }
    
    
    public void rendermodel_drone(EntityDrone entity, String model) {
    	IModelCustom mo = entity.model_drone;
    	if(mo != null) {
    		mo.renderPart(model);
    	}else {
    		//entity.model_drone = AdvancedModelLoader.loadModel(new ResourceLocation(entity.getModel_drone()));
    		{
        		ResourceLocation resource = new ResourceLocation(entity.getModel_drone());
        		try {
					IResource res = Minecraft.getMinecraft().getResourceManager().getResource(resource);
					if(res != null) {
	        			entity.model_drone = AdvancedModelLoader.loadModel(resource);
	        		}
				} catch (IOException e) {
					System.out.println(String.format("warning! not exist model!::::" + entity.getModel_drone()));
				}
        		
        	}
    	}
    }
    
    /**
     * Returns the location of an entity's texture. Doesn't seem to be called unless you call Render.bindEntityTexture.
     */
    protected ResourceLocation getEntityTexture_drone(EntityDrone entity)
    {
    	ResourceLocation dolltex = drone;
    	ResourceLocation te = new ResourceLocation(entity.getTex_drone());
    	if(te != null) {
    		dolltex = te;
    	}
		return dolltex;
    }
    
    
    /**
     * Actually renders the given argument. This is a synthetic bridge method, always casting down its argument and then
     * handing it off to a worker function which does the actual work. In all probabilty, the class Render is generic
     * (Render<T extends Entity>) and this method has signature public void func_76986_a(T entity, double d, double d1,
     * double d2, float f, float f1). But JAD is pre 1.5 so doe
     */
    float iii;
    public void doRender(EntityDrone entity, double x, double y, double z, float entityYaw, float partialTicks)
    {
    	{
			//doRenderShadowAndFire(entity, x, y, z, entityYaw, partialTicks);
		}
    	float limbSwing = this.F6(entity, partialTicks);
		float limbSwingAmount = this.F5(entity, partialTicks);
    	this.bindEntityTexture(entity);
    	GL11.glPushMatrix();
		GL11.glTranslatef((float) x, (float) y, (float) z);
		GL11.glRotatef(180F, 0.0F, 1.0F, 0.0F);
		GL11.glEnable(GL12.GL_RESCALE_NORMAL);
		
		
		
		if(entity.deathTicks > 0){
			GL11.glColor4f(0.6F, 0.6F, 0.6F, 1F);
		}else {
			if(entity.hurtTime > 0) {
				GL11.glColor4f(0.7F, 0.2F, 0.2F, 1F);
			}else {
				GL11.glColor4f(1F, 1F, 1F, 1F);
			}
		}
		
		GL11.glRotatef(180.0F - entity.renderYawOffset, 0.0F, 1.0F, 0.0F);
		//drone
		{
			GL11.glPushMatrix();//glstart
			//Minecraft.getMinecraft().renderEngine.bindTexture(drone);
			Minecraft.getMinecraft().renderEngine.bindTexture(getEntityTexture_drone(entity));
			this.rendermodel_drone(entity, "body");
			this.rendermodel_drone(entity, "arm_l");
			this.rendermodel_drone(entity, "arm_r");
			this.rendermodel_drone(entity, "leg");
			GL11.glPopMatrix();//glend
		}
		{
			GL11.glPushMatrix();//glstart
			GL11.glEnable(GL11.GL_BLEND);
			GL11.glDisable(GL11.GL_CULL_FACE);
			GL11.glDepthMask(false);
			GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
			GL11.glColor4f(1F, 1F, 1F, 0.2F);
			GL11.glRotatef(entity.ontick100sec * 0.01F, 0.0F, 1.0F, 0.0F);
			//drone_model.renderPart("light");
			this.rendermodel_drone(entity, "light");
			//GL11.glColor4f(1F, 1F, 1F, 1F);
			GL11.glDepthMask(true);
            GL11.glEnable(GL11.GL_CULL_FACE);
			GL11.glDisable(GL11.GL_BLEND);
			GL11.glPopMatrix();//glend
		}
		
		
		
		if(entity.tex == null) {
			entity.tex = new ResourceLocation(entity.getTex());
		}
		
		
		Minecraft.getMinecraft().renderEngine.bindTexture(entity.tex);
		
		GL11.glScalef(0.3F, 0.3F, 0.3F);
		GL11.glTranslatef(0.0F, 1.2F, 0.0F);
		GL11.glTranslatef(0F, (float)Math.sin(entity.ontick100sec * 0.1) * 0.1F, 0F);
		{
			GL11.glPushMatrix();//glstart
			this.renderlegs(entity, limbSwing, limbSwingAmount, partialTicks);
			GL11.glPopMatrix();//glend
		}
		if(entity.getMoveT() == 3 || entity.isRiding()) {
			GL11.glTranslatef(0F, -0.4F, 0.0F);
		}
		{
			GL11.glPushMatrix();//glstart
			this.renderbody(entity, limbSwing, limbSwingAmount, partialTicks);
			GL11.glPopMatrix();//glend
		}
		this.bindEntityTexture(entity);
		
		GL11.glRotatef(-(180.0F - entity.renderYawOffset), 0.0F, 1.0F, 0.0F);
		GL11.glTranslatef(0F, 1.0F, 0.0F);
		GL11.glRotatef(180.0F - entity.rotationYawHead, 0.0F, 1.0F, 0.0F);
		GL11.glRotatef(entity.rotationPitch, 1.0F, 0.0F, 0.0F);
		GL11.glTranslatef(0F, -1.0F, 0.0F);
		
		this.rendermodel(entity, "head");
		this.rendermodel(entity, "head1");
		this.rendermodel(entity, "head2");
		
		

		
		
		GL11.glColor4f(1F, 1F, 1F, 1F);
		GL11.glDisable(GL12.GL_RESCALE_NORMAL);
		GL11.glPopMatrix();
		
		//doRenderShadowAndFire(entity, x, y, z, entityYaw, partialTicks);
		//renderEntityOnFire(entity, x, y, z, partialTicks);
		
		super.doRender(entity, x, y, z, entityYaw, partialTicks);
		
    }
    
    private void renderlegs(EntityDrone entity, float limbSwing, float limbSwingAmount, float partialTicks){
    	GL11.glPushMatrix();//glstart
    	{
			float Ax = MathHelper.cos(limbSwing * 0.6662F + (float)Math.PI) * limbSwingAmount;
			GL11.glTranslatef(0.09375F, 0.4375F, 0.0F);//GL11.glTranslatef(0.15F, 0.75F, 0.0F);
			if(entity.getMoveT() == 3 || entity.isRiding()) {
				GL11.glTranslatef(0F, -0.4F, 0.0F);
				GL11.glRotatef(-90, 1.0F, 0.0F, 0.0F);
				GL11.glRotatef(20, 0.0F, 0.0F, 1.0F);
			}else {
				GL11.glRotatef(Ax * (180F / (float)Math.PI), 1.0F, 0.0F, 0.0F);
			}
			GL11.glTranslatef(-0.09375F, -0.4375F, 0.0F);
			this.rendermodel(entity, "leg_l");
		}
		GL11.glPopMatrix();//glend
		
		GL11.glPushMatrix();//glstart
		{
			float Ax =MathHelper.cos(limbSwing * 0.6662F) * limbSwingAmount;
			GL11.glTranslatef(-0.09375F, 0.4375F, 0.0F);
			if(entity.getMoveT() == 3 || entity.isRiding()) {
				GL11.glTranslatef(0F, -0.4F, 0.0F);
				GL11.glRotatef(-90, 1.0F, 0.0F, 0.0F);
				GL11.glRotatef(-20, 0.0F, 0.0F, 1.0F);
			}else {
				GL11.glRotatef(Ax * (180F / (float)Math.PI), 1.0F, 0.0F, 0.0F);
			}
			GL11.glTranslatef(0.09375F, -0.4375F, 0.0F);
			this.rendermodel(entity, "leg_r");
		}
		GL11.glPopMatrix();//glend
	}
    
    private void renderbody(EntityDrone entity, float limbSwing, float limbSwingAmount, float partialTicks){

    	GL11.glPushMatrix();//glstart
    	this.rendermodel(entity, "body");
		this.rendermodel(entity, "body1");
		this.rendermodel(entity, "mantle");
		this.rendermodel(entity, "suka-to");
		GL11.glPopMatrix();//glend
		
		
		 float arm_l_rotex = 0;
	     float arm_l_rotez = 0;
	     float arm_l_rotey = 0;
	     float arm_r_rotex = 0;
	     float arm_r_rotez = 0;
	     float arm_r_rotey = 0;
    	GL11.glPushMatrix();//glstart
    	{
    		GL11.glTranslatef(0.25F, 0.925F, 0.0F);
			float Ax = MathHelper.cos(limbSwing * 0.6662F) * limbSwingAmount;
			//arm_l_rotex = (MathHelper.sin(partialTicks * 0.067F) * 0.05F) * (180F / (float)Math.PI);
			//arm_l_rotex = Ax * (180F / (float)Math.PI) - 20;
			if(entity.getMoveT() == 3 || entity.isRiding()) {
				arm_l_rotex = -30;
				arm_l_rotez = -30;
			}
			else{
				arm_l_rotez = 30;
			}
			GL11.glRotatef(arm_l_rotex, 1.0F, 0.0F, 0.0F);
			GL11.glRotatef(arm_l_rotey, 0.0F, 1.0F, 0.0F);
			GL11.glRotatef(arm_l_rotez, 0.0F, 0.0F, 1.0F);
			
			GL11.glTranslatef(-0.25F, -0.925F, 0.0F);
			this.rendermodel(entity, "arm_l");
			this.rendermodel(entity, "hand_l");
			
		}
		GL11.glPopMatrix();//glend
		this.bindEntityTexture(entity);
		
		
		GL11.glPushMatrix();//glstart
		{
			GL11.glTranslatef(-0.25F, 0.925F, 0.0F);
			float Ax = MathHelper.cos(limbSwing * 0.6662F) * limbSwingAmount;
			//arm_l_rotex = (MathHelper.sin(partialTicks * 0.067F) * 0.05F) * (180F / (float)Math.PI);
			//arm_l_rotex = Ax * (180F / (float)Math.PI) - 20;
			if(entity.getMoveT() == 3 || entity.isRiding()) {
				arm_r_rotex = -30;
				arm_r_rotez = 30;
			}
			else{
				arm_r_rotez = -30;
			}
			GL11.glRotatef(arm_r_rotex, 1.0F, 0.0F, 0.0F);
			GL11.glRotatef(arm_r_rotey, 0.0F, 1.0F, 0.0F);
			GL11.glRotatef(arm_r_rotez, 0.0F, 0.0F, 1.0F);
			
			GL11.glTranslatef(0.25F, -0.925F, 0.0F);
			this.rendermodel(entity, "arm_r");
			this.rendermodel(entity, "hand_r");
			
			
				
		}
		GL11.glPopMatrix();//glend
		this.bindEntityTexture(entity);
	}
    
    
    
    public float F6(EntityLivingBase entity, float partialTicks){
 		float f6 = 0;
 		if (!entity.isRiding())
        {
            f6 = entity.limbSwing - entity.limbSwingAmount * (1.0F - partialTicks);
            if (entity.isChild())
            {
                f6 *= 3.0F;
            }
        }
 		return f6;
 	}
 	public float F5(EntityLivingBase entity, float partialTicks){
 		float f5 = 0;
 		if (!entity.isRiding())
        {
            f5 = entity.prevLimbSwingAmount + (entity.limbSwingAmount - entity.prevLimbSwingAmount) * partialTicks;
            if (f5 > 1.0F)
            {
                f5 = 1.0F;
            }
        }
 		return f5;
 	}
 	
 	/**
     * Returns a rotation angle that is inbetween two other rotation angles. par1 and par2 are the angles between which
     * to interpolate, par3 is probably a float between 0.0 and 1.0 that tells us where "between" the two angles we are.
     * Example: par1 = 30, par2 = 50, par3 = 0.5, then return = 40
     */
    protected float interpolateRotation(float prevYawOffset, float yawOffset, float partialTicks)
    {
        float f;

        for (f = yawOffset - prevYawOffset; f < -180.0F; f += 360.0F)
        {
            ;
        }

        while (f >= 180.0F)
        {
            f -= 360.0F;
        }

        return prevYawOffset + partialTicks * f;
    }
    
    protected void renderLivingAt(EntityDrone entityLivingBaseIn, double x, double y, double z)
    {
        GlStateManager.translate((float)x, (float)y, (float)z);
    }
    protected float handleRotationFloat(EntityDrone livingBase, float partialTicks)
    {
        return (float)livingBase.ticksExisted + partialTicks;
    }
    
    private void renderEntityOnFire(Entity entity, double x, double y, double z, float partialTicks)
    {
        GlStateManager.disableLighting();
        TextureMap texturemap = Minecraft.getMinecraft().getTextureMapBlocks();
        TextureAtlasSprite textureatlassprite = texturemap.getAtlasSprite("minecraft:blocks/fire_layer_0");
        TextureAtlasSprite textureatlassprite1 = texturemap.getAtlasSprite("minecraft:blocks/fire_layer_1");
        GlStateManager.pushMatrix();
        GlStateManager.translate((float)x, (float)y, (float)z);
        float f = entity.width * 1.4F;
        GlStateManager.scale(f, f, f);
        Tessellator tessellator = Tessellator.getInstance();
        BufferBuilder bufferbuilder = tessellator.getBuffer();
        float f1 = 0.5F;
        float f2 = 0.0F;
        float f3 = entity.height / f;
        float f4 = (float)(entity.posY - entity.getEntityBoundingBox().minY);
        GlStateManager.rotate(-this.renderManager.playerViewY, 0.0F, 1.0F, 0.0F);
        GlStateManager.translate(0.0F, 0.0F, -0.3F + (float)((int)f3) * 0.02F);
        GlStateManager.color(1.0F, 1.0F, 1.0F, 1.0F);
        float f5 = 0.0F;
        int i = 0;
        bufferbuilder.begin(7, DefaultVertexFormats.POSITION_TEX);

        while (f3 > 0.0F)
        {
            TextureAtlasSprite textureatlassprite2 = i % 2 == 0 ? textureatlassprite : textureatlassprite1;
            this.bindTexture(TextureMap.LOCATION_BLOCKS_TEXTURE);
            float f6 = textureatlassprite2.getMinU();
            float f7 = textureatlassprite2.getMinV();
            float f8 = textureatlassprite2.getMaxU();
            float f9 = textureatlassprite2.getMaxV();

            if (i / 2 % 2 == 0)
            {
                float f10 = f8;
                f8 = f6;
                f6 = f10;
            }

            bufferbuilder.pos((double)(f1 - 0.0F), (double)(0.0F - f4), (double)f5).tex((double)f8, (double)f9).endVertex();
            bufferbuilder.pos((double)(-f1 - 0.0F), (double)(0.0F - f4), (double)f5).tex((double)f6, (double)f9).endVertex();
            bufferbuilder.pos((double)(-f1 - 0.0F), (double)(1.4F - f4), (double)f5).tex((double)f6, (double)f7).endVertex();
            bufferbuilder.pos((double)(f1 - 0.0F), (double)(1.4F - f4), (double)f5).tex((double)f8, (double)f7).endVertex();
            f3 -= 0.45F;
            f4 -= 0.45F;
            f1 *= 0.9F;
            f5 += 0.03F;
            ++i;
        }

        tessellator.draw();
        GlStateManager.popMatrix();
        GlStateManager.enableLighting();
    }
}