package common.block.foliage;

import java.util.List;

import common.biome.IBiome;
import common.collect.Lists;
import common.init.Blocks;
import common.init.WoodType;
import common.item.CheatTab;
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 BlockSapling extends BlockBush implements IGrowable
{
//    public static final PropertyEnum<BlockPlanks.EnumType> TYPE = PropertyEnum.<BlockPlanks.EnumType>create("type", BlockPlanks.EnumType.class);
    public static final PropertyInteger STAGE = PropertyInteger.create("stage", 0, 1);
    public static final List<BlockSapling> SAPLINGS = Lists.newArrayList();
    
    private final WoodType type;

    public BlockSapling(WoodType type)
    {
        this.setDefaultState(this.getBaseState() /* .withProperty(TYPE, BlockPlanks.EnumType.OAK) */ .withProperty(STAGE, Integer.valueOf(0)));
        float f = 0.4F;
        this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, f * 2.0F, 0.5F + f);
        this.setTab(CheatTab.PLANTS);
        this.type = type;
        this.setFlammable(15, 100);
        SAPLINGS.add(this);
    }

//    /**
//     * Gets the localized name of this block. Used for the statistics page.
//     */
//    public String getLocalizedName()
//    {
//        return I18n.translate(this.getUnlocalizedName() + "." + BlockPlanks.EnumType.OAK.getUnlocalizedName() + ".name");
//    }

    public void updateTick(AWorldServer worldIn, BlockPos pos, State state, Random rand)
    {
        if(Vars.saplingDry && worldIn.getTemperatureC(pos) >= 50.0f)
        {
        	worldIn.setState(pos, worldIn.rand.chance(25) ? Blocks.air.getState() : 
        			Blocks.dead_bush.getState());
        	return;
        }
//        if (!worldIn.client)
//        {
        super.updateTick(worldIn, pos, state, rand);

        if (Vars.treeGrowth > 0 && worldIn.getLightFromNeighbors(pos.up()) >= 9 && rand.chance(Vars.treeGrowth))
        {
            this.grow(worldIn, pos, state, rand);
        }
//        }
    }

    public void grow(AWorldServer worldIn, BlockPos pos, State state, Random rand)
    {
        if (((Integer)state.getValue(STAGE)).intValue() == 0)
        {
            worldIn.setState(pos, state.cycleProperty(STAGE), 4);
        }
        else
        {
        	IBiome.getBiome(worldIn.getBiomeGenForCoords(pos)).generateTree(worldIn, pos, state, rand);
        }
    }

//    /**
//     * Gets the metadata of the item this Block can drop. This method is called when the block gets destroyed. It
//     * returns the metadata of the dropped item based on the old metadata of the block.
//     */
//    public int damageDropped(IBlockState state)
//    {
//        return ((BlockPlanks.EnumType)state.getValue(TYPE)).getMetadata();
//    }

//    /**
//     * returns a list of blocks with the same ID, but different meta (eg: wood returns 4 blocks)
//     */
//    public void getSubBlocks(Item itemIn, CreativeTab tab, List<ItemStack> list)
//    {
//        for (BlockPlanks.EnumType blockplanks$enumtype : BlockPlanks.EnumType.values())
//        {
//            list.add(new ItemStack(itemIn, 1, blockplanks$enumtype.getMetadata()));
//        }
//    }

    /**
     * Whether this IGrowable can grow
     */
    public boolean canGrow(World worldIn, BlockPos pos, State state, boolean isClient)
    {
        return true;
    }

    public boolean canUseBonemeal(World worldIn, Random rand, BlockPos pos, State state)
    {
        return (double)worldIn.rand.floatv() < 0.45D;
    }

    public void grow(AWorldServer worldIn, Random rand, BlockPos pos, State state)
    {
        this.grow(worldIn, pos, state, rand);
    }

    /**
     * Convert the given metadata into a BlockState for this Block
     */
    public State getStateFromMeta(int meta)
    {
        return this.getState() /* .withProperty(TYPE, BlockPlanks.EnumType.byMetadata(meta & 7)) */ .withProperty(STAGE, meta & 1);
    }

    /**
     * Convert the BlockState into the correct metadata value
     */
    public int getMetaFromState(State state)
    {
//        int i = 0;
//        i = i | ((BlockPlanks.EnumType)state.getValue(TYPE)).getMetadata();
//        i = i | ((Integer)state.getValue(STAGE)).intValue() << 3;
        return state.getValue(STAGE);
    }

    protected Property[] getProperties()
    {
        return new Property[] {STAGE};
    }

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

	public WoodType getWoodType() {
		return this.type;
	}
}
