package client.renderer.layers;

import org.lwjgl.opengl.GL11;

import client.renderer.GlState;
import client.renderer.entity.RendererLivingEntity;
import client.renderer.model.ModelArmor;
import client.renderer.model.ModelBiped;
import common.entity.types.EntityLiving;
import common.item.ItemArmor;
import common.item.ItemStack;

public class LayerArmor implements LayerRenderer<EntityLiving>
{
    protected static final String ENCHANTED_ITEM_GLINT_RES = "textures/glint.png";
    
    protected ModelBiped modelLeggings;
    protected ModelBiped modelArmor;
    private final RendererLivingEntity<?> renderer;
    private float alpha = 1.0F;
    private float colorR = 1.0F;
    private float colorG = 1.0F;
    private float colorB = 1.0F;
    private boolean skipRenderGlint;
//    private static final Map<String, String> ARMOR_TEXTURE_RES_MAP = Maps.<String, String>newHashMap();

    public LayerArmor(RendererLivingEntity<?> rendererIn, int arms, int legs)
    {
        this.renderer = rendererIn;
//        this.initArmor();
        this.modelLeggings = new ModelArmor(0.5F, arms, legs);
        this.modelArmor = new ModelArmor(1.0F, arms, legs);
    }

    public void doRenderLayer(EntityLiving entitylivingbaseIn, float p_177141_2_, float p_177141_3_, float partialTicks, float p_177141_5_, float p_177141_6_, float p_177141_7_, float scale)
    {
        this.renderLayer(entitylivingbaseIn, p_177141_2_, p_177141_3_, partialTicks, p_177141_5_, p_177141_6_, p_177141_7_, scale, 4);
        this.renderLayer(entitylivingbaseIn, p_177141_2_, p_177141_3_, partialTicks, p_177141_5_, p_177141_6_, p_177141_7_, scale, 3);
        this.renderLayer(entitylivingbaseIn, p_177141_2_, p_177141_3_, partialTicks, p_177141_5_, p_177141_6_, p_177141_7_, scale, 2);
        this.renderLayer(entitylivingbaseIn, p_177141_2_, p_177141_3_, partialTicks, p_177141_5_, p_177141_6_, p_177141_7_, scale, 1);
    }

    public boolean shouldCombineTextures()
    {
        return false;
    }

    private void renderLayer(EntityLiving entitylivingbaseIn, float p_177182_2_, float p_177182_3_, float partialTicks, float p_177182_5_, float p_177182_6_, float p_177182_7_, float scale, int armorSlot)
    {
        ItemStack itemstack = this.getCurrentArmor(entitylivingbaseIn, armorSlot);

        if (itemstack != null && itemstack.getItem() instanceof ItemArmor)
        {
            ItemArmor itemarmor = (ItemArmor)itemstack.getItem();
            ModelBiped t = this.getArmorModel(armorSlot);
            t.setModelAttributes(this.renderer.getMainModel());
            t.setLivingAnimations(entitylivingbaseIn, p_177182_2_, p_177182_3_, partialTicks);
            this.setModelPartVisible(t, armorSlot);
            boolean flag = this.isSlotForLeggings(armorSlot);
            this.renderer.bindTexture(this.getArmorResource(itemarmor, flag, null));

            if (itemarmor.getArmorMaterial().canBeDyed())
            {
                int i = itemarmor.getArmorColor(itemstack);
                float f = (float)(i >> 16 & 255) / 255.0F;
                float f1 = (float)(i >> 8 & 255) / 255.0F;
                float f2 = (float)(i & 255) / 255.0F;
                GlState.color(this.colorR * f, this.colorG * f1, this.colorB * f2, this.alpha);
                t.render(entitylivingbaseIn, p_177182_2_, p_177182_3_, p_177182_5_, p_177182_6_, p_177182_7_, scale);
                this.renderer.bindTexture(this.getArmorResource(itemarmor, flag, "overlay"));
            }

//                case CHAIN:
//                case IRON:
//                case GOLD:
//                case DIAMOND:
            if(itemarmor.getArmorMaterial().hasArmor()) {
	            GlState.color(this.colorR, this.colorG, this.colorB, this.alpha);
	            t.render(entitylivingbaseIn, p_177182_2_, p_177182_3_, p_177182_5_, p_177182_6_, p_177182_7_, scale);
            }

//                default:
            if (!this.skipRenderGlint && itemstack.isItemEnchanted())
            {
                this.renderGlint(entitylivingbaseIn, t, p_177182_2_, p_177182_3_, partialTicks, p_177182_5_, p_177182_6_, p_177182_7_, scale);
            }
        }
    }

