package common.block.foliage;

import common.block.Block;
import common.block.Material;
import common.init.Blocks;
import common.init.Items;
import common.item.CheatTab;
import common.item.Item;
import common.item.ItemStack;
import common.item.block.ItemSeeds;
import common.model.Model;
import common.model.ModelProvider;
import common.properties.Property;
import common.properties.PropertyInteger;
import common.rng.Random;
import common.util.BlockPos;
import common.vars.Vars;
import common.world.State;
import common.world.World;
import common.world.AWorldServer;

public class BlockWart extends BlockBush
{
    public static final PropertyInteger AGE = PropertyInteger.create("age", 0, 3);

    public BlockWart()
    {
        super(Material.PLANT);
        this.setDefaultState(this.getBaseState().withProperty(AGE, Integer.valueOf(0)));
        this.setTickRandomly();
        float f = 0.5F;
        this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, 0.25F, 0.5F + f);
        this.setTab((CheatTab)null);
    }

    /**
     * is the block grass, dirt or farmland
     */
    protected boolean canPlaceBlockOn(Block ground)
    {
        return ground == Blocks.soul_sand;
    }

    public boolean canBlockStay(World worldIn, BlockPos pos, State state)
    {
        return this.canPlaceBlockOn(worldIn.getState(pos.down()).getBlock());
    }

    public void updateTick(AWorldServer worldIn, BlockPos pos, State state, Random rand)
    {
    	if(Vars.wartGrowth > 0) {
	        int i = ((Integer)state.getValue(AGE)).intValue();
	
	        if (i < 3 && rand.chance(Vars.wartGrowth))
	        {
	            state = state.withProperty(AGE, Integer.valueOf(i + 1));
	            worldIn.setState(pos, state, 2);
	        }
    	}

        super.updateTick(worldIn, pos, state, rand);
    }

    /**
     * Spawns this Block's drops into the World as EntityItems.
     */
    public void dropBlockAsItemWithChance(World worldIn, BlockPos pos, State state, float chance, int fortune)
    {
        if (!worldIn.client)
        {
            int i = 1;

            if (((Integer)state.getValue(AGE)).intValue() >= 3)
            {
                i = worldIn.rand.range(2, 4);

                if (fortune > 0)
                {
                    i += worldIn.rand.zrange(fortune + 1);
                }
            }

            for (int j = 0; j < i; ++j)
            {
                spawnAsEntity(worldIn, pos, new ItemStack(Items.soul_wart));
            }
        }
    }

    /**
     * Get the Item that this Block should drop when harvested.
     */
    public Item getItemDropped(State state, Random rand, int fortune)
    {
        return null;
    }

    /**
     * Returns the quantity of items to drop on block destruction.
     */
    public int quantityDropped(Random random)
    {
        return 0;
    }

    public Item getItem(World worldIn, BlockPos pos)
    {
        return Items.soul_wart;
    }

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

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

    protected Property[] getProperties()
    {
        return new Property[] {AGE};
    }
    
    public Model getModel(ModelProvider provider, String name, State state) {
    	int age = state.getValue(AGE);
    	return BlockCrops.crop(provider, name + "_" + (age >= 2 ? age - 1 : age));
    }
	
	protected Item getItemToRegister() {
		return new ItemSeeds(this, Blocks.soul_sand).setDisplay("Seelenwarze").setPotionEffect("+4").setMaxAmount(128);
	}
}
