package com.zivilon.cinder_loe.items;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemArmor;
import net.minecraft.item.ItemFood;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;

public class FieldRepairKit extends ItemFood {

    private int damageAmount;
    public FieldRepairKit() {
        super(0, 0.0F, false); // No hunger, no saturation
        this.setAlwaysEdible(); // Allow eating even at full hunger
    }

    @Override
    public int getItemStackLimit() {
        return 1;
    }

    @Override
    public ItemStack onEaten(ItemStack stack, World world, EntityPlayer player) {
        if (!world.isRemote) {
            world.playSoundAtEntity(player, "random.anvil_break", 1.0F, 1.0F);
        }
        return super.onEaten(stack, world, player);
    }

    @Override
    protected void onFoodEaten(ItemStack stack, World world, EntityPlayer player) {
        if (!world.isRemote) {
            for (int i = 0; i < 4; i++) {
                ItemStack armor = player.getCurrentArmor(i);
                if (armor != null && armor.getItem() instanceof ItemArmor && armor.isItemDamaged()) {
                    int maxDamage = armor.getMaxDamage();
                    int repairAmount = Math.max(1, (int)(maxDamage * 0.05));
                    armor.setItemDamage(Math.max(0, armor.getItemDamage() - repairAmount));
                }
            }
        }
    }

    @Override
    public void onUsingTick(ItemStack stack, EntityPlayer player, int count) {
        if (!player.worldObj.isRemote && count % 20 == 0) {
            player.worldObj.playSoundAtEntity(player, "random.anvil_use", 0.7F, 1.0F);
        }
    }

    @Override
    public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) {
        player.setItemInUse(stack, this.getMaxItemUseDuration(stack));
        return stack;
    }

    @Override
    public int getMaxItemUseDuration(ItemStack stack) {
        return 200;
    }
}