package common.item.tool;

import common.block.Block;
import common.entity.npc.EntityNPC;
import common.init.Blocks;
import common.init.ToolMaterial;
import common.item.CheatTab;
import common.item.Item;
import common.item.ItemStack;
import common.model.Transforms;
import common.util.BlockPos;
import common.util.Facing;
import common.world.State;
import common.world.World;

public class ItemHoe extends Item
{
    protected ToolMaterial theToolMaterial;

    public ItemHoe(ToolMaterial material)
    {
        this.theToolMaterial = material;
        this.setMaxDamage(material.getDurability());
        this.setTab(CheatTab.TOOLS);
    }

    

    /**
     * Called when a Block is right-clicked with this Item
     */
    public boolean onItemUse(ItemStack stack, EntityNPC playerIn, World worldIn, BlockPos pos, Facing side, float hitX, float hitY, float hitZ)
    {
        if (!playerIn.canPlayerEdit(pos.offset(side), side, stack))
        {
            return false;
        }
        else
        {
            State iblockstate = worldIn.getState(pos);
            Block block = iblockstate.getBlock();

            if (side != Facing.DOWN && worldIn.getState(pos.up()).getBlock() == Blocks.air)
            {
                if (block == Blocks.grass)
                {
                    return this.useHoe(stack, playerIn, worldIn, pos, Blocks.farmland.getState());
                }

                if (block == Blocks.dirt)
                {
                    return this.useHoe(stack, playerIn, worldIn, pos, Blocks.farmland.getState());
                }

                if (block == Blocks.coarse_dirt)
                {
                	return this.useHoe(stack, playerIn, worldIn, pos, Blocks.dirt.getState());
                }
            }

            return false;
        }
    }

    protected boolean useHoe(ItemStack stack, EntityNPC player, World worldIn, BlockPos target, State newState)
    {
    	if(newState.getBlock().sound.getStepSound() != null)
        	worldIn.playSound(newState.getBlock().sound.getStepSound(), (double)((float)target.getX() + 0.5F), (double)((float)target.getY() + 0.5F), (double)((float)target.getZ() + 0.5F), 1.0F);

        if (worldIn.client)
        {
            return true;
        }
        else
        {
            worldIn.setState(target, newState);
            stack.damageItem(1, player);
            return true;
        }
    }

//    /**
//     * Returns True is the item is renderer in full 3D when hold.
//     */
//    public boolean isFull3D()
//    {
//        return true;
//    }

//    /**
//     * Returns the name of the material this tool is made from as it is declared in EnumToolMaterial (meaning diamond
//     * would return "EMERALD")
//     */
//    public String getMaterialName()
//    {
//        return this.theToolMaterial.toString();
//    }
    
    public boolean isMagnetic() {
    	return this.theToolMaterial.isMagnetic();
    }

    public Transforms getTransform() {
    	return Transforms.TOOL;
    }

    public ToolMaterial getToolMaterial()
    {
        return this.theToolMaterial;
    }
    
    public boolean canBeWielded() {
    	return true;
    }
}
