package com.cosmicdan.reignadditionals.client.renderers.tileentities;

import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ItemRenderer;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IIcon;

import org.lwjgl.opengl.GL11;

import com.cosmicdan.reignadditionals.blocks.ModBlocks;
import com.cosmicdan.reignadditionals.blocks.tileentities.TileEntityCampfire;

public class TileEntityCampfireRenderer extends TileEntitySpecialRenderer {

    //private static final ResourceLocation spitrod = new ResourceLocation("imperium4x:textures/blocks/campfire_spitrod.png");
    private Tessellator tessellator; 
    private IIcon spitrod;
    private float spitrodMap[] = new float[4];
    private double transX;
    private double transY;
    private double transZ;
    private float transScale;
    private float u;
    private float v;
    private float U;
    private float V;
    
    public TileEntityCampfireRenderer() {
        spitrod = ModBlocks.CAMPFIRE_LIT.getIcon(1, 7);
        spitrodMap[0] = spitrod.getMinU();
        spitrodMap[1] = spitrod.getMinV();
        spitrodMap[2] = spitrod.getMaxU();
        spitrodMap[3] = spitrod.getMaxV();
    }
    
    @Override
    public void renderTileEntityAt(TileEntity tileEntity, double dx, double dy, double dz, float f) {
        renderThisTileEntity((TileEntityCampfire) tileEntity, dx, dy, dz, f);
    }
    
    public void renderThisTileEntity(TileEntityCampfire tileEntity, double dx, double dy, double dz, float f) {
        if (tileEntity.metadata > 6) { // has food in inventory, draw the spit and items
            tessellator = Tessellator.instance;
            
            // draw the spit rod
            transY = dy + 0.9375D;
            transZ = dz + 0.5078125D;
            transScale = 0.25F;
            bindTexture(Minecraft.getMinecraft().renderEngine.getResourceLocation(0));
            GL11.glPushMatrix();
            GL11.glTranslated(dx, transY, transZ);
            GL11.glScalef(transScale, transScale, transScale);
            ItemRenderer.renderItemIn2D(tessellator, spitrodMap[2], spitrodMap[1], spitrodMap[0], spitrodMap[3], 16, 16, 0.0625F);
            GL11.glPopMatrix();
            GL11.glPushMatrix();
            GL11.glTranslated(dx + 0.25D, transY, transZ);
            GL11.glScalef(transScale, transScale, transScale);
            ItemRenderer.renderItemIn2D(tessellator, spitrodMap[2], spitrodMap[1], spitrodMap[0], spitrodMap[3], 16, 16, 0.0625F);
            GL11.glPopMatrix();
            GL11.glPushMatrix();
            GL11.glTranslated(dx + 0.5D, transY, transZ);
            GL11.glScalef(transScale, transScale, transScale);
            ItemRenderer.renderItemIn2D(tessellator, spitrodMap[2], spitrodMap[1], spitrodMap[0], spitrodMap[3], 16, 16, 0.0625F);
            GL11.glPopMatrix();
            GL11.glPushMatrix();
            GL11.glTranslated(dx + 0.75D, transY, transZ);
            GL11.glScalef(transScale, transScale, transScale);
            ItemRenderer.renderItemIn2D(tessellator, spitrodMap[2], spitrodMap[1], spitrodMap[0], spitrodMap[3], 16, 16, 0.0625F);
            GL11.glPopMatrix();
            
            bindTexture(Minecraft.getMinecraft().renderEngine.getResourceLocation(1));
            transY = dy + 1.0D;
            transZ = dz + 0.6D;
            transScale = 0.3125F;
        
            for (int i = 0; i < tileEntity.itemSlotStatus.length; i++) {
                if (tileEntity.itemSlotStatus[i] > 0) {
                    GL11.glPushMatrix();
                    switch (i){
                        case 0: transX = dx + 0.50D; break;
                        case 1: transX = dx + 0.30D; break;
                        case 2: transX = dx + 0.70D; break;
                        case 3: transX = dx + 0.10D; break;
                    }
                    drawItem(tileEntity.itemSlot[i]);
                    GL11.glPopMatrix();
                }
            }
        }
    }
    
    private void drawItem(ItemStack itemStack) {
        //itemIcon = Items.beef.getIconFromDamage(0);
        if (itemStack == null) return;
        IIcon itemIcon = itemStack.getIconIndex();
        GL11.glTranslated(transX, transY, transZ);
        GL11.glRotatef(45.0F, 0.0F, 1.0F, 0.0F);
        u = itemIcon.getMinU();
        v = itemIcon.getMinV();
        U = itemIcon.getMaxU();
        V = itemIcon.getMaxV();
        GL11.glScalef(transScale, transScale, transScale);
        ItemRenderer.renderItemIn2D(tessellator, U, v, u, V, itemIcon.getIconWidth(), itemIcon.getIconHeight(), 0.0625F);
    }
}



@@ -1122,236 +1122,8 @@
 ) %7B%0A
-        spitrod = ModBlocks.CAMPFIRE_LIT.getIcon(1, 7);%0A        spitrodMap%5B0%5D = spitrod.getMinU();%0A        spitrodMap%5B1%5D = spitrod.getMinV();%0A        spitrodMap%5B2%5D = spitrod.getMaxU();%0A        spitrodMap%5B3%5D = spitrod.getMaxV();%0A
     
@@ -1484,16 +1484,29 @@
 a %3E 6) %7B
+%0A            
  // has 
@@ -1608,29 +1608,16 @@
         
-%0A            
 // draw 
@@ -1628,16 +1628,398 @@
 spit rod
+%0A            // for some reason calling getIcon doesn't work when done in the constructor. It USED to back in Imperium days though... %0A            spitrod = ModBlocks.CAMPFIRE_LIT.getIcon(1, 7);%0A            spitrodMap%5B0%5D = spitrod.getMinU();%0A            spitrodMap%5B1%5D = spitrod.getMinV();%0A            spitrodMap%5B2%5D = spitrod.getMaxU();%0A            spitrodMap%5B3%5D = spitrod.getMaxV();
 %0A       

Fix Campfire render