package common.block.foliage;

import common.block.Block;
import common.block.SoundType;
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 BlockCrops extends BlockBush implements IGrowable
{
    public static final PropertyInteger AGE = PropertyInteger.create("age", 0, 7);

    public BlockCrops()
    {
        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);
        this.setHardness(0.0F);
        this.setStepSound(SoundType.GRASS);
//        this.disableStats();
    }

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

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

        if (Vars.cropGrowth > 0 && worldIn.getLightFromNeighbors(pos.up()) >= 9)
        {
            int i = ((Integer)state.getValue(AGE)).intValue();

            if (i < 7)
            {
                float f = getGrowthChance(this, worldIn, pos);

                if (rand.chance((int)((float)(Vars.cropGrowth - 1) / f) + 1))
                {
                    worldIn.setState(pos, state.withProperty(AGE, Integer.valueOf(i + 1)), 2);
                }
            }
        }
    }

    public void grow(World worldIn, BlockPos pos, State state)
    {
        int i = ((Integer)state.getValue(AGE)).intValue() + worldIn.rand.range(2, 5);

        if (i > 7)
        {
            i = 7;
        }

        worldIn.setState(pos, state.withProperty(AGE, Integer.valueOf(i)), 2);
    }

    protected static float getGrowthChance(Block blockIn, World worldIn, BlockPos pos)
    {
        float f = 1.0F;
        BlockPos blockpos = pos.down();

        for (int i = -1; i <= 1; ++i)
        {
            for (int j = -1; j <= 1; ++j)
            {
                float f1 = 0.0F;
                State iblockstate = worldIn.getState(blockpos.add(i, 0, j));

                if (iblockstate.getBlock() == Blocks.farmland)
                {
                    f1 = 1.0F;

                    if (((Integer)iblockstate.getValue(BlockFarmland.MOISTURE)).intValue() > 0)
                    {
                        f1 = 3.0F;
                    }
                }

                if (i != 0 || j != 0)
                {
                    f1 /= 4.0F;
                }

                f += f1;
            }
        }

        BlockPos blockpos1 = pos.north();
        BlockPos blockpos2 = pos.south();
        BlockPos blockpos3 = pos.west();
        BlockPos blockpos4 = pos.east();
        boolean flag = blockIn == worldIn.getState(blockpos3).getBlock() || blockIn == worldIn.getState(blockpos4).getBlock();
        boolean flag1 = blockIn == worldIn.getState(blockpos1).getBlock() || blockIn == worldIn.getState(blockpos2).getBlock();

        if (flag && flag1)
        {
            f /= 2.0F;
        }
        else
        {
            boolean flag2 = blockIn == worldIn.getState(blockpos3.north()).getBlock() || blockIn == worldIn.getState(blockpos4.north()).getBlock() || blockIn == worldIn.getState(blockpos4.south()).getBlock() || blockIn == worldIn.getState(blockpos3.south()).getBlock();

            if (flag2)
            {
                f /= 2.0F;
            }
        }

        return f;
    }

    public boolean canBlockStay(World worldIn, BlockPos pos, State state)
    {
        return (worldIn.getLight(pos) >= 8 || worldIn.canSeeSky(pos)) && this.canPlaceBlockOn(worldIn.getState(pos.down()).getBlock());
    }

    protected Item getSeed()
    {
        return Items.wheat;
    }

    protected Item getCrop()
    {
        return Items.wheats;
    }

    /**
     * Spawns this Block's drops into the World as EntityItems.
     */
    public void dropBlockAsItemWithChance(World worldIn, BlockPos pos, State state, float chance, int fortune)
    {
        super.dropBlockAsItemWithChance(worldIn, pos, state, chance, 0);

        if (!worldIn.client)
        {
            int i = ((Integer)state.getValue(AGE)).intValue();

            if (i >= 7)
            {
                int j = 3 + fortune;

                for (int k = 0; k < j; ++k)
                {
                    if (worldIn.rand.zrange(15) <= i)
                    {
                        spawnAsEntity(worldIn, pos, new ItemStack(this.getSeed()));
                    }
                }
            }
        }
    }

    /**
     * Get the Item that this Block should drop when harvested.
     */
    public Item getItemDropped(State state, Random rand, int fortune)
    {
        return ((Integer)state.getValue(AGE)).intValue() == 7 ? this.getCrop() : this.getSeed();
    }

    public Item getItem(World worldIn, BlockPos pos)
    {
        return this.getSeed();
    }

    /**
     * Whether this IGrowable can grow
     */
    public boolean canGrow(World worldIn, BlockPos pos, State state, boolean isClient)
    {
        return ((Integer)state.getValue(AGE)).intValue() < 7;
    }

    public boolean canUseBonemeal(World worldIn, Random rand, BlockPos pos, State state)
    {
        return true;
    }

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

    /**
     * Convert the given metadata into a BlockState for this Block
     */
    public State getStateFromMeta(int meta)
    {
        return this.getState().withProperty(AGE, Integer.valueOf(meta > 7 ? 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) {
    	return crop(provider, name + "_" + state.getValue(AGE));
    }

	public static Model crop(ModelProvider provider, String crop) {
		return provider.getModel(crop)
			.add(4, -1, 0, 4, 15, 16).noShade()
				.w().uv(0, 0, 16, 16).noCull()
				.e().uv(0, 0, 16, 16).noCull()
			.add(12, -1, 0, 12, 15, 16).noShade()
				.w().uv(0, 0, 16, 16).noCull()
				.e().uv(0, 0, 16, 16).noCull()
			.add(0, -1, 4, 16, 15, 4).noShade()
				.n().uv(0, 0, 16, 16).noCull()
				.s().uv(0, 0, 16, 16).noCull()
			.add(0, -1, 12, 16, 15, 12).noShade()
				.n().uv(0, 0, 16, 16).noCull()
				.s().uv(0, 0, 16, 16).noCull();
	}
	
	protected Item getItemToRegister() {
		return new ItemSeeds(this, Blocks.farmland).setDisplay("Weizenkörner").setMaxAmount(256);
	}
}
