package common.item.tool;

import common.block.Block;
import common.block.Material;
import common.entity.npc.EntityNPC;
import common.entity.types.EntityLiving;
import common.init.Blocks;
import common.init.ToolMaterial;
import common.item.CheatTab;
import common.item.Item;
import common.item.ItemAction;
import common.item.ItemStack;
import common.model.Transforms;
import common.util.BlockPos;
import common.world.World;

public class ItemSword extends Item
{
    private int attackDamage;
    private final ToolMaterial material;

    public ItemSword(ToolMaterial material)
    {
        this.material = material;
        this.setMaxDamage(material.getDurability());
        this.setTab(CheatTab.COMBAT);
        this.attackDamage = 4 + material.getDamage();
    }

    /**
     * Returns the amount of damage this item will deal. One heart of damage is equal to 2 damage points.
     */
    public int getDamageVsEntity()
    {
        return this.material.getDamage();
    }
    
    public ToolMaterial getToolMaterial()
    {
        return this.material;
    }

    public float getStrVsBlock(ItemStack stack, Block state)
    {
        if (state == Blocks.web)
        {
            return 15.0F;
        }
        else
        {
            Material material = state.getMaterial();
            return material != Material.PLANT && material != Material.BUSH && material != Material.LEAVES && material != Material.SOFT ? 1.0F : 1.5F;
        }
    }

    /**
     * Current implementations of this method in child classes do not use the entry argument beside ev. They just raise
     * the damage on the stack.
     */
    public boolean hitEntity(ItemStack stack, EntityLiving target, EntityLiving attacker)
    {
        stack.damageItem(1, attacker);
        return true;
    }

    /**
     * Called when a Block is destroyed using this Item. Return true to trigger the "Use Item" statistic.
     */
    public boolean onBlockDestroyed(ItemStack stack, World worldIn, Block blockIn, BlockPos pos, EntityLiving playerIn)
    {
        if ((double)blockIn.getBlockHardness(worldIn, pos) != 0.0D)
        {
            stack.damageItem(2, playerIn);
        }

        return true;
    }

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

    /**
     * returns the action that specifies what animation to play when the items is being used
     */
    public ItemAction getItemUseAction(ItemStack stack)
    {
        return ItemAction.BLOCK;
    }

    /**
     * How long it takes to use or consume an item
     */
    public int getMaxItemUseDuration(ItemStack stack)
    {
        return 72000;
    }

    /**
     * Called whenever this item is equipped and the right mouse button is pressed. Args: itemStack, world, entityPlayer
     */
    public ItemStack onItemRightClick(ItemStack itemStackIn, World worldIn, EntityNPC playerIn)
    {
        playerIn.setItemInUse(itemStackIn, this.getMaxItemUseDuration(itemStackIn));
        return itemStackIn;
    }

    /**
     * Check whether this Item can harvest the given Block
     */
    public boolean canHarvestBlock(Block blockIn)
    {
        return blockIn == Blocks.web;
    }

    /**
     * Return the enchantability factor of the item, most of the time is based on material.
     */
    public int getItemEnchantability()
    {
        return this.material.getEnchantability();
    }

//    /**
//     * Return the name for this tool's material.
//     */
//    public String getToolMaterialName()
//    {
//        return this.material.toString();
//    }

    /**
     * Return whether this item is repairable in an anvil.
     */
    public boolean getIsRepairable(ItemStack toRepair, ItemStack repair)
    {
        return this.material.isRepairItem(repair.getItem()) ? true : super.getIsRepairable(toRepair, repair);
    }
    
    public int getAttackDamageBonus() {
    	return this.attackDamage;
    }
    
//    public boolean canBreakBlocks() {
//        return false;
//    }
    
    public boolean isMagnetic() {
    	return this.material.isMagnetic();
    }

    public Transforms getTransform() {
    	return Transforms.TOOL;
    }
    
    public boolean canBeWielded() {
    	return true;
    }
}
