package client.renderer.tileentity;

import org.lwjgl.opengl.GL11;

import client.Client;
import client.renderer.BlockRenderer;
import client.renderer.DefaultVertexFormats;
import client.renderer.GlState;
import client.renderer.ItemRenderer;
import client.renderer.RenderBuffer;
import client.renderer.Tessellator;
import client.renderer.texture.TextureMap;
import common.block.Block;
import common.block.tech.BlockPistonBase;
import common.block.tech.BlockPistonHead;
import common.init.Blocks;
import common.tileentity.TileEntityPiston;
import common.util.BlockPos;
import common.world.State;
import common.world.World;

public class TileEntityPistonRenderer extends TileEntitySpecialRenderer<TileEntityPiston>
{
    private final BlockRenderer blockRenderer = Client.CLIENT.getBlockRendererDispatcher();

    public void renderTileEntityAt(TileEntityPiston te, double x, double y, double z, float partialTicks, int destroyStage)
    {
        BlockPos blockpos = te.getPos();
        State iblockstate = te.getPistonState();
        Block block = iblockstate.getBlock();

        if (block != Blocks.air && te.getProgress(partialTicks) < 1.0F)
        {
//            Tessellator tessellator = Tessellator.getInstance();
            RenderBuffer worldrenderer = Tessellator.getBuffer();
            this.bindTexture(TextureMap.locationBlocksTexture);
            ItemRenderer.disableStandardItemLighting();
            GlState.blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
            GlState.enableBlend();
            GlState.disableCull();

//            if (Game.getGame().isAmbientOcclusionEnabled())
//            {
//                GlState.shadeModel(GL11.GL_SMOOTH);
//            }
//            else
//            {
            GlState.shadeModel(GL11.GL_FLAT);
//            }

            worldrenderer.begin(GL11.GL_QUADS, DefaultVertexFormats.BLOCK);
            worldrenderer.setTranslation((double)((float)x - (float)blockpos.getX() + te.getOffsetX(partialTicks)), (double)((float)y - (float)blockpos.getY() + te.getOffsetY(partialTicks)), (double)((float)z - (float)blockpos.getZ() + te.getOffsetZ(partialTicks)));
            World world = this.getWorld();

            if (block == Blocks.piston_head && te.getProgress(partialTicks) < 0.5F)
            {
                iblockstate = iblockstate.withProperty(BlockPistonHead.SHORT, Boolean.valueOf(true));
                this.blockRenderer.renderModel(world, this.blockRenderer.getModelFromBlockState(iblockstate, world, blockpos), iblockstate, blockpos, worldrenderer, true);
            }
            else if (te.shouldPistonHeadBeRendered() && !te.isExtending())
            {
                BlockPistonHead.EnumPistonType blockpistonextension$enumpistontype = block == Blocks.sticky_piston ? BlockPistonHead.EnumPistonType.STICKY : BlockPistonHead.EnumPistonType.DEFAULT;
                State iblockstate1 = Blocks.piston_head.getState().withProperty(BlockPistonHead.TYPE, blockpistonextension$enumpistontype).withProperty(BlockPistonHead.FACING, iblockstate.getValue(BlockPistonBase.FACING));
                iblockstate1 = iblockstate1.withProperty(BlockPistonHead.SHORT, Boolean.valueOf(te.getProgress(partialTicks) >= 0.5F));
                this.blockRenderer.renderModel(world, this.blockRenderer.getModelFromBlockState(iblockstate1, world, blockpos), iblockstate1, blockpos, worldrenderer, true);
                worldrenderer.setTranslation((double)((float)x - (float)blockpos.getX()), (double)((float)y - (float)blockpos.getY()), (double)((float)z - (float)blockpos.getZ()));
                iblockstate.withProperty(BlockPistonBase.EXTENDED, Boolean.valueOf(true));
                this.blockRenderer.renderModel(world, this.blockRenderer.getModelFromBlockState(iblockstate, world, blockpos), iblockstate, blockpos, worldrenderer, true);
            }
            else
            {
                this.blockRenderer.renderModel(world, this.blockRenderer.getModelFromBlockState(iblockstate, world, blockpos), iblockstate, blockpos, worldrenderer, false);
            }

            worldrenderer.setTranslation(0.0D, 0.0D, 0.0D);
            Tessellator.draw();
            ItemRenderer.enableStandardItemLighting();
        }
    }
}
