package net.minecraft.world.entity.animal.horse;

import net.minecraft.world.entity.ai.targeting.TargetingConditions;
import java.util.EnumSet;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.goal.target.TargetGoal;
import net.minecraft.world.entity.AgableMob;
import javax.annotation.Nullable;
import net.minecraft.world.entity.SpawnGroupData;
import net.minecraft.world.entity.MobSpawnType;
import net.minecraft.world.DifficultyInstance;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.npc.WanderingTrader;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.ai.goal.Goal;
import net.minecraft.world.entity.PathfinderMob;
import net.minecraft.world.entity.ai.goal.PanicGoal;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.Level;
import net.minecraft.world.entity.EntityType;

public class TraderLlama extends Llama {
    private int despawnDelay;
    
    public TraderLlama(final EntityType<? extends TraderLlama> aoq, final Level bqb) {
        super(aoq, bqb);
        this.despawnDelay = 47999;
    }
    
    @Override
    public boolean isTraderLlama() {
        return true;
    }
    
    @Override
    protected Llama makeBabyLlama() {
        return EntityType.TRADER_LLAMA.create(this.level);
    }
    
    @Override
    public void addAdditionalSaveData(final CompoundTag le) {
        super.addAdditionalSaveData(le);
        le.putInt("DespawnDelay", this.despawnDelay);
    }
    
    @Override
    public void readAdditionalSaveData(final CompoundTag le) {
        super.readAdditionalSaveData(le);
        if (le.contains("DespawnDelay", 99)) {
            this.despawnDelay = le.getInt("DespawnDelay");
        }
    }
    
    @Override
    protected void registerGoals() {
        super.registerGoals();
        this.goalSelector.addGoal(1, new PanicGoal(this, 2.0));
        this.targetSelector.addGoal(1, new TraderLlamaDefendWanderingTraderGoal(this));
    }
    
    @Override
    protected void doPlayerRide(final Player bec) {
        final Entity aom3 = this.getLeashHolder();
        if (aom3 instanceof WanderingTrader) {
            return;
        }
        super.doPlayerRide(bec);
    }
    
    @Override
    public void aiStep() {
        super.aiStep();
        if (!this.level.isClientSide) {
            this.maybeDespawn();
        }
    }
    
    private void maybeDespawn() {
        if (!this.canDespawn()) {
            return;
        }
        this.despawnDelay = (this.isLeashedToWanderingTrader() ? (((WanderingTrader)this.getLeashHolder()).getDespawnDelay() - 1) : (this.despawnDelay - 1));
        if (this.despawnDelay <= 0) {
            this.dropLeash(true, false);
            this.remove();
        }
    }
    
    private boolean canDespawn() {
        return !this.isTamed() && !this.isLeashedToSomethingOtherThanTheWanderingTrader() && !this.hasOnePlayerPassenger();
    }
    
    private boolean isLeashedToWanderingTrader() {
        return this.getLeashHolder() instanceof WanderingTrader;
    }
    
    private boolean isLeashedToSomethingOtherThanTheWanderingTrader() {
        return this.isLeashed() && !this.isLeashedToWanderingTrader();
    }
    
    @Nullable
    @Override
    public SpawnGroupData finalizeSpawn(final LevelAccessor bqc, final DifficultyInstance ane, final MobSpawnType apb, @Nullable SpawnGroupData apo, @Nullable final CompoundTag le) {
        if (apb == MobSpawnType.EVENT) {
            this.setAge(0);
        }
        if (apo == null) {
            apo = new AgableMobGroupData();
            ((AgableMobGroupData)apo).setShouldSpawnBaby(false);
        }
        return super.finalizeSpawn(bqc, ane, apb, apo, le);
    }
    
    public class TraderLlamaDefendWanderingTraderGoal extends TargetGoal {
        private final Llama llama;
        private LivingEntity ownerLastHurtBy;
        private int timestamp;
        
        public TraderLlamaDefendWanderingTraderGoal(final Llama azp) {
            super(azp, false);
            this.llama = azp;
            this.setFlags(EnumSet.<Flag>of(Flag.TARGET));
        }
        
        @Override
        public boolean canUse() {
            if (!this.llama.isLeashed()) {
                return false;
            }
            final Entity aom2 = this.llama.getLeashHolder();
            if (!(aom2 instanceof WanderingTrader)) {
                return false;
            }
            final WanderingTrader bdv3 = (WanderingTrader)aom2;
            this.ownerLastHurtBy = bdv3.getLastHurtByMob();
            final int integer4 = bdv3.getLastHurtByMobTimestamp();
            return integer4 != this.timestamp && this.canAttack(this.ownerLastHurtBy, TargetingConditions.DEFAULT);
        }
        
        @Override
        public void start() {
            this.mob.setTarget(this.ownerLastHurtBy);
            final Entity aom2 = this.llama.getLeashHolder();
            if (aom2 instanceof WanderingTrader) {
                this.timestamp = ((WanderingTrader)aom2).getLastHurtByMobTimestamp();
            }
            super.start();
        }
    }
}
