package net.minecraft.world.entity.decoration;

import net.minecraft.network.protocol.game.ClientboundAddPaintingPacket;
import net.minecraft.network.protocol.Packet;
import net.minecraft.world.level.ItemLike;
import net.minecraft.world.item.Items;
import net.minecraft.world.entity.player.Player;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.level.GameRules;
import javax.annotation.Nullable;
import net.minecraft.world.entity.Entity;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.nbt.CompoundTag;
import java.util.Iterator;
import java.util.List;
import net.minecraft.core.Registry;
import com.google.common.collect.Lists;
import net.minecraft.core.Direction;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.entity.EntityType;

public class Painting extends HangingEntity {
    public Motive motive;
    
    public Painting(final EntityType<? extends Painting> aku, final Level bjw) {
        super(aku, bjw);
    }
    
    public Painting(final Level bjw, final BlockPos fk, final Direction fp) {
        super(EntityType.PAINTING, bjw, fk);
        final List<Motive> list5 = Lists.newArrayList();
        int integer6 = 0;
        for (final Motive avt8 : Registry.MOTIVE) {
            this.motive = avt8;
            this.setDirection(fp);
            if (this.survives()) {
                list5.add(avt8);
                final int integer7 = avt8.getWidth() * avt8.getHeight();
                if (integer7 <= integer6) {
                    continue;
                }
                integer6 = integer7;
            }
        }
        if (!list5.isEmpty()) {
            final Iterator<Motive> iterator7 = list5.iterator();
            while (iterator7.hasNext()) {
                final Motive avt8 = iterator7.next();
                if (avt8.getWidth() * avt8.getHeight() < integer6) {
                    iterator7.remove();
                }
            }
            this.motive = list5.get(this.random.nextInt(list5.size()));
        }
        this.setDirection(fp);
    }
    
    public Painting(final Level bjw, final BlockPos fk, final Direction fp, final Motive avt) {
        this(bjw, fk, fp);
        this.motive = avt;
        this.setDirection(fp);
    }
    
    @Override
    public void addAdditionalSaveData(final CompoundTag jt) {
        jt.putString("Motive", Registry.MOTIVE.getKey(this.motive).toString());
        super.addAdditionalSaveData(jt);
    }
    
    @Override
    public void readAdditionalSaveData(final CompoundTag jt) {
        this.motive = Registry.MOTIVE.get(ResourceLocation.tryParse(jt.getString("Motive")));
        super.readAdditionalSaveData(jt);
    }
    
    @Override
    public int getWidth() {
        if (this.motive == null) {
            return 1;
        }
        return this.motive.getWidth();
    }
    
    @Override
    public int getHeight() {
        if (this.motive == null) {
            return 1;
        }
        return this.motive.getHeight();
    }
    
    @Override
    public void dropItem(@Nullable final Entity akq) {
        if (!this.level.getGameRules().getBoolean(GameRules.RULE_DOENTITYDROPS)) {
            return;
        }
        this.playSound(SoundEvents.PAINTING_BREAK, 1.0f, 1.0f);
        if (akq instanceof Player) {
            final Player ayj3 = (Player)akq;
            if (ayj3.abilities.instabuild) {
                return;
            }
        }
        this.spawnAtLocation(Items.PAINTING);
    }
    
    @Override
    public void playPlacementSound() {
        this.playSound(SoundEvents.PAINTING_PLACE, 1.0f, 1.0f);
    }
    
    @Override
    public void moveTo(final double double1, final double double2, final double double3, final float float4, final float float5) {
        this.setPos(double1, double2, double3);
    }
    
    @Override
    public void lerpTo(final double double1, final double double2, final double double3, final float float4, final float float5, final int integer, final boolean boolean7) {
        final BlockPos fk12 = this.pos.offset(double1 - this.getX(), double2 - this.getY(), double3 - this.getZ());
        this.setPos(fk12.getX(), fk12.getY(), fk12.getZ());
    }
    
    @Override
    public Packet<?> getAddEntityPacket() {
        return new ClientboundAddPaintingPacket(this);
    }
}
