package net.minecraft.world.entity.projectile;

import net.minecraft.world.level.block.state.StateHolder;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.block.CampfireBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.tags.Tag;
import net.minecraft.tags.BlockTags;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.AreaEffectCloud;
import net.minecraft.world.effect.MobEffect;
import javax.annotation.Nullable;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.phys.EntityHitResult;
import net.minecraft.world.phys.HitResult;
import java.util.Iterator;
import net.minecraft.core.BlockPos;
import net.minecraft.world.effect.MobEffectInstance;
import java.util.List;
import net.minecraft.world.item.alchemy.Potion;
import net.minecraft.world.item.ItemStack;
import net.minecraft.core.Direction;
import net.minecraft.world.item.alchemy.Potions;
import net.minecraft.world.item.alchemy.PotionUtils;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.Level;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import java.util.function.Predicate;

public class ThrownPotion extends ThrowableItemProjectile implements ItemSupplier {
    public static final Predicate<LivingEntity> WATER_SENSITIVE;
    
    public ThrownPotion(final EntityType<? extends ThrownPotion> aoq, final Level bqb) {
        super(aoq, bqb);
    }
    
    public ThrownPotion(final Level bqb, final LivingEntity aoy) {
        super(EntityType.POTION, aoy, bqb);
    }
    
    public ThrownPotion(final Level bqb, final double double2, final double double3, final double double4) {
        super(EntityType.POTION, double2, double3, double4, bqb);
    }
    
    @Override
    protected Item getDefaultItem() {
        return Items.SPLASH_POTION;
    }
    
    @Override
    protected float getGravity() {
        return 0.05f;
    }
    
    @Override
    protected void onHitBlock(final BlockHitResult deh) {
        super.onHitBlock(deh);
        if (this.level.isClientSide) {
            return;
        }
        final ItemStack bki3 = this.getItem();
        final Potion bmb4 = PotionUtils.getPotion(bki3);
        final List<MobEffectInstance> list5 = PotionUtils.getMobEffects(bki3);
        final boolean boolean6 = bmb4 == Potions.WATER && list5.isEmpty();
        final Direction fz7 = deh.getDirection();
        final BlockPos fu8 = deh.getBlockPos();
        final BlockPos fu9 = fu8.relative(fz7);
        if (boolean6) {
            this.dowseFire(fu9, fz7);
            this.dowseFire(fu9.relative(fz7.getOpposite()), fz7);
            for (final Direction fz8 : Direction.Plane.HORIZONTAL) {
                this.dowseFire(fu9.relative(fz8), fz8);
            }
        }
    }
    
    @Override
    protected void onHit(final HitResult dej) {
        super.onHit(dej);
        if (this.level.isClientSide) {
            return;
        }
        final ItemStack bki3 = this.getItem();
        final Potion bmb4 = PotionUtils.getPotion(bki3);
        final List<MobEffectInstance> list5 = PotionUtils.getMobEffects(bki3);
        final boolean boolean6 = bmb4 == Potions.WATER && list5.isEmpty();
        if (boolean6) {
            this.applyWater();
        }
        else if (!list5.isEmpty()) {
            if (this.isLingering()) {
                this.makeAreaOfEffectCloud(bki3, bmb4);
            }
            else {
                this.applySplash(list5, (dej.getType() == HitResult.Type.ENTITY) ? ((EntityHitResult)dej).getEntity() : null);
            }
        }
        final int integer7 = bmb4.hasInstantEffects() ? 2007 : 2002;
        this.level.levelEvent(integer7, this.blockPosition(), PotionUtils.getColor(bki3));
        this.remove();
    }
    
    private void applyWater() {
        final AABB deg2 = this.getBoundingBox().inflate(4.0, 2.0, 4.0);
        final List<LivingEntity> list3 = this.level.<LivingEntity>getEntitiesOfClass(LivingEntity.class, deg2, ThrownPotion.WATER_SENSITIVE);
        if (!list3.isEmpty()) {
            for (final LivingEntity aoy5 : list3) {
                final double double6 = this.distanceToSqr(aoy5);
                if (double6 < 16.0 && aoy5.isSensitiveToWater()) {
                    aoy5.hurt(DamageSource.indirectMagic(aoy5, this.getOwner()), 1.0f);
                }
            }
        }
    }
    
    private void applySplash(final List<MobEffectInstance> list, @Nullable final Entity aom) {
        final AABB deg4 = this.getBoundingBox().inflate(4.0, 2.0, 4.0);
        final List<LivingEntity> list2 = this.level.<LivingEntity>getEntitiesOfClass(LivingEntity.class, deg4);
        if (!list2.isEmpty()) {
            for (final LivingEntity aoy7 : list2) {
                if (!aoy7.isAffectedByPotions()) {
                    continue;
                }
                final double double8 = this.distanceToSqr(aoy7);
                if (double8 >= 16.0) {
                    continue;
                }
                double double9 = 1.0 - Math.sqrt(double8) / 4.0;
                if (aoy7 == aom) {
                    double9 = 1.0;
                }
                for (final MobEffectInstance aog13 : list) {
                    final MobEffect aoe14 = aog13.getEffect();
                    if (aoe14.isInstantenous()) {
                        aoe14.applyInstantenousEffect(this, this.getOwner(), aoy7, aog13.getAmplifier(), double9);
                    }
                    else {
                        final int integer15 = (int)(double9 * aog13.getDuration() + 0.5);
                        if (integer15 <= 20) {
                            continue;
                        }
                        aoy7.addEffect(new MobEffectInstance(aoe14, integer15, aog13.getAmplifier(), aog13.isAmbient(), aog13.isVisible()));
                    }
                }
            }
        }
    }
    
    private void makeAreaOfEffectCloud(final ItemStack bki, final Potion bmb) {
        final AreaEffectCloud aol4 = new AreaEffectCloud(this.level, this.getX(), this.getY(), this.getZ());
        final Entity aom5 = this.getOwner();
        if (aom5 instanceof LivingEntity) {
            aol4.setOwner((LivingEntity)aom5);
        }
        aol4.setRadius(3.0f);
        aol4.setRadiusOnUse(-0.5f);
        aol4.setWaitTime(10);
        aol4.setRadiusPerTick(-aol4.getRadius() / aol4.getDuration());
        aol4.setPotion(bmb);
        for (final MobEffectInstance aog7 : PotionUtils.getCustomEffects(bki)) {
            aol4.addEffect(new MobEffectInstance(aog7));
        }
        final CompoundTag le6 = bki.getTag();
        if (le6 != null && le6.contains("CustomPotionColor", 99)) {
            aol4.setFixedColor(le6.getInt("CustomPotionColor"));
        }
        this.level.addFreshEntity(aol4);
    }
    
    private boolean isLingering() {
        return this.getItem().getItem() == Items.LINGERING_POTION;
    }
    
    private void dowseFire(final BlockPos fu, final Direction fz) {
        final BlockState cfj4 = this.level.getBlockState(fu);
        if (cfj4.is(BlockTags.FIRE)) {
            this.level.removeBlock(fu, false);
        }
        else if (CampfireBlock.isLitCampfire(cfj4)) {
            this.level.levelEvent(null, 1009, fu, 0);
            CampfireBlock.dowse(this.level, fu, cfj4);
            this.level.setBlockAndUpdate(fu, ((StateHolder<O, BlockState>)cfj4).<Comparable, Boolean>setValue((Property<Comparable>)CampfireBlock.LIT, false));
        }
    }
    
    static {
        WATER_SENSITIVE = LivingEntity::isSensitiveToWater;
    }
}