    public ItemStack getCurrentArmor(EntityLiving entitylivingbaseIn, int armorSlot)
    {
        return entitylivingbaseIn.getArmor(armorSlot - 1);
    }

    public ModelBiped getArmorModel(int armorSlot)
    {
        return this.isSlotForLeggings(armorSlot) ? this.modelLeggings : this.modelArmor;
    }

    private boolean isSlotForLeggings(int armorSlot)
    {
        return armorSlot == 2;
    }

    private void renderGlint(EntityLiving entitylivingbaseIn, ModelBiped modelbaseIn, float p_177183_3_, float p_177183_4_, float partialTicks, float p_177183_6_, float p_177183_7_, float p_177183_8_, float scale)
    {
        float f = (float)entitylivingbaseIn.ticksExisted + partialTicks;
        this.renderer.bindTexture(ENCHANTED_ITEM_GLINT_RES);
        GlState.enableBlend();
        GlState.depthFunc(GL11.GL_EQUAL);
        GlState.depthMask(false);
        float f1 = 0.5F;
        GlState.color(f1, f1, f1, 1.0F);

        for (int i = 0; i < 2; ++i)
        {
            GlState.disableLighting();
            GlState.blendFunc(GL11.GL_SRC_COLOR, GL11.GL_ONE);
            float f2 = 0.76F;
            GlState.color(0.5F * f2, 0.25F * f2, 0.8F * f2, 1.0F);
            GL11.glMatrixMode(GL11.GL_TEXTURE);
            GL11.glLoadIdentity();
            float f3 = 0.33333334F;
            GL11.glScalef(f3, f3, f3);
            GL11.glRotatef(30.0F - (float)i * 60.0F, 0.0F, 0.0F, 1.0F);
            GL11.glTranslatef(0.0F, f * (0.001F + (float)i * 0.003F) * 20.0F, 0.0F);
            GL11.glMatrixMode(GL11.GL_MODELVIEW);
            modelbaseIn.render(entitylivingbaseIn, p_177183_3_, p_177183_4_, p_177183_6_, p_177183_7_, p_177183_8_, scale);
        }

        GL11.glMatrixMode(GL11.GL_TEXTURE);
        GL11.glLoadIdentity();
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GlState.enableLighting();
        GlState.depthMask(true);
        GlState.depthFunc(GL11.GL_LEQUAL);
        GlState.disableBlend();
    }

//    private String getArmorResource(ItemArmor item, boolean legs)
//    {
//        return this.getArmorResource(item, legs, null);
//    }

    private String getArmorResource(ItemArmor item, boolean legs, String layer)
    {
        return String.format("textures/armor/%s_layer_%d%s.png", item.getArmorTexture(), legs ? 2 : 1, 
        		layer == null ? "" : ("_" + layer));
//        ResourceLocation resourcelocation = (ResourceLocation)ARMOR_TEXTURE_RES_MAP.get(s);
//
//        if (resourcelocation == null)
//        {
//            resourcelocation = new ResourceLocation(s);
//            ARMOR_TEXTURE_RES_MAP.put(s, resourcelocation);
//        }
//
//        return s;
    }

//    protected abstract void initArmor();

//    protected abstract void setModelPartVisible(T model, int armorSlot);

//    protected void initArmor()
//    {
//        this.modelLeggings = new ModelBiped(0.5F);
//        this.modelArmor = new ModelBiped(1.0F);
//    }

    protected void setModelPartVisible(ModelBiped model, int armorSlot)
    {
    	model.setVisible(false);

        switch (armorSlot)
        {
            case 1:
                model.bipedRightLeg.showModel = true;
                model.bipedLeftLeg.showModel = true;
                break;

            case 2:
                model.bipedBody.showModel = true;
                model.bipedRightLeg.showModel = true;
                model.bipedLeftLeg.showModel = true;
                break;

            case 3:
                model.bipedBody.showModel = true;
                model.bipedRightArm.showModel = true;
                model.bipedLeftArm.showModel = true;
                break;

            case 4:
                model.bipedHead.showModel = true;
                model.bipedHeadwear.showModel = true;
        }
    }

//    protected void setModelVisible(ModelBiped model)
//    {
//        model.setVisible(false);
//    }
}
