package common.item.tool;

import common.entity.npc.EntityNPC;
import common.init.SoundEvent;
import common.item.CheatTab;
import common.item.Item;
import common.item.ItemAction;
import common.item.ItemStack;
import common.potion.Potion;
import common.potion.PotionEffect;
import common.world.World;

public class ItemFood extends Item
{
    public final int itemUseDuration;
    private final int healAmount;
    private final boolean isWolfsFavoriteMeat;
    private Potion potionId;
    private int potionDuration;
    private int potionAmplifier;
    private float potionEffectProbability;

    public ItemFood(int amount, boolean isWolfFood)
    {
        this.itemUseDuration = 32;
        this.healAmount = amount;
        this.isWolfsFavoriteMeat = isWolfFood;
        this.setTab(CheatTab.MISC);
    }

    /**
     * Called when the player finishes using this Item (E.g. finishes eating.). Not called when the player stops using
     * the Item before the action is complete.
     */
    public ItemStack onItemUseFinish(ItemStack stack, World worldIn, EntityNPC playerIn)
    {
//        if(!playerIn.creative)
        --stack.size;
        worldIn.playSoundAtEntity(playerIn, SoundEvent.EAT, 0.5F);
        playerIn.heal((int)((float)this.getHealAmount(stack) * 0.5f * (1.0f + worldIn.rand.floatv())));
        this.onFoodEaten(stack, worldIn, playerIn);
//        playerIn.triggerAchievement(StatRegistry.objectUseStats[ItemRegistry.getIdFromItem(this)]);
        return stack;
    }

    protected void onFoodEaten(ItemStack stack, World worldIn, EntityNPC player)
    {
        if (!worldIn.client && this.potionId != null && worldIn.rand.floatv() < this.potionEffectProbability)
        {
            player.addEffect(new PotionEffect(this.potionId, this.potionDuration * 20, this.potionAmplifier));
        }
    }

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

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

    /**
     * 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;
    }

    public int getHealAmount(ItemStack stack)
    {
        return this.healAmount;
    }

    /**
     * Whether wolves like this food (true for raw and cooked porkchop).
     */
    public boolean isWolfsFavoriteMeat()
    {
        return this.isWolfsFavoriteMeat;
    }

    /**
     * sets a potion effect on the item. Args: int potionId, int duration (will be multiplied by 20), int amplifier,
     * float probability of effect happening
     */
    public ItemFood setPotionEffect(Potion id, int duration, int amplifier, float probability)
    {
        this.potionId = id;
        this.potionDuration = duration;
        this.potionAmplifier = amplifier;
        this.potionEffectProbability = probability;
        return this;
    }
}
