package net.minecraft.world.entity.animal;

import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import javax.annotation.Nullable;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.player.Player;
import java.util.Random;
import net.minecraft.world.entity.MobSpawnType;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.LevelReader;
import net.minecraft.core.BlockPos;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.world.level.Level;
import net.minecraft.world.entity.EntityType;
import java.util.UUID;
import net.minecraft.world.entity.AgableMob;

public abstract class Animal extends AgableMob {
    private int inLove;
    private UUID loveCause;
    
    protected Animal(final EntityType<? extends Animal> aku, final Level bjw) {
        super(aku, bjw);
    }
    
    @Override
    protected void customServerAiStep() {
        if (this.getAge() != 0) {
            this.inLove = 0;
        }
        super.customServerAiStep();
    }
    
    @Override
    public void aiStep() {
        super.aiStep();
        if (this.getAge() != 0) {
            this.inLove = 0;
        }
        if (this.inLove > 0) {
            --this.inLove;
            if (this.inLove % 10 == 0) {
                final double double2 = this.random.nextGaussian() * 0.02;
                final double double3 = this.random.nextGaussian() * 0.02;
                final double double4 = this.random.nextGaussian() * 0.02;
                this.level.addParticle(ParticleTypes.HEART, this.getRandomX(1.0), this.getRandomY() + 0.5, this.getRandomZ(1.0), double2, double3, double4);
            }
        }
    }
    
    @Override
    public boolean hurt(final DamageSource ajz, final float float2) {
        if (this.isInvulnerableTo(ajz)) {
            return false;
        }
        this.inLove = 0;
        return super.hurt(ajz, float2);
    }
    
    @Override
    public float getWalkTargetValue(final BlockPos fk, final LevelReader bjz) {
        if (bjz.getBlockState(fk.below()).getBlock() == Blocks.GRASS_BLOCK) {
            return 10.0f;
        }
        return bjz.getBrightness(fk) - 0.5f;
    }
    
    @Override
    public void addAdditionalSaveData(final CompoundTag jt) {
        super.addAdditionalSaveData(jt);
        jt.putInt("InLove", this.inLove);
        if (this.loveCause != null) {
            jt.putUUID("LoveCause", this.loveCause);
        }
    }
    
    @Override
    public double getRidingHeight() {
        return 0.14;
    }
    
    @Override
    public void readAdditionalSaveData(final CompoundTag jt) {
        super.readAdditionalSaveData(jt);
        this.inLove = jt.getInt("InLove");
        this.loveCause = (jt.hasUUID("LoveCause") ? jt.getUUID("LoveCause") : null);
    }
    
    public static boolean checkAnimalSpawnRules(final EntityType<? extends Animal> aku, final LevelAccessor bjx, final MobSpawnType alc, final BlockPos fk, final Random random) {
        return bjx.getBlockState(fk.below()).getBlock() == Blocks.GRASS_BLOCK && bjx.getRawBrightness(fk, 0) > 8;
    }
    
    @Override
    public int getAmbientSoundInterval() {
        return 120;
    }
    
    @Override
    public boolean removeWhenFarAway(final double double1) {
        return false;
    }
    
    @Override
    protected int getExperienceReward(final Player ayj) {
        return 1 + this.level.random.nextInt(3);
    }
    
    public boolean isFood(final ItemStack ben) {
        return ben.getItem() == Items.WHEAT;
    }
    
    @Override
    public boolean mobInteract(final Player ayj, final InteractionHand ajk) {
        final ItemStack ben4 = ayj.getItemInHand(ajk);
        if (this.isFood(ben4)) {
            if (!this.level.isClientSide && this.getAge() == 0 && this.canFallInLove()) {
                this.usePlayerItem(ayj, ben4);
                this.setInLove(ayj);
                ayj.swing(ajk, true);
                return true;
            }
            if (this.isBaby()) {
                this.usePlayerItem(ayj, ben4);
                this.ageUp((int)(-this.getAge() / 20 * 0.1f), true);
                return true;
            }
        }
        return super.mobInteract(ayj, ajk);
    }
    
    protected void usePlayerItem(final Player ayj, final ItemStack ben) {
        if (!ayj.abilities.instabuild) {
            ben.shrink(1);
        }
    }
    
    public boolean canFallInLove() {
        return this.inLove <= 0;
    }
    
    public void setInLove(@Nullable final Player ayj) {
        this.inLove = 600;
        if (ayj != null) {
            this.loveCause = ayj.getUUID();
        }
        this.level.broadcastEntityEvent(this, (byte)18);
    }
    
    public void setInLoveTime(final int integer) {
        this.inLove = integer;
    }
    
    @Nullable
    public ServerPlayer getLoveCause() {
        if (this.loveCause == null) {
            return null;
        }
        final Player ayj2 = this.level.getPlayerByUUID(this.loveCause);
        if (ayj2 instanceof ServerPlayer) {
            return (ServerPlayer)ayj2;
        }
        return null;
    }
    
    public boolean isInLove() {
        return this.inLove > 0;
    }
    
    public void resetLove() {
        this.inLove = 0;
    }
    
    public boolean canMate(final Animal atc) {
        return atc != this && atc.getClass() == this.getClass() && this.isInLove() && atc.isInLove();
    }
    
    @Override
    public void handleEntityEvent(final byte byte1) {
        if (byte1 == 18) {
            for (int integer3 = 0; integer3 < 7; ++integer3) {
                final double double4 = this.random.nextGaussian() * 0.02;
                final double double5 = this.random.nextGaussian() * 0.02;
                final double double6 = this.random.nextGaussian() * 0.02;
                this.level.addParticle(ParticleTypes.HEART, this.getRandomX(1.0), this.getRandomY() + 0.5, this.getRandomZ(1.0), double4, double5, double6);
            }
        }
        else {
            super.handleEntityEvent(byte1);
        }
    }
}
