package client.renderer.layers;

import java.util.List;

import org.lwjgl.opengl.GL11;

import client.Client;
import client.renderer.GlState;
import client.renderer.blockmodel.ModelGenerator;
import client.renderer.model.ModelBox;
import client.renderer.model.ModelHumanoid;
import client.renderer.model.ModelRenderer;
import client.renderer.texture.EntityTexManager;
import common.collect.Lists;
import common.entity.npc.EntityNPC;
import common.init.SpeciesRegistry.ModelType;

public class LayerExtra implements LayerRenderer<EntityNPC>
{
    private final List<ModelRenderer> body;
    private final List<ModelRenderer> head;
    private final List<ModelRenderer> larm;
    private final List<ModelRenderer> rarm;
    private final List<ModelRenderer> wing;
    private final List<ModelRenderer> halo;
    
	private ModelHumanoid model;

    public LayerExtra(List<ModelRenderer> body, List<ModelRenderer> head, List<ModelRenderer> larm, List<ModelRenderer> rarm, 
    		List<ModelRenderer> wing, List<ModelRenderer> halo)
    {
    	this.body = body;
    	this.head = head;
    	this.larm = larm;
    	this.rarm = rarm;
    	this.wing = wing;
    	this.halo = halo;
    }
    
    public void setModel(ModelHumanoid model) {
    	this.model = model;
    }
    
    public void discard() {
    	for(ModelRenderer model : this.body)
    		model.deleteDisplayList();
    	for(ModelRenderer model : this.head)
    		model.deleteDisplayList();
    	for(ModelRenderer model : this.larm)
    		model.deleteDisplayList();
    	for(ModelRenderer model : this.rarm)
    		model.deleteDisplayList();
    	for(ModelRenderer model : this.wing)
    		model.deleteDisplayList();
    	for(ModelRenderer model : this.halo)
    		model.deleteDisplayList();
    }

    public void doRenderLayer(EntityNPC entity, float p_177141_2_, float p_177141_3_, float partialTicks, float p_177141_5_, float p_177141_6_, float p_177141_7_, float scale)
    {
//        if (!entity.isInvisible())
//        {
        GlState.color(1.0F, 1.0F, 1.0F, 1.0F);
        Client.CLIENT.getTextureManager().bindTexture(EntityTexManager.getSkin(entity));
        GL11.glPushMatrix();
        
        if (entity.isSneakingVisually())
        {
        	GL11.glTranslatef(0.0F, 0.2F, 0.0F);
        }
        
//            if (entity.isChild())
//            {
//                float f = 2.0F;
//                float f1 = 1.4F;
//                SKC.glScalef(1.0F / f, 1.0F / f, 1.0F / f);
//                SKC.glTranslatef(0.0F, 24.0F * scale, 0.0F);
//            }

        GL11.glPushMatrix();
        this.model.bipedBody.postRender(0.0625F);
        for(ModelRenderer model : this.wing) {
        	if(entity.hasDualWings()) {
        		GL11.glPushMatrix();
        		GL11.glRotatef(model.rotateAngleY < 0.0f ? -10.0f : 10.0f, 0.0f, 1.0f, 0.0f);
        		GL11.glTranslatef(0.0f, 0.125f, -0.0625f);
        		model.render(0.0625F);
                GL11.glPopMatrix();
                GL11.glPushMatrix();
        		GL11.glRotatef(model.rotateAngleY < 0.0f ? 10.0f : -10.0f, 0.0f, 1.0f, 0.0f);
        		GL11.glTranslatef(model.rotateAngleY < 0.0f ? 0.0625f : -0.0625f, -0.125f, 0.0f);
        		model.render(0.0625F);
                GL11.glPopMatrix();
        	}
        	else {
        		model.render(0.0625F);
        	}
        }
        for(ModelRenderer model : this.body) {
        	model.render(0.0625F);
        }
        GL11.glPopMatrix();
        GL11.glPushMatrix();
        this.model.bipedLeftArm.postRender(0.0625F);
        this.model.slimLeftArm.postRender(0.0625F, -1.0f, 0.0f, 0.0f);
        for(ModelRenderer model : this.larm) {
        	model.render(0.0625F);
        }
        GL11.glPopMatrix();
        GL11.glPushMatrix();
        this.model.bipedRightArm.postRender(0.0625F);
        this.model.slimRightArm.postRender(0.0625F, 1.0f, 0.0f, 0.0f);
        for(ModelRenderer model : this.rarm) {
        	model.render(0.0625F);
        }
        GL11.glPopMatrix();
        GL11.glPushMatrix();
        this.model.bipedHead.postRender(0.0625F);
        for(ModelRenderer model : this.head) {
        	model.render(0.0625F);
        }
        for(ModelRenderer model : this.halo) {
    		GL11.glPushMatrix();
    		GL11.glRotatef((float)(entity.ticksExisted % (360 * 4)) * 0.25f, 0.0f, 1.0f, 0.0f);
    		model.render(0.0625F);
            GL11.glPopMatrix();
        }
        GL11.glPopMatrix();
        
        GL11.glPopMatrix();
//        }
    }

