package common.block;

import common.init.Blocks;
import common.init.Config;
import common.item.CheatTab;
import common.item.Item;
import common.material.Material;
import common.model.Model;
import common.model.ModelProvider;
import common.model.ParticleType;
import common.properties.IProperty;
import common.properties.PropertyBool;
import common.rng.Random;
import common.util.BlockPos;
import common.world.IWorldAccess;
import common.world.AWorldClient;
import common.world.State;
import common.world.AWorldServer;

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

    public BlockMycelium()
    {
        super(Material.grass);
        this.setDefaultState(this.getBaseState().withProperty(SNOWY, Boolean.valueOf(false)));
        this.setTickRandomly();
        this.setTab(CheatTab.tabNature);
    }

    /**
     * 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 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(Config.mycelDecay)
        		worldIn.setState(pos, Blocks.dirt.getState().withProperty(BlockDirt.VARIANT, BlockDirt.DirtType.DIRT));
        }
        else if(Config.mycelSpread)
        {
            if (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);
                    State iblockstate = worldIn.getState(blockpos);
                    Block block = worldIn.getState(blockpos.up()).getBlock();

                    if (iblockstate.getBlock() == Blocks.dirt && iblockstate.getValue(BlockDirt.VARIANT) == BlockDirt.DirtType.DIRT && worldIn.getLightFromNeighbors(blockpos.up()) >= 4 && block.getLightOpacity() <= 2)
                    {
                        worldIn.setState(blockpos, this.getState());
                    }
                }
            }
        }
//        }
    }

    public void randomDisplayTick(AWorldClient worldIn, BlockPos pos, State state, Random rand)
    {
        super.randomDisplayTick(worldIn, pos, state, rand);

        if (rand.chance(10))
        {
            worldIn.spawnParticle(ParticleType.TOWN_AURA, (double)((float)pos.getX() + rand.floatv()), (double)((float)pos.getY() + 1.1F), (double)((float)pos.getZ() + rand.floatv()), 0.0D, 0.0D, 0.0D);
        }
    }

    /**
     * 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().withProperty(BlockDirt.VARIANT, BlockDirt.DirtType.DIRT), rand, fortune);
    }

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

    protected IProperty[] getProperties()
    {
        return new IProperty[] {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("mycelium_side").add().nswe().d("dirt").u("mycelium_top");
    }
}
