package client.renderer.tileentity;

import org.lwjgl.opengl.GL11;

import client.renderer.GlState;
import client.renderer.model.ModelChest;
import client.renderer.model.ModelLargeChest;
import common.block.Block;
import common.block.tech.BlockChest;
import common.tileentity.TileEntityChest;


public class TileEntityChestRenderer extends TileEntitySpecialRenderer<TileEntityChest>
{
    private static final String textureTrappedDouble = "textures/blocks/chest_trapped_double.png";
//    private static final ResourceLocation textureChristmasDouble = new ResourceLocation("textures/tile/chest_christmas_double.png");
    private static final String textureNormalDouble = "textures/blocks/chest_normal_double.png";
    private static final String textureTrapped = "textures/blocks/chest_trapped.png";
//    private static final ResourceLocation textureChristmas = new ResourceLocation("textures/tile/chest_christmas.png");
    private static final String textureNormal = "textures/blocks/chest_normal.png";
    
    private ModelChest simpleChest = new ModelChest();
    private ModelChest largeChest = new ModelLargeChest();
//    private boolean isChristmas;

//    public TileEntityChestRenderer()
//    {
//        this.setChristmas(true);
//    }
    
//    public void setChristmas(boolean xmas) {
//    	if(!xmas) {
//    		this.isChristmas = false;
//    	}
//    	else {
//	    	Calendar calendar = Calendar.getInstance();
//	        if(calendar.get(2) + 1 == 12 && calendar.get(5) >= 24 && calendar.get(5) <= 26)
//	            this.isChristmas = true;
//    	}
//    }

    public void renderTileEntityAt(TileEntityChest te, double x, double y, double z, float partialTicks, int destroyStage)
    {
        GlState.enableDepth();
        GlState.depthFunc(GL11.GL_LEQUAL);
        GlState.depthMask(true);
        int i;

        if (!te.hasWorldObj())
        {
            i = 0;
        }
        else
        {
            Block block = te.getBlockType();
            i = te.getBlockMetadata();

            if (block instanceof BlockChest && i == 0)
            {
                ((BlockChest)block).checkForSurroundingChests(te.getWorld(), te.getPos(), te.getWorld().getState(te.getPos()));
                i = te.getBlockMetadata();
            }

            te.checkForAdjacentChests();
        }

        if (te.adjacentChestZNeg == null && te.adjacentChestXNeg == null)
        {
            ModelChest modelchest;

            if (te.adjacentChestXPos == null && te.adjacentChestZPos == null)
            {
                modelchest = this.simpleChest;

                if (destroyStage >= 0)
                {
                    this.bindTexture(DESTROY_STAGES[destroyStage]);
                    GL11.glMatrixMode(GL11.GL_TEXTURE);
                    GL11.glPushMatrix();
                    GL11.glScalef(4.0F, 4.0F, 1.0F);
                    GL11.glTranslatef(0.0625F, 0.0625F, 0.0625F);
                    GL11.glMatrixMode(GL11.GL_MODELVIEW);
                }
//                else if (this.isChristmas)
//                {
//                    this.bindTexture(textureChristmas);
//                }
                else if (te.getChestType() == 1)
                {
                    this.bindTexture(textureTrapped);
                }
                else
                {
                    this.bindTexture(textureNormal);
                }
            }
            else
            {
                modelchest = this.largeChest;

                if (destroyStage >= 0)
                {
                    this.bindTexture(DESTROY_STAGES[destroyStage]);
                    GL11.glMatrixMode(GL11.GL_TEXTURE);
                    GL11.glPushMatrix();
                    GL11.glScalef(8.0F, 4.0F, 1.0F);
                    GL11.glTranslatef(0.0625F, 0.0625F, 0.0625F);
                    GL11.glMatrixMode(GL11.GL_MODELVIEW);
                }
//                else if (this.isChristmas)
//                {
//                    this.bindTexture(textureChristmasDouble);
//                }
                else if (te.getChestType() == 1)
                {
                    this.bindTexture(textureTrappedDouble);
                }
                else
                {
                    this.bindTexture(textureNormalDouble);
                }
            }

            GL11.glPushMatrix();
            GlState.enableRescaleNormal();

            if (destroyStage < 0)
            {
                GlState.color(1.0F, 1.0F, 1.0F, 1.0F);
            }

            GL11.glTranslatef((float)x, (float)y + 1.0F, (float)z + 1.0F);
            GL11.glScalef(1.0F, -1.0F, -1.0F);
            GL11.glTranslatef(0.5F, 0.5F, 0.5F);
            int j = 0;

            if (i == 2)
            {
                j = 180;
            }

            if (i == 3)
            {
                j = 0;
            }

            if (i == 4)
            {
                j = 90;
            }

            if (i == 5)
            {
                j = -90;
            }

            if (i == 2 && te.adjacentChestXPos != null)
            {
                GL11.glTranslatef(1.0F, 0.0F, 0.0F);
            }

            if (i == 5 && te.adjacentChestZPos != null)
            {
                GL11.glTranslatef(0.0F, 0.0F, -1.0F);
            }

            GL11.glRotatef((float)j, 0.0F, 1.0F, 0.0F);
            GL11.glTranslatef(-0.5F, -0.5F, -0.5F);
            float f = te.prevLidAngle + (te.lidAngle - te.prevLidAngle) * partialTicks;

            if (te.adjacentChestZNeg != null)
            {
                float f1 = te.adjacentChestZNeg.prevLidAngle + (te.adjacentChestZNeg.lidAngle - te.adjacentChestZNeg.prevLidAngle) * partialTicks;

                if (f1 > f)
                {
                    f = f1;
                }
            }

            if (te.adjacentChestXNeg != null)
            {
                float f2 = te.adjacentChestXNeg.prevLidAngle + (te.adjacentChestXNeg.lidAngle - te.adjacentChestXNeg.prevLidAngle) * partialTicks;

                if (f2 > f)
                {
                    f = f2;
                }
            }

            f = 1.0F - f;
            f = 1.0F - f * f * f;
            modelchest.chestLid.rotateAngleX = -(f * (float)Math.PI / 2.0F);
            modelchest.renderAll();
            GlState.disableRescaleNormal();
            GL11.glPopMatrix();
            GlState.color(1.0F, 1.0F, 1.0F, 1.0F);

            if (destroyStage >= 0)
            {
                GL11.glMatrixMode(GL11.GL_TEXTURE);
                GL11.glPopMatrix();
                GL11.glMatrixMode(GL11.GL_MODELVIEW);
            }
        }
    }
}
