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

import java.util.AbstractList;
import net.minecraft.world.entity.Entity;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.item.Items;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.nbt.Tag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.level.Level;
import net.minecraft.world.entity.EntityType;
import net.minecraft.network.syncher.EntityDataAccessor;

public abstract class AbstractChestedHorse extends AbstractHorse {
    private static final EntityDataAccessor<Boolean> DATA_ID_CHEST;
    
    protected AbstractChestedHorse(final EntityType<? extends AbstractChestedHorse> aoq, final Level bqb) {
        super(aoq, bqb);
        this.canGallop = false;
    }
    
    @Override
    protected void randomizeAttributes() {
        this.getAttribute(Attributes.MAX_HEALTH).setBaseValue(this.generateRandomMaxHealth());
    }
    
    @Override
    protected void defineSynchedData() {
        super.defineSynchedData();
        this.entityData.<Boolean>define(AbstractChestedHorse.DATA_ID_CHEST, false);
    }
    
    public static AttributeSupplier.Builder createBaseChestedHorseAttributes() {
        return AbstractHorse.createBaseHorseAttributes().add(Attributes.MOVEMENT_SPEED, 0.17499999701976776).add(Attributes.JUMP_STRENGTH, 0.5);
    }
    
    public boolean hasChest() {
        return this.entityData.<Boolean>get(AbstractChestedHorse.DATA_ID_CHEST);
    }
    
    public void setChest(final boolean boolean1) {
        this.entityData.<Boolean>set(AbstractChestedHorse.DATA_ID_CHEST, boolean1);
    }
    
    @Override
    protected int getInventorySize() {
        if (this.hasChest()) {
            return 17;
        }
        return super.getInventorySize();
    }
    
    @Override
    public double getPassengersRidingOffset() {
        return super.getPassengersRidingOffset() - 0.25;
    }
    
    @Override
    protected void dropEquipment() {
        super.dropEquipment();
        if (this.hasChest()) {
            if (!this.level.isClientSide) {
                this.spawnAtLocation(Blocks.CHEST);
            }
            this.setChest(false);
        }
    }
    
    @Override
    public void addAdditionalSaveData(final CompoundTag le) {
        super.addAdditionalSaveData(le);
        le.putBoolean("ChestedHorse", this.hasChest());
        if (this.hasChest()) {
            final ListTag lk3 = new ListTag();
            for (int integer4 = 2; integer4 < this.inventory.getContainerSize(); ++integer4) {
                final ItemStack bki5 = this.inventory.getItem(integer4);
                if (!bki5.isEmpty()) {
                    final CompoundTag le2 = new CompoundTag();
                    le2.putByte("Slot", (byte)integer4);
                    bki5.save(le2);
                    ((AbstractList<CompoundTag>)lk3).add(le2);
                }
            }
            le.put("Items", lk3);
        }
    }
    
    @Override
    public void readAdditionalSaveData(final CompoundTag le) {
        super.readAdditionalSaveData(le);
        this.setChest(le.getBoolean("ChestedHorse"));
        if (this.hasChest()) {
            final ListTag lk3 = le.getList("Items", 10);
            this.createInventory();
            for (int integer4 = 0; integer4 < lk3.size(); ++integer4) {
                final CompoundTag le2 = lk3.getCompound(integer4);
                final int integer5 = le2.getByte("Slot") & 0xFF;
                if (integer5 >= 2 && integer5 < this.inventory.getContainerSize()) {
                    this.inventory.setItem(integer5, ItemStack.of(le2));
                }
            }
        }
        this.updateContainerEquipment();
    }
    
    @Override
    public boolean setSlot(final int integer, final ItemStack bki) {
        if (integer == 499) {
            if (this.hasChest() && bki.isEmpty()) {
                this.setChest(false);
                this.createInventory();
                return true;
            }
            if (!this.hasChest() && bki.getItem() == Blocks.CHEST.asItem()) {
                this.setChest(true);
                this.createInventory();
                return true;
            }
        }
        return super.setSlot(integer, bki);
    }
    
    @Override
    public InteractionResult mobInteract(final Player bec, final InteractionHand anf) {
        final ItemStack bki4 = bec.getItemInHand(anf);
        if (!this.isBaby()) {
            if (this.isTamed() && bec.isSecondaryUseActive()) {
                this.openInventory(bec);
                return InteractionResult.sidedSuccess(this.level.isClientSide);
            }
            if (this.isVehicle()) {
                return super.mobInteract(bec, anf);
            }
        }
        if (!bki4.isEmpty()) {
            if (this.isFood(bki4)) {
                return this.fedFood(bec, bki4);
            }
            if (!this.isTamed()) {
                this.makeMad();
                return InteractionResult.sidedSuccess(this.level.isClientSide);
            }
            if (!this.hasChest() && bki4.getItem() == Blocks.CHEST.asItem()) {
                this.setChest(true);
                this.playChestEquipsSound();
                if (!bec.abilities.instabuild) {
                    bki4.shrink(1);
                }
                this.createInventory();
                return InteractionResult.sidedSuccess(this.level.isClientSide);
            }
            if (!this.isBaby() && !this.isSaddled() && bki4.getItem() == Items.SADDLE) {
                this.openInventory(bec);
                return InteractionResult.sidedSuccess(this.level.isClientSide);
            }
        }
        if (this.isBaby()) {
            return super.mobInteract(bec, anf);
        }
        this.doPlayerRide(bec);
        return InteractionResult.sidedSuccess(this.level.isClientSide);
    }
    
    protected void playChestEquipsSound() {
        this.playSound(SoundEvents.DONKEY_CHEST, 1.0f, (this.random.nextFloat() - this.random.nextFloat()) * 0.2f + 1.0f);
    }
    
    public int getInventoryColumns() {
        return 5;
    }
    
    static {
        DATA_ID_CHEST = SynchedEntityData.<Boolean>defineId(AbstractChestedHorse.class, EntityDataSerializers.BOOLEAN);
    }
}
