package info.jbcs.minecraft.chisel;

import java.util.List;

import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.item.ItemStack;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.MathHelper;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeDirection;

public class BlockMarbleStairs extends BlockMarble {
	int orientation;
	BlockMarbleStairs main;
	Block[] orientations = new Block[4];
	private final Block modelBlock;

	private final boolean field_72156_cr = false;
	private final int field_72160_cs = 0;

	public BlockMarbleStairs(String name, int i, Block block) {
		this(name, i, block, null, 0);
	}

	protected BlockMarbleStairs(String name, int i, Block block, BlockMarbleStairs mainBlock, int orientation) {
		super(name, i, block.blockMaterial);
		modelBlock = block;
		setHardness(block.blockHardness);
		setResistance(block.blockResistance / 3.0F);
		setStepSound(block.stepSound);
		setLightOpacity(255);
		setCreativeTab(Chisel.tabChisel);
		
		useNeighborBrightness[i]=true;

		this.orientation = orientation;
		main = mainBlock == null ? this : mainBlock;

		main.orientations[orientation] = this;

		if (orientation == 0) {
			new BlockMarbleStairs(name + ".1", i + 1, block, this, 1);
			new BlockMarbleStairs(name + ".2", i + 2, block, this, 2);
			new BlockMarbleStairs(name + ".3", i + 3, block, this, 3);
		} else {
			carverHelper = mainBlock.carverHelper;
		}
	}

	@Override
	public void setBlockBoundsBasedOnState(IBlockAccess iblockaccess, int i, int j, int k) {
		setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
	}

	@Override
	public boolean isOpaqueCube() {
		return false;
	}

	@Override
	public boolean renderAsNormalBlock() {
		return false;
	}

	@Override
	public int getRenderType() {
		return BlockMarbleStairsRenderer.id;
	}

	@Override
	public void addCollisionBoxesToList(World world, int i, int j, int k, AxisAlignedBB axisalignedbb, List arraylist, Entity entity) {
		if (orientation == 0) {
			setBlockBounds(0.0F, 0.0F, 0.0F, 0.5F, 0.5F, 1.0F);
			super.addCollisionBoxesToList(world, i, j, k, axisalignedbb, arraylist, entity);
			setBlockBounds(0.5F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
			super.addCollisionBoxesToList(world, i, j, k, axisalignedbb, arraylist, entity);
		} else if (orientation == 1) {
			setBlockBounds(0.0F, 0.0F, 0.0F, 0.5F, 1.0F, 1.0F);
			super.addCollisionBoxesToList(world, i, j, k, axisalignedbb, arraylist, entity);
			setBlockBounds(0.5F, 0.0F, 0.0F, 1.0F, 0.5F, 1.0F);
			super.addCollisionBoxesToList(world, i, j, k, axisalignedbb, arraylist, entity);
		} else if (orientation == 2) {
			setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 0.5F, 0.5F);
			super.addCollisionBoxesToList(world, i, j, k, axisalignedbb, arraylist, entity);
			setBlockBounds(0.0F, 0.0F, 0.5F, 1.0F, 1.0F, 1.0F);
			super.addCollisionBoxesToList(world, i, j, k, axisalignedbb, arraylist, entity);
		} else if (orientation == 3) {
			setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 0.5F);
			super.addCollisionBoxesToList(world, i, j, k, axisalignedbb, arraylist, entity);
			setBlockBounds(0.0F, 0.0F, 0.5F, 1.0F, 0.5F, 1.0F);
			super.addCollisionBoxesToList(world, i, j, k, axisalignedbb, arraylist, entity);
		}
		setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
	}

	@Override
	public void onBlockPlacedBy(World world, int i, int j, int k, EntityLiving entityliving, ItemStack stack) {
		int l = MathHelper.floor_double(entityliving.rotationYaw * 4F / 360F + 0.5D) & 3;

		int desiredOrientation = 0;
		switch (l) {
		case 0:
			desiredOrientation = 2;
			break;
		case 1:
			desiredOrientation = 1;
			break;
		case 2:
			desiredOrientation = 3;
			break;
		case 3:
			desiredOrientation = 0;
			break;
		}

		if (orientation != desiredOrientation)
			world.setBlock(i, j, k, main.orientations[desiredOrientation].blockID, world.getBlockMetadata(i, j, k), 2);
	}

    /**
     * Checks if the block is a solid face on the given side, used by placement logic.
     *
     * @param world The current world
     * @param x X Position
     * @param y Y position
     * @param z Z position
     * @param side The side to check
     * @return True if the block is solid on the specified side.
     */
	
    @Override
	public boolean isBlockSolidOnSide(World world, int x, int y, int z, ForgeDirection side){
        return side==ForgeDirection.DOWN;
    }
}
