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

import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.Items;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import javax.annotation.Nullable;
import net.minecraft.world.entity.AgableMob;
import net.minecraft.world.entity.ai.goal.Goal;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.MobType;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.tags.Tag;
import net.minecraft.tags.FluidTags;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
import net.minecraft.world.level.Level;
import net.minecraft.world.entity.EntityType;

public class SkeletonHorse extends AbstractHorse {
    private final SkeletonTrapGoal skeletonTrapGoal;
    private boolean isTrap;
    private int trapTime;
    
    public SkeletonHorse(final EntityType<? extends SkeletonHorse> aoq, final Level bqb) {
        super(aoq, bqb);
        this.skeletonTrapGoal = new SkeletonTrapGoal(this);
    }
    
    public static AttributeSupplier.Builder createAttributes() {
        return AbstractHorse.createBaseHorseAttributes().add(Attributes.MAX_HEALTH, 15.0).add(Attributes.MOVEMENT_SPEED, 0.20000000298023224);
    }
    
    @Override
    protected void randomizeAttributes() {
        this.getAttribute(Attributes.JUMP_STRENGTH).setBaseValue(this.generateRandomJumpStrength());
    }
    
    @Override
    protected void addBehaviourGoals() {
    }
    
    @Override
    protected SoundEvent getAmbientSound() {
        super.getAmbientSound();
        if (this.isEyeInFluid(FluidTags.WATER)) {
            return SoundEvents.SKELETON_HORSE_AMBIENT_WATER;
        }
        return SoundEvents.SKELETON_HORSE_AMBIENT;
    }
    
    @Override
    protected SoundEvent getDeathSound() {
        super.getDeathSound();
        return SoundEvents.SKELETON_HORSE_DEATH;
    }
    
    @Override
    protected SoundEvent getHurtSound(final DamageSource anw) {
        super.getHurtSound(anw);
        return SoundEvents.SKELETON_HORSE_HURT;
    }
    
    @Override
    protected SoundEvent getSwimSound() {
        if (this.onGround) {
            if (!this.isVehicle()) {
                return SoundEvents.SKELETON_HORSE_STEP_WATER;
            }
            ++this.gallopSoundCounter;
            if (this.gallopSoundCounter > 5 && this.gallopSoundCounter % 3 == 0) {
                return SoundEvents.SKELETON_HORSE_GALLOP_WATER;
            }
            if (this.gallopSoundCounter <= 5) {
                return SoundEvents.SKELETON_HORSE_STEP_WATER;
            }
        }
        return SoundEvents.SKELETON_HORSE_SWIM;
    }
    
    @Override
    protected void playSwimSound(final float float1) {
        if (this.onGround) {
            super.playSwimSound(0.3f);
        }
        else {
            super.playSwimSound(Math.min(0.1f, float1 * 25.0f));
        }
    }
    
    @Override
    protected void playJumpSound() {
        if (this.isInWater()) {
            this.playSound(SoundEvents.SKELETON_HORSE_JUMP_WATER, 0.4f, 1.0f);
        }
        else {
            super.playJumpSound();
        }
    }
    
    @Override
    public MobType getMobType() {
        return MobType.UNDEAD;
    }
    
    @Override
    public double getPassengersRidingOffset() {
        return super.getPassengersRidingOffset() - 0.1875;
    }
    
    @Override
    public void aiStep() {
        super.aiStep();
        if (this.isTrap() && this.trapTime++ >= 18000) {
            this.remove();
        }
    }
    
    @Override
    public void addAdditionalSaveData(final CompoundTag le) {
        super.addAdditionalSaveData(le);
        le.putBoolean("SkeletonTrap", this.isTrap());
        le.putInt("SkeletonTrapTime", this.trapTime);
    }
    
    @Override
    public void readAdditionalSaveData(final CompoundTag le) {
        super.readAdditionalSaveData(le);
        this.setTrap(le.getBoolean("SkeletonTrap"));
        this.trapTime = le.getInt("SkeletonTrapTime");
    }
    
    @Override
    public boolean rideableUnderWater() {
        return true;
    }
    
    @Override
    protected float getWaterSlowDown() {
        return 0.96f;
    }
    
    public boolean isTrap() {
        return this.isTrap;
    }
    
    public void setTrap(final boolean boolean1) {
        if (boolean1 == this.isTrap) {
            return;
        }
        this.isTrap = boolean1;
        if (boolean1) {
            this.goalSelector.addGoal(1, this.skeletonTrapGoal);
        }
        else {
            this.goalSelector.removeGoal(this.skeletonTrapGoal);
        }
    }
    
    @Nullable
    @Override
    public AgableMob getBreedOffspring(final AgableMob aok) {
        return EntityType.SKELETON_HORSE.create(this.level);
    }
    
    @Override
    public InteractionResult mobInteract(final Player bec, final InteractionHand anf) {
        final ItemStack bki4 = bec.getItemInHand(anf);
        if (!this.isTamed()) {
            return InteractionResult.PASS;
        }
        if (this.isBaby()) {
            return super.mobInteract(bec, anf);
        }
        if (bec.isSecondaryUseActive()) {
            this.openInventory(bec);
            return InteractionResult.sidedSuccess(this.level.isClientSide);
        }
        if (this.isVehicle()) {
            return super.mobInteract(bec, anf);
        }
        if (!bki4.isEmpty()) {
            if (bki4.getItem() == Items.SADDLE && !this.isSaddled()) {
                this.openInventory(bec);
                return InteractionResult.sidedSuccess(this.level.isClientSide);
            }
            final InteractionResult ang5 = bki4.interactLivingEntity(bec, this, anf);
            if (ang5.consumesAction()) {
                return ang5;
            }
        }
        this.doPlayerRide(bec);
        return InteractionResult.sidedSuccess(this.level.isClientSide);
    }
}
