package common.block.foliage;

import common.block.Material;
import common.color.Colorizer;
import common.entity.npc.EntityNPC;
import common.init.Blocks;
import common.init.Items;
import common.item.Item;
import common.item.ItemStack;
import common.item.block.ItemColored;
import common.item.tool.ItemShears;
import common.model.Model;
import common.model.ModelProvider;
import common.rng.Random;
import common.tileentity.TileEntity;
import common.util.BlockPos;
import common.util.Identifyable;
import common.vars.Vars;
import common.world.IWorldAccess;
import common.world.State;
import common.world.World;
import common.world.AWorldServer;

public class BlockTallGrass extends BlockBush implements IGrowable
{
	public static final BlockTallGrass[] BUSHES = new BlockTallGrass[EnumType.values().length];
	
    private final EnumType type;
    
    public static BlockTallGrass getByType(EnumType type) {
    	return BUSHES[type.ordinal()];
    }

    public BlockTallGrass(EnumType type)
    {
        super(Material.BUSH);
        this.type = type;
        float f = 0.4F;
        this.setBlockBounds(0.5F - f, 0.0F, 0.5F - f, 0.5F + f, 0.8F, 0.5F + f);
        this.setFlammable(60, 100);
        BUSHES[type.ordinal()] = this;
    }
    
    public EnumType getType() {
    	return this.type;
    }

    public void updateTick(AWorldServer worldIn, BlockPos pos, State state, Random rand)
    {
        if(Vars.tallgrassDry && worldIn.getTemperatureC(pos) >= 50.0f && this.type != EnumType.DEAD_BUSH)
        {
        	worldIn.setState(pos, this.type == EnumType.GRASS || worldIn.rand.chance(20) ? Blocks.air.getState() : 
        			Blocks.dead_bush.getState());
        	return;
        }
        super.updateTick(worldIn, pos, state, rand);
    }

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

    /**
     * Whether this Block can be replaced directly by other blocks (true for e.g. tall grass)
     */
    public boolean isReplaceable(World worldIn, BlockPos pos)
    {
        return true;
    }

    public int getRenderColor(State state)
    {
        if (state.getBlock() != this)
        {
            return super.getRenderColor(state);
        }
        else
        {
            return this.type == EnumType.DEAD_BUSH ? 16777215 : Colorizer.getGrassColor(0.5D, 1.0D);
        }
    }

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

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

    /**
     * Get the quantity dropped based on the given fortune level
     */
    public int quantityDroppedWithBonus(int fortune, Random random)
    {
        return random.roll(fortune * 2 + 1);
    }

    public void harvestBlock(World worldIn, EntityNPC player, BlockPos pos, State state, TileEntity te)
    {
        if (!worldIn.client && player.getCurrentEquippedItem() != null && player.getCurrentEquippedItem().getItem() instanceof ItemShears)
        {
            spawnAsEntity(worldIn, pos, new ItemStack(this.getItem()));
        }
        else
        {
            super.harvestBlock(worldIn, player, pos, state, te);
        }
    }

    /**
     * Whether this IGrowable can grow
     */
    public boolean canGrow(World worldIn, BlockPos pos, State state, boolean isClient)
    {
        return this.type != EnumType.DEAD_BUSH;
    }

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

    public void grow(AWorldServer worldIn, Random rand, BlockPos pos, State state)
    {
        BlockDoublePlant block = Blocks.large_tallgrass;

        if (this.type == BlockTallGrass.EnumType.FERN)
        {
            block = Blocks.large_fern;
        }

        if (block.canPlaceBlockAt(worldIn, pos))
        {
            block.placeAt(worldIn, pos, 2);
        }
    }

    public Model getModel(ModelProvider provider, String name, State state) {
    	if(this.type != EnumType.DEAD_BUSH)
    		return provider.getModel(this.type.getName()).crossTint();
    	else
    		return provider.getModel("deadbush").cross();
    }
	
	protected Item getItemToRegister() {
		return new ItemColored(this, "");
	}

    public static enum EnumType implements Identifyable
    {
        DEAD_BUSH(0, "dead_bush", "Busch"),
        GRASS(1, "tallgrass", "Gras"),
        FERN(2, "fern", "Farn");

        private static final BlockTallGrass.EnumType[] META_LOOKUP = new BlockTallGrass.EnumType[values().length];
        private final int meta;
        private final String name;
        private final String display;

        private EnumType(int meta, String name, String display)
        {
            this.meta = meta;
            this.name = name;
            this.display = display;
        }

        public int getMeta()
        {
            return this.meta;
        }

        public String toString()
        {
            return this.name;
        }

        public static BlockTallGrass.EnumType byMetadata(int meta)
        {
            if (meta < 0 || meta >= META_LOOKUP.length)
            {
                meta = 0;
            }

            return META_LOOKUP[meta];
        }

        public String getName()
        {
            return this.name;
        }

        public String getDisplay()
        {
            return this.display;
        }

        static {
            for (BlockTallGrass.EnumType blocktallgrass$enumtype : values())
            {
                META_LOOKUP[blocktallgrass$enumtype.getMeta()] = blocktallgrass$enumtype;
            }
        }
    }
}
