package net.minecraft.world.entity.monster;

import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.world.entity.EntityDimensions;
import net.minecraft.world.entity.Pose;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.entity.projectile.ThrownPotion;
import net.minecraft.util.Mth;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
import net.minecraft.world.item.alchemy.Potion;
import java.util.Iterator;
import java.util.List;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.alchemy.Potions;
import net.minecraft.world.effect.MobEffects;
import net.minecraft.tags.FluidTags;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.item.alchemy.PotionUtils;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal;
import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;
import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
import net.minecraft.world.entity.PathfinderMob;
import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;
import net.minecraft.world.entity.ai.goal.RangedAttackGoal;
import net.minecraft.world.entity.ai.goal.Goal;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.ai.goal.FloatGoal;
import net.minecraft.world.entity.LivingEntity;
import java.util.function.Predicate;
import net.minecraft.world.level.Level;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.ai.goal.target.NearestAttackableWitchTargetGoal;
import net.minecraft.world.entity.ai.goal.target.NearestHealableRaiderTargetGoal;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import java.util.UUID;
import net.minecraft.world.entity.raid.Raider;

public class Witch extends Raider implements RangedAttackMob {
    private static final UUID SPEED_MODIFIER_DRINKING_UUID;
    private static final AttributeModifier SPEED_MODIFIER_DRINKING;
    private static final EntityDataAccessor<Boolean> DATA_USING_ITEM;
    private int usingTime;
    private NearestHealableRaiderTargetGoal<Raider> healRaidersGoal;
    private NearestAttackableWitchTargetGoal<Player> attackPlayersGoal;
    
    public Witch(final EntityType<? extends Witch> aku, final Level bjw) {
        super(aku, bjw);
    }
    
    @Override
    protected void registerGoals() {
        super.registerGoals();
        this.healRaidersGoal = new NearestHealableRaiderTargetGoal<Raider>(this, Raider.class, true, akz -> akz != null && this.hasActiveRaid() && akz.getType() != EntityType.WITCH);
        this.attackPlayersGoal = new NearestAttackableWitchTargetGoal<Player>(this, Player.class, 10, true, false, null);
        this.goalSelector.addGoal(1, new FloatGoal(this));
        this.goalSelector.addGoal(2, new RangedAttackGoal(this, 1.0, 60, 10.0f));
        this.goalSelector.addGoal(2, new WaterAvoidingRandomStrollGoal(this, 1.0));
        this.goalSelector.addGoal(3, new LookAtPlayerGoal(this, Player.class, 8.0f));
        this.goalSelector.addGoal(3, new RandomLookAroundGoal(this));
        this.targetSelector.addGoal(1, new HurtByTargetGoal(this, new Class[] { Raider.class }));
        this.targetSelector.addGoal(2, this.healRaidersGoal);
        this.targetSelector.addGoal(3, this.attackPlayersGoal);
    }
    
    @Override
    protected void defineSynchedData() {
        super.defineSynchedData();
        this.getEntityData().<Boolean>define(Witch.DATA_USING_ITEM, false);
    }
    
    @Override
    protected SoundEvent getAmbientSound() {
        return SoundEvents.WITCH_AMBIENT;
    }
    
    @Override
    protected SoundEvent getHurtSound(final DamageSource ajz) {
        return SoundEvents.WITCH_HURT;
    }
    
    @Override
    protected SoundEvent getDeathSound() {
        return SoundEvents.WITCH_DEATH;
    }
    
    public void setUsingItem(final boolean boolean1) {
        this.getEntityData().<Boolean>set(Witch.DATA_USING_ITEM, boolean1);
    }
    
    public boolean isDrinkingPotion() {
        return this.getEntityData().<Boolean>get(Witch.DATA_USING_ITEM);
    }
    
