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.world.entity.LivingEntity;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.SpawnEggItem;
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.sounds.SoundEvents;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.entity.monster.SharedMonsterAttributes;
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> aku, final Level bjw) {
        super(aku, bjw);
        this.canGallop = false;
    }
    
    @Override
    protected void defineSynchedData() {
        super.defineSynchedData();
        this.entityData.<Boolean>define(AbstractChestedHorse.DATA_ID_CHEST, false);
    }
    
    @Override
    protected void registerAttributes() {
        super.registerAttributes();
        this.getAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(this.generateRandomMaxHealth());
        this.getAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.17499999701976776);
        this.getAttribute(AbstractChestedHorse.JUMP_STRENGTH).setBaseValue(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 getRideHeight() {
        return super.getRideHeight() - 0.25;
    }
    
    @Override
    protected SoundEvent getAngrySound() {
        super.getAngrySound();
        return SoundEvents.DONKEY_ANGRY;
    }
    
    @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 jt) {
        super.addAdditionalSaveData(jt);
        jt.putBoolean("ChestedHorse", this.hasChest());
        if (this.hasChest()) {
            final ListTag jz3 = new ListTag();
            for (int integer4 = 2; integer4 < this.inventory.getContainerSize(); ++integer4) {
                final ItemStack ben5 = this.inventory.getItem(integer4);
                if (!ben5.isEmpty()) {
                    final CompoundTag jt2 = new CompoundTag();
                    jt2.putByte("Slot", (byte)integer4);
                    ben5.save(jt2);
                    ((AbstractList<CompoundTag>)jz3).add(jt2);
                }
            }
            jt.put("Items", jz3);
        }
    }
    
    @Override
    public void readAdditionalSaveData(final CompoundTag jt) {
        super.readAdditionalSaveData(jt);
        this.setChest(jt.getBoolean("ChestedHorse"));
        if (this.hasChest()) {
            final ListTag jz3 = jt.getList("Items", 10);
            this.createInventory();
            for (int integer4 = 0; integer4 < jz3.size(); ++integer4) {
                final CompoundTag jt2 = jz3.getCompound(integer4);
                final int integer5 = jt2.getByte("Slot") & 0xFF;
                if (integer5 >= 2 && integer5 < this.inventory.getContainerSize()) {
                    this.inventory.setItem(integer5, ItemStack.of(jt2));
                }
            }
        }
        this.updateEquipment();
    }
    
    @Override
    public boolean setSlot(final int integer, final ItemStack ben) {
        if (integer == 499) {
            if (this.hasChest() && ben.isEmpty()) {
                this.setChest(false);
                this.createInventory();
                return true;
            }
            if (!this.hasChest() && ben.getItem() == Blocks.CHEST.asItem()) {
                this.setChest(true);
                this.createInventory();
                return true;
            }
        }
        return super.setSlot(integer, ben);
    }
    
    @Override
    public boolean mobInteract(final Player ayj, final InteractionHand ajk) {
        final ItemStack ben4 = ayj.getItemInHand(ajk);
        if (ben4.getItem() instanceof SpawnEggItem) {
            return super.mobInteract(ayj, ajk);
        }
        if (!this.isBaby()) {
            if (this.isTamed() && ayj.isSecondaryUseActive()) {
                this.openInventory(ayj);
                return true;
            }
            if (this.isVehicle()) {
                return super.mobInteract(ayj, ajk);
            }
        }
        if (!ben4.isEmpty()) {
            boolean boolean5 = this.handleEating(ayj, ben4);
            if (!boolean5) {
                if (!this.isTamed() || ben4.getItem() == Items.NAME_TAG) {
                    if (ben4.interactEnemy(ayj, this, ajk)) {
                        return true;
                    }
                    this.makeMad();
                    return true;
                }
                else {
                    if (!this.hasChest() && ben4.getItem() == Blocks.CHEST.asItem()) {
                        this.setChest(true);
                        this.playChestEquipsSound();
                        boolean5 = true;
                        this.createInventory();
                    }
                    if (!this.isBaby() && !this.isSaddled() && ben4.getItem() == Items.SADDLE) {
                        this.openInventory(ayj);
                        return true;
                    }
                }
            }
            if (boolean5) {
                if (!ayj.abilities.instabuild) {
                    ben4.shrink(1);
                }
                return true;
            }
        }
        if (this.isBaby()) {
            return super.mobInteract(ayj, ajk);
        }
        this.doPlayerRide(ayj);
        return true;
    }
    
    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);
    }
}
