package net.minecraft.world.item;

import java.util.function.Predicate;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.InteractionHand;
import net.minecraft.stats.Stat;
import net.minecraft.stats.Stats;
import net.minecraft.sounds.SoundSource;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.entity.projectile.AbstractArrow;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;
import net.minecraft.resources.ResourceLocation;

public class BowItem extends ProjectileWeaponItem {
    public BowItem(final Properties a) {
        super(a);
        this.addProperty(new ResourceLocation("pull"), (ben, bjw, akz) -> {
            if (akz == null) {
                return 0.0f;
            }
            else if (akz.getUseItem().getItem() != Items.BOW) {
                return 0.0f;
            }
            else {
                return (ben.getUseDuration() - akz.getUseItemRemainingTicks()) / 20.0f;
            }
        });
        this.addProperty(new ResourceLocation("pulling"), (ben, bjw, akz) -> (akz != null && akz.isUsingItem() && akz.getUseItem() == ben) ? 1.0f : 0.0f);
    }
    
    @Override
    public void releaseUsing(final ItemStack ben, final Level bjw, final LivingEntity akz, final int integer) {
        if (!(akz instanceof Player)) {
            return;
        }
        final Player ayj3 = (Player)akz;
        final boolean boolean7 = ayj3.abilities.instabuild || EnchantmentHelper.getItemEnchantmentLevel(Enchantments.INFINITY_ARROWS, ben) > 0;
        ItemStack ben2 = ayj3.getProjectile(ben);
        if (ben2.isEmpty() && !boolean7) {
            return;
        }
        if (ben2.isEmpty()) {
            ben2 = new ItemStack(Items.ARROW);
        }
        final int integer2 = this.getUseDuration(ben) - integer;
        final float float10 = getPowerForTime(integer2);
        if (float10 < 0.1) {
            return;
        }
        final boolean boolean8 = boolean7 && ben2.getItem() == Items.ARROW;
        if (!bjw.isClientSide) {
            final ArrowItem bck12 = (ArrowItem)((ben2.getItem() instanceof ArrowItem) ? ben2.getItem() : Items.ARROW);
            final AbstractArrow ayn13 = bck12.createArrow(bjw, ben2, ayj3);
            ayn13.shootFromRotation(ayj3, ayj3.xRot, ayj3.yRot, 0.0f, float10 * 3.0f, 1.0f);
            if (float10 == 1.0f) {
                ayn13.setCritArrow(true);
            }
            final int integer3 = EnchantmentHelper.getItemEnchantmentLevel(Enchantments.POWER_ARROWS, ben);
            if (integer3 > 0) {
                ayn13.setBaseDamage(ayn13.getBaseDamage() + integer3 * 0.5 + 0.5);
            }
            final int integer4 = EnchantmentHelper.getItemEnchantmentLevel(Enchantments.PUNCH_ARROWS, ben);
            if (integer4 > 0) {
                ayn13.setKnockback(integer4);
            }
            if (EnchantmentHelper.getItemEnchantmentLevel(Enchantments.FLAMING_ARROWS, ben) > 0) {
                ayn13.setSecondsOnFire(100);
            }
            ben.<Player>hurtAndBreak(1, ayj3, ayj2 -> ayj2.broadcastBreakEvent(ayj3.getUsedItemHand()));
            if (boolean8 || (ayj3.abilities.instabuild && (ben2.getItem() == Items.SPECTRAL_ARROW || ben2.getItem() == Items.TIPPED_ARROW))) {
                ayn13.pickup = AbstractArrow.Pickup.CREATIVE_ONLY;
            }
            bjw.addFreshEntity(ayn13);
        }
        bjw.playSound(null, ayj3.getX(), ayj3.getY(), ayj3.getZ(), SoundEvents.ARROW_SHOOT, SoundSource.PLAYERS, 1.0f, 1.0f / (BowItem.random.nextFloat() * 0.4f + 1.2f) + float10 * 0.5f);
        if (!boolean8 && !ayj3.abilities.instabuild) {
            ben2.shrink(1);
            if (ben2.isEmpty()) {
                ayj3.inventory.removeItem(ben2);
            }
        }
        ayj3.awardStat(Stats.ITEM_USED.get(this));
    }
    
    public static float getPowerForTime(final int integer) {
        float float2 = integer / 20.0f;
        float2 = (float2 * float2 + float2 * 2.0f) / 3.0f;
        if (float2 > 1.0f) {
            float2 = 1.0f;
        }
        return float2;
    }
    
    @Override
    public int getUseDuration(final ItemStack ben) {
        return 72000;
    }
    
    @Override
    public UseAnim getUseAnimation(final ItemStack ben) {
        return UseAnim.BOW;
    }
    
    @Override
    public InteractionResultHolder<ItemStack> use(final Level bjw, final Player ayj, final InteractionHand ajk) {
        final ItemStack ben5 = ayj.getItemInHand(ajk);
        final boolean boolean6 = !ayj.getProjectile(ben5).isEmpty();
        if (ayj.abilities.instabuild || boolean6) {
            ayj.startUsingItem(ajk);
            return InteractionResultHolder.<ItemStack>consume(ben5);
        }
        return InteractionResultHolder.<ItemStack>fail(ben5);
    }
    
    @Override
    public Predicate<ItemStack> getAllSupportedProjectiles() {
        return BowItem.ARROW_ONLY;
    }
}
