package common.block;

import common.entity.DamageSource;
import common.entity.Entity;
import common.init.Blocks;
import common.init.Config;
import common.item.CheatTab;
import common.material.Material;
import common.model.BlockLayer;
import common.model.Model;
import common.model.ModelProvider;
import common.properties.IProperty;
import common.properties.PropertyInteger;
import common.rng.Random;
import common.util.BlockPos;
import common.util.BoundingBox;
import common.util.Facing;
import common.world.State;
import common.world.World;
import common.world.AWorldServer;

public class BlockCactus extends Block
{
	private static final Model cactus = ModelProvider.getModelProvider().getModel("cactus_side").noOcclude()
			.add(0, 0, 0, 16, 16, 16)
				.d("cactus_bottom").uv(0, 0, 16, 16)
				.u("cactus_top").uv(0, 0, 16, 16)
			.add(0, 0, 1, 16, 16, 15)
				.n().uv(0, 0, 16, 16).noCull()
				.s().uv(0, 0, 16, 16).noCull()
			.add(1, 0, 0, 15, 16, 16)
				.w().uv(0, 0, 16, 16).noCull()
				.e().uv(0, 0, 16, 16).noCull()
	;
	
    public static final PropertyInteger AGE = PropertyInteger.create("age", 0, 15);

    public BlockCactus()
    {
        super(Material.cactus);
        this.setDefaultState(this.getBaseState().withProperty(AGE, Integer.valueOf(0)));
        this.setTickRandomly();
        this.setTab(CheatTab.tabPlants);
    }

    public void updateTick(AWorldServer worldIn, BlockPos pos, State state, Random rand)
    {
        BlockPos blockpos = pos.up();

        if (Config.cactusHeight > 0 && worldIn.isAirBlock(blockpos))
        {
            int i;

            for (i = 1; worldIn.getState(pos.down(i)).getBlock() == this; ++i)
            {
                ;
            }

            if (i < Config.cactusHeight)
            {
                int j = ((Integer)state.getValue(AGE)).intValue();

                if (j == 15)
                {
                    worldIn.setState(blockpos, this.getState());
                    State iblockstate = state.withProperty(AGE, Integer.valueOf(0));
                    worldIn.setState(pos, iblockstate, 4);
                    this.onNeighborBlockChange(worldIn, blockpos, iblockstate, this);
                }
                else
                {
                    worldIn.setState(pos, state.withProperty(AGE, Integer.valueOf(j + 1)), 4);
                }
            }
        }
    }

    public BoundingBox getCollisionBoundingBox(World worldIn, BlockPos pos, State state)
    {
        float f = 0.0625F;
        return new BoundingBox((double)((float)pos.getX() + f), (double)pos.getY(), (double)((float)pos.getZ() + f), (double)((float)(pos.getX() + 1) - f), (double)((float)(pos.getY() + 1) - f), (double)((float)(pos.getZ() + 1) - f));
    }

    public BoundingBox getSelectedBoundingBox(World worldIn, BlockPos pos)
    {
        float f = 0.0625F;
        return new BoundingBox((double)((float)pos.getX() + f), (double)pos.getY(), (double)((float)pos.getZ() + f), (double)((float)(pos.getX() + 1) - f), (double)(pos.getY() + 1), (double)((float)(pos.getZ() + 1) - f));
    }

    public boolean isFullCube()
    {
        return false;
    }

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

    public boolean canPlaceBlockAt(World worldIn, BlockPos pos)
    {
        return super.canPlaceBlockAt(worldIn, pos) ? this.canBlockStay(worldIn, pos) : false;
    }

    /**
     * Called when a neighboring block changes.
     */
    public void onNeighborBlockChange(World worldIn, BlockPos pos, State state, Block neighborBlock)
    {
        if (!this.canBlockStay(worldIn, pos))
        {
            worldIn.destroyBlock(pos, true);
        }
    }

    public boolean canBlockStay(World worldIn, BlockPos pos)
    {
        for (Facing enumfacing : Facing.Plane.HORIZONTAL)
        {
            if (worldIn.getState(pos.offset(enumfacing)).getBlock().getMaterial().isSolid())
            {
                return false;
            }
        }

        Block block = worldIn.getState(pos.down()).getBlock();
        return block == Blocks.cactus || block == Blocks.sand;
    }

    /**
     * Called When an Entity Collided with the Block
     */
    public void onEntityCollidedWithBlock(World worldIn, BlockPos pos, State state, Entity entityIn)
    {
    	if(worldIn.client || Config.cactusDamage)
    		entityIn.attackEntityFrom(DamageSource.cactus, 1);
    }

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

    /**
     * Convert the given metadata into a BlockState for this Block
     */
    public State getStateFromMeta(int meta)
    {
        return this.getState().withProperty(AGE, Integer.valueOf(meta));
    }

    /**
     * Convert the BlockState into the correct metadata value
     */
    public int getMetaFromState(State state)
    {
        return ((Integer)state.getValue(AGE)).intValue();
    }

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

    public Model getModel(ModelProvider provider, String name, State state) {
    	return cactus;
    }

    public IProperty<?>[] getIgnoredProperties() {
    	return new IProperty[] {AGE};
    }
}