    @Override
    protected void registerAttributes() {
        super.registerAttributes();
        this.getAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(26.0);
        this.getAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.25);
    }
    
    @Override
    public void aiStep() {
        if (!this.level.isClientSide && this.isAlive()) {
            this.healRaidersGoal.decrementCooldown();
            if (this.healRaidersGoal.getCooldown() <= 0) {
                this.attackPlayersGoal.setCanAttack(true);
            }
            else {
                this.attackPlayersGoal.setCanAttack(false);
            }
            if (this.isDrinkingPotion()) {
                if (this.usingTime-- <= 0) {
                    this.setUsingItem(false);
                    final ItemStack ben2 = this.getMainHandItem();
                    this.setItemSlot(EquipmentSlot.MAINHAND, ItemStack.EMPTY);
                    if (ben2.getItem() == Items.POTION) {
                        final List<MobEffectInstance> list3 = PotionUtils.getMobEffects(ben2);
                        if (list3 != null) {
                            for (final MobEffectInstance akk5 : list3) {
                                this.addEffect(new MobEffectInstance(akk5));
                            }
                        }
                    }
                    this.getAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).removeModifier(Witch.SPEED_MODIFIER_DRINKING);
                }
            }
            else {
                Potion bgd2 = null;
                if (this.random.nextFloat() < 0.15f && this.isUnderLiquid(FluidTags.WATER) && !this.hasEffect(MobEffects.WATER_BREATHING)) {
                    bgd2 = Potions.WATER_BREATHING;
                }
                else if (this.random.nextFloat() < 0.15f && (this.isOnFire() || (this.getLastDamageSource() != null && this.getLastDamageSource().isFire())) && !this.hasEffect(MobEffects.FIRE_RESISTANCE)) {
                    bgd2 = Potions.FIRE_RESISTANCE;
                }
                else if (this.random.nextFloat() < 0.05f && this.getHealth() < this.getMaxHealth()) {
                    bgd2 = Potions.HEALING;
                }
                else if (this.random.nextFloat() < 0.5f && this.getTarget() != null && !this.hasEffect(MobEffects.MOVEMENT_SPEED) && this.getTarget().distanceToSqr(this) > 121.0) {
                    bgd2 = Potions.SWIFTNESS;
                }
                if (bgd2 != null) {
                    this.setItemSlot(EquipmentSlot.MAINHAND, PotionUtils.setPotion(new ItemStack(Items.POTION), bgd2));
                    this.usingTime = this.getMainHandItem().getUseDuration();
                    this.setUsingItem(true);
                    this.level.playSound(null, this.getX(), this.getY(), this.getZ(), SoundEvents.WITCH_DRINK, this.getSoundSource(), 1.0f, 0.8f + this.random.nextFloat() * 0.4f);
                    final AttributeInstance alr3 = this.getAttribute(SharedMonsterAttributes.MOVEMENT_SPEED);
                    alr3.removeModifier(Witch.SPEED_MODIFIER_DRINKING);
                    alr3.addModifier(Witch.SPEED_MODIFIER_DRINKING);
                }
            }
            if (this.random.nextFloat() < 7.5E-4f) {
                this.level.broadcastEntityEvent(this, (byte)15);
            }
        }
        super.aiStep();
    }
    
    @Override
    public SoundEvent getCelebrateSound() {
        return SoundEvents.WITCH_CELEBRATE;
    }
    
    @Override
    public void handleEntityEvent(final byte byte1) {
        if (byte1 == 15) {
            for (int integer3 = 0; integer3 < this.random.nextInt(35) + 10; ++integer3) {
                this.level.addParticle(ParticleTypes.WITCH, this.getX() + this.random.nextGaussian() * 0.12999999523162842, this.getBoundingBox().maxY + 0.5 + this.random.nextGaussian() * 0.12999999523162842, this.getZ() + this.random.nextGaussian() * 0.12999999523162842, 0.0, 0.0, 0.0);
            }
        }
        else {
            super.handleEntityEvent(byte1);
        }
    }
    
    @Override
    protected float getDamageAfterMagicAbsorb(final DamageSource ajz, float float2) {
        float2 = super.getDamageAfterMagicAbsorb(ajz, float2);
        if (ajz.getEntity() == this) {
            float2 = 0.0f;
        }
        if (ajz.isMagic()) {
            float2 *= (float)0.15;
        }
        return float2;
    }
    
    @Override
    public void performRangedAttack(final LivingEntity akz, final float float2) {
        if (this.isDrinkingPotion()) {
            return;
        }
        final Vec3 cvl4 = akz.getDeltaMovement();
        final double double5 = akz.getX() + cvl4.x - this.getX();
        final double double6 = akz.getEyeY() - 1.100000023841858 - this.getY();
        final double double7 = akz.getZ() + cvl4.z - this.getZ();
        final float float3 = Mth.sqrt(double5 * double5 + double7 * double7);
        Potion bgd12 = Potions.HARMING;
        if (akz instanceof Raider) {
            if (akz.getHealth() <= 4.0f) {
                bgd12 = Potions.HEALING;
            }
            else {
                bgd12 = Potions.REGENERATION;
            }
            this.setTarget(null);
        }
        else if (float3 >= 8.0f && !akz.hasEffect(MobEffects.MOVEMENT_SLOWDOWN)) {
            bgd12 = Potions.SLOWNESS;
        }
        else if (akz.getHealth() >= 8.0f && !akz.hasEffect(MobEffects.POISON)) {
            bgd12 = Potions.POISON;
        }
        else if (float3 <= 3.0f && !akz.hasEffect(MobEffects.WEAKNESS) && this.random.nextFloat() < 0.25f) {
            bgd12 = Potions.WEAKNESS;
        }
        final ThrownPotion azj13 = new ThrownPotion(this.level, this);
        azj13.setItem(PotionUtils.setPotion(new ItemStack(Items.SPLASH_POTION), bgd12));
        final ThrownPotion thrownPotion = azj13;
        thrownPotion.xRot += 20.0f;
        azj13.shoot(double5, double6 + float3 * 0.2f, double7, 0.75f, 8.0f);
        this.level.playSound(null, this.getX(), this.getY(), this.getZ(), SoundEvents.WITCH_THROW, this.getSoundSource(), 1.0f, 0.8f + this.random.nextFloat() * 0.4f);
        this.level.addFreshEntity(azj13);
    }
    
    @Override
    protected float getStandingEyeHeight(final Pose alj, final EntityDimensions akr) {
        return 1.62f;
    }
    
    @Override
    public void applyRaidBuffs(final int integer, final boolean boolean2) {
    }
    
    @Override
    public boolean canBeLeader() {
        return false;
    }
    
    static {
        SPEED_MODIFIER_DRINKING_UUID = UUID.fromString("5CD17E52-A79A-43D3-A529-90FDE04B181E");
        SPEED_MODIFIER_DRINKING = new AttributeModifier(Witch.SPEED_MODIFIER_DRINKING_UUID, "Drinking speed penalty", -0.25, AttributeModifier.Operation.ADDITION).setSerialize(false);
        DATA_USING_ITEM = SynchedEntityData.<Boolean>defineId(Witch.class, EntityDataSerializers.BOOLEAN);
    }
}
