package com.zivilon.cinder_loe.client.render.block;

import com.zivilon.cinder_loe.blocks.IceCage;
import cpw.mods.fml.client.registry.ISimpleBlockRenderingHandler;
import cpw.mods.fml.client.registry.RenderingRegistry;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.RenderBlocks;
import net.minecraft.world.IBlockAccess;
import org.lwjgl.opengl.GL11;

public class RenderIceCage implements ISimpleBlockRenderingHandler {

    public static final int RENDER_ID = RenderingRegistry.getNextAvailableRenderId();

    @Override
    public void renderInventoryBlock(Block block, int metadata, int modelID, RenderBlocks renderer) {
        if (modelID != RENDER_ID) return;

        // Render inventory block as flat texture icon
        renderer.renderBlockAsItem(block, metadata, 1.0F);
    }

    @Override
    public boolean renderWorldBlock(IBlockAccess world, int x, int y, int z, Block block, int modelId, RenderBlocks renderer) {
        if (!(block instanceof IceCage) || modelId != RENDER_ID) {
            return false;
        }

        int meta = world.getBlockMetadata(x, y, z);
        float thickness = 0.0625F; // 1/16th of a block thickness

        GL11.glPushMatrix();
        GL11.glEnable(GL11.GL_BLEND);
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

        // Render the bounding boxes as defined in the block class
        if (meta == 0) { // Lower part
            renderer.setRenderBounds(0, 0, 0, thickness, 1, 1); // Left wall
            renderer.renderStandardBlock(block, x, y, z);
            renderer.setRenderBounds(1 - thickness, 0, 0, 1, 1, 1); // Right wall
            renderer.renderStandardBlock(block, x, y, z);
            renderer.setRenderBounds(0, 0, 0, 1, 1, thickness); // Front wall
            renderer.renderStandardBlock(block, x, y, z);
            renderer.setRenderBounds(0, 0, 1 - thickness, 1, 1, 1); // Back wall
            renderer.renderStandardBlock(block, x, y, z);
            renderer.setRenderBounds(0, 0, 0, 1, thickness, 1); // Floor
            renderer.renderStandardBlock(block, x, y, z);
        } else if (meta == 1) { // Upper part
            renderer.setRenderBounds(0, 0, 0, thickness, 1, 1); // Left wall
            renderer.renderStandardBlock(block, x, y, z);
            renderer.setRenderBounds(1 - thickness, 0, 0, 1, 1, 1); // Right wall
            renderer.renderStandardBlock(block, x, y, z);
            renderer.setRenderBounds(0, 0, 0, 1, 1, thickness); // Front wall
            renderer.renderStandardBlock(block, x, y, z);
            renderer.setRenderBounds(0, 0, 1 - thickness, 1, 1, 1); // Back wall
            renderer.renderStandardBlock(block, x, y, z);
            renderer.setRenderBounds(0, 1, 0, 1, 1 + thickness, 1); // Ceiling
            renderer.renderStandardBlock(block, x, y, z);
        }

        GL11.glDisable(GL11.GL_BLEND);
        GL11.glPopMatrix();

        return true;
    }

    @Override
    public boolean shouldRender3DInInventory(int modelId) {
        return false; // Do not render as 3D in inventory
    }

    @Override
    public int getRenderId() {
        return RENDER_ID;
    }
}
