package common.block;

import common.entity.types.EntityLiving;
import common.item.CheatTab;
import common.material.Material;
import common.model.BlockLayer;
import common.model.Model;
import common.model.ModelProvider;
import common.model.ModelRotation;
import common.properties.IProperty;
import common.properties.PropertyDirection;
import common.util.BlockPos;
import common.util.BoundingBox;
import common.util.Facing;
import common.world.IWorldAccess;
import common.world.State;
import common.world.World;

public class BlockLadder extends Block
{
    public static final PropertyDirection FACING = PropertyDirection.create("facing", Facing.Plane.HORIZONTAL);

    public BlockLadder()
    {
        super(Material.circuits);
        this.setDefaultState(this.getBaseState().withProperty(FACING, Facing.NORTH));
        this.setTab(CheatTab.tabWood);
    }

    public BoundingBox getCollisionBoundingBox(World worldIn, BlockPos pos, State state)
    {
        this.setBlockBoundsBasedOnState(worldIn, pos);
        return super.getCollisionBoundingBox(worldIn, pos, state);
    }

    public BoundingBox getSelectedBoundingBox(World worldIn, BlockPos pos)
    {
        this.setBlockBoundsBasedOnState(worldIn, pos);
        return super.getSelectedBoundingBox(worldIn, pos);
    }

    public void setBlockBoundsBasedOnState(IWorldAccess worldIn, BlockPos pos)
    {
        State iblockstate = worldIn.getState(pos);

        if (iblockstate.getBlock() == this)
        {
            float f = 0.125F;

            switch ((Facing)iblockstate.getValue(FACING))
            {
                case NORTH:
                    this.setBlockBounds(0.0F, 0.0F, 1.0F - f, 1.0F, 1.0F, 1.0F);
                    break;

                case SOUTH:
                    this.setBlockBounds(0.0F, 0.0F, 0.0F, 1.0F, 1.0F, f);
                    break;

                case WEST:
                    this.setBlockBounds(1.0F - f, 0.0F, 0.0F, 1.0F, 1.0F, 1.0F);
                    break;

                case EAST:
                default:
                    this.setBlockBounds(0.0F, 0.0F, 0.0F, f, 1.0F, 1.0F);
            }
        }
    }

    /**
     * Used to determine ambient occlusion and culling when rebuilding chunks for render
     */
    public boolean isOpaqueCube()
    {
        return false;
    }

    public boolean isFullCube()
    {
        return false;
    }

    public boolean canPlaceBlockAt(World worldIn, BlockPos pos)
    {
        return worldIn.getState(pos.west()).getBlock().isNormalCube() ? true : (worldIn.getState(pos.east()).getBlock().isNormalCube() ? true : (worldIn.getState(pos.north()).getBlock().isNormalCube() ? true : worldIn.getState(pos.south()).getBlock().isNormalCube()));
    }

    /**
     * Called by ItemBlocks just before a block is actually set in the world, to allow for adjustments to the
     * IBlockstate
     */
    public State onBlockPlaced(World worldIn, BlockPos pos, Facing facing, float hitX, float hitY, float hitZ, int meta, EntityLiving placer)
    {
        if (facing.getAxis().isHorizontal() && this.canBlockStay(worldIn, pos, facing))
        {
            return this.getState().withProperty(FACING, facing);
        }
        else
        {
            for (Facing enumfacing : Facing.Plane.HORIZONTAL)
            {
                if (this.canBlockStay(worldIn, pos, enumfacing))
                {
                    return this.getState().withProperty(FACING, enumfacing);
                }
            }

            return this.getState();
        }
    }

    /**
     * Called when a neighboring block changes.
     */
    public void onNeighborBlockChange(World worldIn, BlockPos pos, State state, Block neighborBlock)
    {
        Facing enumfacing = (Facing)state.getValue(FACING);

        if (!this.canBlockStay(worldIn, pos, enumfacing))
        {
            this.dropBlockAsItem(worldIn, pos, state, 0);
            worldIn.setBlockToAir(pos);
        }

        super.onNeighborBlockChange(worldIn, pos, state, neighborBlock);
    }

    protected boolean canBlockStay(World worldIn, BlockPos pos, Facing facing)
    {
        return worldIn.getState(pos.offset(facing.getOpposite())).getBlock().isNormalCube();
    }

    public BlockLayer getBlockLayer()
    {
        return BlockLayer.CUTOUT;
    }

    /**
     * Convert the given metadata into a BlockState for this Block
     */
    public State getStateFromMeta(int meta)
    {
        Facing enumfacing = Facing.getFront(meta);

        if (enumfacing.getAxis() == Facing.Axis.Y)
        {
            enumfacing = Facing.NORTH;
        }

        return this.getState().withProperty(FACING, enumfacing);
    }

    /**
     * Convert the BlockState into the correct metadata value
     */
    public int getMetaFromState(State state)
    {
        return ((Facing)state.getValue(FACING)).getIndex();
    }

    protected IProperty[] getProperties()
    {
        return new IProperty[] {FACING};
    }

    public Model getModel(ModelProvider provider, String name, State state) {
    	return provider.getModel("ladder").noOcclude()
		.add(0, 0, 15.2f, 16, 16, 15.2f).noShade()
			.n().uv(0, 0, 16, 16).noCull()
			.s().uv(0, 0, 16, 16).noCull()
			.rotate(ModelRotation.getNorthRot(state.getValue(BlockDirectional.FACING)));
    }
}
