package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.AbstractStateHolder;
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.item.ItemStack;
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.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.properties.BooleanProperty;

public class JukeboxBlock extends BaseEntityBlock {
    public static final BooleanProperty HAS_RECORD;
    
    protected JukeboxBlock(final Properties c) {
        super(c);
        this.registerDefaultState(((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Boolean>setValue((Property<Comparable>)JukeboxBlock.HAS_RECORD, false));
    }
    
    @Override
    public InteractionResult use(BlockState byj, final Level bjw, final BlockPos fk, final Player ayj, final InteractionHand ajk, final BlockHitResult cvg) {
        if (byj.<Boolean>getValue((Property<Boolean>)JukeboxBlock.HAS_RECORD)) {
            this.dropRecording(bjw, fk);
            byj = ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Boolean>setValue((Property<Comparable>)JukeboxBlock.HAS_RECORD, false);
            bjw.setBlock(fk, byj, 2);
            return InteractionResult.SUCCESS;
        }
        return InteractionResult.PASS;
    }
    
    public void setRecord(final LevelAccessor bjx, final BlockPos fk, final BlockState byj, final ItemStack ben) {
        final BlockEntity bwl6 = bjx.getBlockEntity(fk);
        if (!(bwl6 instanceof JukeboxBlockEntity)) {
            return;
        }
        ((JukeboxBlockEntity)bwl6).setRecord(ben.copy());
        bjx.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Boolean>setValue((Property<Comparable>)JukeboxBlock.HAS_RECORD, true), 2);
    }
    
    private void dropRecording(final Level bjw, final BlockPos fk) {
        if (bjw.isClientSide) {
            return;
        }
        final BlockEntity bwl4 = bjw.getBlockEntity(fk);
        if (!(bwl4 instanceof JukeboxBlockEntity)) {
            return;
        }
        final JukeboxBlockEntity bxc5 = (JukeboxBlockEntity)bwl4;
        final ItemStack ben6 = bxc5.getRecord();
        if (ben6.isEmpty()) {
            return;
        }
        bjw.levelEvent(1010, fk, 0);
        bxc5.clearContent();
        final float float7 = 0.7f;
        final double double8 = bjw.random.nextFloat() * 0.7f + 0.15000000596046448;
        final double double9 = bjw.random.nextFloat() * 0.7f + 0.06000000238418579 + 0.6;
        final double double10 = bjw.random.nextFloat() * 0.7f + 0.15000000596046448;
        final ItemStack ben7 = ben6.copy();
        final ItemEntity awb15 = new ItemEntity(bjw, fk.getX() + double8, fk.getY() + double9, fk.getZ() + double10, ben7);
        awb15.setDefaultPickUpDelay();
        bjw.addFreshEntity(awb15);
    }
    
    @Override
    public void onRemove(final BlockState byj1, final Level bjw, final BlockPos fk, final BlockState byj4, final boolean boolean5) {
        if (byj1.getBlock() == byj4.getBlock()) {
            return;
        }
        this.dropRecording(bjw, fk);
        super.onRemove(byj1, bjw, fk, byj4, boolean5);
    }
    
    @Override
    public BlockEntity newBlockEntity(final BlockGetter bjg) {
        return new JukeboxBlockEntity();
    }
    
    @Override
    public boolean hasAnalogOutputSignal(final BlockState byj) {
        return true;
    }
    
    @Override
    public int getAnalogOutputSignal(final BlockState byj, final Level bjw, final BlockPos fk) {
        final BlockEntity bwl5 = bjw.getBlockEntity(fk);
        if (bwl5 instanceof JukeboxBlockEntity) {
            final Item bei6 = ((JukeboxBlockEntity)bwl5).getRecord().getItem();
            if (bei6 instanceof RecordItem) {
                return ((RecordItem)bei6).getAnalogOutput();
            }
        }
        return 0;
    }
    
    @Override
    public RenderShape getRenderShape(final BlockState byj) {
        return RenderShape.MODEL;
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(JukeboxBlock.HAS_RECORD);
    }
    
    static {
        HAS_RECORD = BlockStateProperties.HAS_RECORD;
    }
}