    public boolean shouldCombineTextures()
    {
        return false;
    }
    
    public static ModelRenderer addLayer(List<ModelRenderer> models, int[] texture, int w, int h, int tx, int ty, int tw, int th, 
    		float xs, float ys, float x, float y, float z, float xr, float yr, float zr) {
    	List<ModelBox> elems = ModelGenerator.bakeModel(texture, tx, ty, w, h, xs, ys, 0.0f, tw, th, 1);
    	if(!elems.isEmpty()) {
	    	ModelRenderer model = new ModelRenderer();
	    	model.addBoxes(elems);
	    	model.setRotationPoint(x, y, z);
	    	model.rotateAngleX = xr / (180F / (float)Math.PI);
	    	model.rotateAngleY = yr / (180F / (float)Math.PI);
	    	model.rotateAngleZ = zr / (180F / (float)Math.PI);
	    	models.add(model);
	    	return model;
    	}
    	return null;
    }
    
    public static LayerExtra getLayer(int[] texture, int w, int h, ModelType model) {
    	if(w < 64 || h < 20 || (model != ModelType.HUMANOID && model != ModelType.ARACHNOID && model != ModelType.DWARF && 
    			model != ModelType.HALFLING))
    		return null;
    	List<ModelRenderer> body = Lists.newArrayList();
    	List<ModelRenderer> head = Lists.newArrayList();
    	List<ModelRenderer> larm = Lists.newArrayList();
    	List<ModelRenderer> rarm = Lists.newArrayList();
    	List<ModelRenderer> wing = Lists.newArrayList();
    	List<ModelRenderer> halo = Lists.newArrayList();
    	if(h >= 64) {
    		addLayer(wing, texture, w, h, 56, 32, 8, 16, 0.0f, 0.0f, 3.5f, -4.0f, 2.0f, 0.0f, -25.0f, 0.0f);
    		addLayer(wing, texture, w, h, 56, 16, 8, 16, -8.0f, 0.0f, -3.5f, -4.0f, 2.0f, 0.0f, 25.0f, 0.0f);
    	}
    	addLayer(head, texture, w, h, 56, 0, 8, 8, 0.0f, 0.0f, 2.0f, -14.0f, -2.0f, 10.0f, 15.0f, 0.0f);
    	addLayer(head, texture, w, h, 0, 0, 8, 8, -8.0f, 0.0f, -2.0f, -14.0f, -2.0f, 10.0f, -15.0f, 0.0f);
    	addLayer(head, texture, w, h, 24, 0, 8, 8, -4.0f, 0.0f, 0.0f, -16.0f, -2.5f, 0.0f, 0.0f, 0.0f);
    	addLayer(head, texture, w, h, 32, 0, 8, 8, -4.0f, 0.0f, 0.0f, -16.0f, 2.5f, 0.0f, 180.0f, 0.0f);
    	if(h >= 64) {
	    	addLayer(head, texture, w, h, 12, 16, 8, 4, -4.0f, 0.0f, 0.0f, -12.0f, -5.0f, 0.0f, 0.0f, 0.0f);
	    	addLayer(head, texture, w, h, 36, 16, 8, 4, -4.0f, 0.0f, 0.0f, -12.0f, 5.0f, 0.0f, 180.0f, 0.0f);
	    	addLayer(head, texture, w, h, 12, 32, 8, 4, -4.0f, 0.0f, -5.0f, -12.0f, 0.0f, 0.0f, 90.0f, 0.0f);
	    	addLayer(head, texture, w, h, 36, 32, 8, 4, -4.0f, 0.0f, 5.0f, -12.0f, 0.0f, 0.0f, -90.0f, 0.0f);
	    	addLayer(head, texture, w, h, 0, 48, 4, 4, -2.0f, 0.0f, 0.0f, -9.0f, 4.95f, 0.0f, 180.0f, 0.0f);
	    	addLayer(head, texture, w, h, 60, 48, 4, 4, -2.0f, 0.0f, 0.0f, -9.0f, 5.95f, 0.0f, 180.0f, 0.0f);
	    	addLayer(halo, texture, w, h, 12, 48, 8, 1, -4.0f, 0.0f, 0.0f, -18.0f, -5.0f, 0.0f, 0.0f, 0.0f);
	    	addLayer(halo, texture, w, h, 12, 49, 8, 1, -4.0f, 0.0f, 0.0f, -18.0f, 5.0f, 0.0f, 180.0f, 0.0f);
	    	addLayer(halo, texture, w, h, 12, 50, 8, 1, -4.0f, 0.0f, -5.0f, -18.0f, 0.0f, 0.0f, 90.0f, 0.0f);
	    	addLayer(halo, texture, w, h, 12, 51, 8, 1, -4.0f, 0.0f, 5.0f, -18.0f, 0.0f, 0.0f, -90.0f, 0.0f);
    	}
		addLayer(head, texture, w, h, h >= 64 ? 44 : 36, h >= 64 ? 48 : 16, 8, 4, 0.0f, 0.0f, 0.0f, -8.0f, -4.0f, 0.0f, 30.0f, -55.0f);
    	addLayer(head, texture, w, h, h >= 64 ? 28 : 12, h >= 64 ? 48 : 16, 8, 4, -8.0f, 0.0f, 0.0f, -8.0f, -4.0f, 0.0f, -30.0f, 55.0f);
    	addLayer(head, texture, w, h, 0, 16, 4, 4, -2.0f, 0.0f, -6.5f, -7.0f, 1.5f, 15.0f, 65.0f, 0.0f);
    	addLayer(head, texture, w, h, 52, 16, 4, 4, -2.0f, 0.0f, 6.5f, -7.0f, 1.5f, 15.0f, -65.0f, 0.0f);
    	addLayer(larm, texture, w, h, h >= 64 ? 52 : 60, h >= 64 ? 32 : 16, 4, 4, -2.0f, 0.0f, 2.0f, 9.5f, 0.0f, -25.0f, 95.0f, 0.0f);
    	addLayer(rarm, texture, w, h, h >= 64 ? 0 : 56, h >= 64 ? 32 : 16, 4, 4, -2.0f, 0.0f, -2.0f, 9.5f, 0.0f, -25.0f, -95.0f, 0.0f);
    	return (body.isEmpty() && head.isEmpty() && larm.isEmpty() && rarm.isEmpty() && wing.isEmpty() && halo.isEmpty()) ? null : new LayerExtra(body, head, larm, rarm, wing, halo);
    }
}
