package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.StateHolder;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.RecordItem;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.JukeboxBlockEntity;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.ItemStack;
import javax.annotation.Nullable;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.properties.BooleanProperty;

public class JukeboxBlock extends BaseEntityBlock {
    public static final BooleanProperty HAS_RECORD;
    
    protected JukeboxBlock(final Properties c) {
        super(c);
        this.registerDefaultState(((StateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Boolean>setValue((Property<Comparable>)JukeboxBlock.HAS_RECORD, false));
    }
    
    @Override
    public void setPlacedBy(final Level bqb, final BlockPos fu, final BlockState cfj, @Nullable final LivingEntity aoy, final ItemStack bki) {
        super.setPlacedBy(bqb, fu, cfj, aoy, bki);
        final CompoundTag le7 = bki.getOrCreateTag();
        if (le7.contains("BlockEntityTag")) {
            final CompoundTag le8 = le7.getCompound("BlockEntityTag");
            if (le8.contains("RecordItem")) {
                bqb.setBlock(fu, ((StateHolder<O, BlockState>)cfj).<Comparable, Boolean>setValue((Property<Comparable>)JukeboxBlock.HAS_RECORD, true), 2);
            }
        }
    }
    
    @Override
    public InteractionResult use(BlockState cfj, final Level bqb, final BlockPos fu, final Player bec, final InteractionHand anf, final BlockHitResult deh) {
        if (cfj.<Boolean>getValue((Property<Boolean>)JukeboxBlock.HAS_RECORD)) {
            this.dropRecording(bqb, fu);
            cfj = ((StateHolder<O, BlockState>)cfj).<Comparable, Boolean>setValue((Property<Comparable>)JukeboxBlock.HAS_RECORD, false);
            bqb.setBlock(fu, cfj, 2);
            return InteractionResult.sidedSuccess(bqb.isClientSide);
        }
        return InteractionResult.PASS;
    }
    
    public void setRecord(final LevelAccessor bqc, final BlockPos fu, final BlockState cfj, final ItemStack bki) {
        final BlockEntity cdl6 = bqc.getBlockEntity(fu);
        if (!(cdl6 instanceof JukeboxBlockEntity)) {
            return;
        }
        ((JukeboxBlockEntity)cdl6).setRecord(bki.copy());
        bqc.setBlock(fu, ((StateHolder<O, BlockState>)cfj).<Comparable, Boolean>setValue((Property<Comparable>)JukeboxBlock.HAS_RECORD, true), 2);
    }
    
    private void dropRecording(final Level bqb, final BlockPos fu) {
        if (bqb.isClientSide) {
            return;
        }
        final BlockEntity cdl4 = bqb.getBlockEntity(fu);
        if (!(cdl4 instanceof JukeboxBlockEntity)) {
            return;
        }
        final JukeboxBlockEntity cec5 = (JukeboxBlockEntity)cdl4;
        final ItemStack bki6 = cec5.getRecord();
        if (bki6.isEmpty()) {
            return;
        }
        bqb.levelEvent(1010, fu, 0);
        cec5.clearContent();
        final float float7 = 0.7f;
        final double double8 = bqb.random.nextFloat() * 0.7f + 0.15000000596046448;
        final double double9 = bqb.random.nextFloat() * 0.7f + 0.06000000238418579 + 0.6;
        final double double10 = bqb.random.nextFloat() * 0.7f + 0.15000000596046448;
        final ItemStack bki7 = bki6.copy();
        final ItemEntity bbg15 = new ItemEntity(bqb, fu.getX() + double8, fu.getY() + double9, fu.getZ() + double10, bki7);
        bbg15.setDefaultPickUpDelay();
        bqb.addFreshEntity(bbg15);
    }
    
    @Override
    public void onRemove(final BlockState cfj1, final Level bqb, final BlockPos fu, final BlockState cfj4, final boolean boolean5) {
        if (cfj1.is(cfj4.getBlock())) {
            return;
        }
        this.dropRecording(bqb, fu);
        super.onRemove(cfj1, bqb, fu, cfj4, boolean5);
    }
    
    @Override
    public BlockEntity newBlockEntity(final BlockGetter bpg) {
        return new JukeboxBlockEntity();
    }
    
    @Override
    public boolean hasAnalogOutputSignal(final BlockState cfj) {
        return true;
    }
    
    @Override
    public int getAnalogOutputSignal(final BlockState cfj, final Level bqb, final BlockPos fu) {
        final BlockEntity cdl5 = bqb.getBlockEntity(fu);
        if (cdl5 instanceof JukeboxBlockEntity) {
            final Item bke6 = ((JukeboxBlockEntity)cdl5).getRecord().getItem();
            if (bke6 instanceof RecordItem) {
                return ((RecordItem)bke6).getAnalogOutput();
            }
        }
        return 0;
    }
    
    @Override
    public RenderShape getRenderShape(final BlockState cfj) {
        return RenderShape.MODEL;
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(JukeboxBlock.HAS_RECORD);
    }
    
    static {
        HAS_RECORD = BlockStateProperties.HAS_RECORD;
    }
}
