package common.block.foliage;

import common.biome.IBiome;
import common.block.Block;
import common.block.Material;
import common.color.Colorizer;
import common.init.Blocks;
import common.item.CheatTab;
import common.item.Item;
import common.item.block.ItemColored;
import common.model.BlockLayer;
import common.model.Model;
import common.model.ModelProvider;
import common.properties.Property;
import common.properties.PropertyBool;
import common.rng.Random;
import common.util.BlockPos;
import common.vars.Vars;
import common.world.IWorldAccess;
import common.world.State;
import common.world.World;
import common.world.AWorldServer;

public class BlockGrass extends Block implements IGrowable
{
    public static final PropertyBool SNOWY = PropertyBool.create("snowy");

    public BlockGrass()
    {
        super(Material.LOOSE);
        this.setDefaultState(this.getBaseState().withProperty(SNOWY, Boolean.valueOf(false)));
        this.setTickRandomly();
        this.setTab(CheatTab.NATURE);
    }

    /**
     * Get the actual Block state of this Block at the given position. This applies properties not visible in the
     * metadata, such as fence connections.
     */
    public State getActualState(State state, IWorldAccess worldIn, BlockPos pos)
    {
        Block block = worldIn.getState(pos.up()).getBlock();
        return state.withProperty(SNOWY, Boolean.valueOf(block == Blocks.snow || block == Blocks.snow_layer));
    }

//    public int getBlockColor()
//    {
//        return ColorizerFoliage.getGrassColor(0.5D, 1.0D);
//    }

    public int getRenderColor(State state)
    {
    	return Colorizer.getGrassColor(0.5D, 1.0D);
//        return this.getBlockColor();
    }

    public int colorMultiplier(IWorldAccess worldIn, BlockPos pos, int renderPass)
    {
        return Colorizer.getGrassColor(worldIn, pos);
    }

    public void updateTick(AWorldServer worldIn, BlockPos pos, State state, Random rand)
    {
//        if (!worldIn.client)
//        {
        if (worldIn.getLightFromNeighbors(pos.up()) < 4 && worldIn.getState(pos.up()).getBlock().getLightOpacity() > 2)
        {
        	if(Vars.grassDecay)
        		worldIn.setState(pos, Blocks.dirt.getState());
        }
        else if(worldIn.getTemperatureC(pos) < 50.0f)
        {
            if (Vars.grassSpread && worldIn.getLightFromNeighbors(pos.up()) >= 9)
            {
                for (int i = 0; i < 4; ++i)
                {
                    BlockPos blockpos = pos.add(rand.zrange(3) - 1, rand.zrange(5) - 3, rand.zrange(3) - 1);
                    Block block = worldIn.getState(blockpos.up()).getBlock();
                    State iblockstate = worldIn.getState(blockpos);

                    if (iblockstate.getBlock() == Blocks.dirt && worldIn.getLightFromNeighbors(blockpos.up()) >= 4 && block.getLightOpacity() <= 2)
                    {
                        worldIn.setState(blockpos, Blocks.grass.getState());
                    }
                }
            }
        }
        else {
        	if(Vars.grassDry)
        		worldIn.setState(pos, worldIn.rand.chance(20) ? Blocks.coarse_dirt.getState() : 
        				Blocks.dirt.getState());
        }
//        }
    }

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

    /**
     * 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 true;
    }

    public void grow(AWorldServer worldIn, Random rand, BlockPos pos, State state)
    {
    	IBiome.getBiome(worldIn.getBiomeGenForCoords(pos)).growGrass(worldIn, pos, state, rand);
    }

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

    /**
     * Convert the BlockState into the correct metadata value
     */
    public int getMetaFromState(State state)
    {
        return 0;
    }

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

    public Model getModel(ModelProvider provider, String name, State state) {
    	if(state.getValue(SNOWY))
    		return provider.getModel("grass_side_snowed").add().nswe().d("dirt").u("grass_top");
    	else
    		return provider.getModel("dirt").add().d().u("grass_top").tint().nswe("grass_side")
    				.add().nswe("grass_side_overlay").tint();
    }
	
	protected Item getItemToRegister() {
		return new ItemColored(this);
	}
}
