package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.StateHolder;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.stats.Stats;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.item.Item;
import net.minecraft.tags.Tag;
import net.minecraft.tags.ItemTags;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.item.ItemEntity;
import java.util.Random;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundSource;
import net.minecraft.sounds.SoundEvents;
import javax.annotation.Nullable;
import net.minecraft.world.level.block.entity.LecternBlockEntity;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.entity.player.Player;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.DirectionProperty;

public class LecternBlock extends BaseEntityBlock {
    public static final DirectionProperty FACING;
    public static final BooleanProperty POWERED;
    public static final BooleanProperty HAS_BOOK;
    public static final VoxelShape SHAPE_BASE;
    public static final VoxelShape SHAPE_POST;
    public static final VoxelShape SHAPE_COMMON;
    public static final VoxelShape SHAPE_TOP_PLATE;
    public static final VoxelShape SHAPE_COLLISION;
    public static final VoxelShape SHAPE_WEST;
    public static final VoxelShape SHAPE_NORTH;
    public static final VoxelShape SHAPE_EAST;
    public static final VoxelShape SHAPE_SOUTH;
    
    protected LecternBlock(final Properties c) {
        super(c);
        this.registerDefaultState(((((StateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)LecternBlock.FACING, Direction.NORTH)).setValue((Property<Comparable>)LecternBlock.POWERED, false)).<Comparable, Boolean>setValue((Property<Comparable>)LecternBlock.HAS_BOOK, false));
    }
    
    @Override
    public RenderShape getRenderShape(final BlockState cfj) {
        return RenderShape.MODEL;
    }
    
    @Override
    public VoxelShape getOcclusionShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu) {
        return LecternBlock.SHAPE_COMMON;
    }
    
    @Override
    public boolean useShapeForLightOcclusion(final BlockState cfj) {
        return true;
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bin) {
        final Level bqb3 = bin.getLevel();
        final ItemStack bki4 = bin.getItemInHand();
        final CompoundTag le5 = bki4.getTag();
        final Player bec6 = bin.getPlayer();
        boolean boolean7 = false;
        if (!bqb3.isClientSide && bec6 != null && le5 != null && bec6.canUseGameMasterBlocks() && le5.contains("BlockEntityTag")) {
            final CompoundTag le6 = le5.getCompound("BlockEntityTag");
            if (le6.contains("Book")) {
                boolean7 = true;
            }
        }
        return (((StateHolder<O, BlockState>)this.defaultBlockState()).setValue((Property<Comparable>)LecternBlock.FACING, bin.getHorizontalDirection().getOpposite())).<Comparable, Boolean>setValue((Property<Comparable>)LecternBlock.HAS_BOOK, boolean7);
    }
    
    @Override
    public VoxelShape getCollisionShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        return LecternBlock.SHAPE_COLLISION;
    }
    
    @Override
    public VoxelShape getShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        switch (cfj.<Direction>getValue((Property<Direction>)LecternBlock.FACING)) {
            case NORTH: {
                return LecternBlock.SHAPE_NORTH;
            }
            case SOUTH: {
                return LecternBlock.SHAPE_SOUTH;
            }
            case EAST: {
                return LecternBlock.SHAPE_EAST;
            }
            case WEST: {
                return LecternBlock.SHAPE_WEST;
            }
            default: {
                return LecternBlock.SHAPE_COMMON;
            }
        }
    }
    
    @Override
    public BlockState rotate(final BlockState cfj, final Rotation cap) {
        return ((StateHolder<O, BlockState>)cfj).<Comparable, Direction>setValue((Property<Comparable>)LecternBlock.FACING, cap.rotate(cfj.<Direction>getValue((Property<Direction>)LecternBlock.FACING)));
    }
    
    @Override
    public BlockState mirror(final BlockState cfj, final Mirror bzj) {
        return cfj.rotate(bzj.getRotation(cfj.<Direction>getValue((Property<Direction>)LecternBlock.FACING)));
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(LecternBlock.FACING, LecternBlock.POWERED, LecternBlock.HAS_BOOK);
    }
    
    @Nullable
    @Override
    public BlockEntity newBlockEntity(final BlockGetter bpg) {
        return new LecternBlockEntity();
    }
    
    public static boolean tryPlaceBook(final Level bqb, final BlockPos fu, final BlockState cfj, final ItemStack bki) {
        if (!cfj.<Boolean>getValue((Property<Boolean>)LecternBlock.HAS_BOOK)) {
            if (!bqb.isClientSide) {
                placeBook(bqb, fu, cfj, bki);
            }
            return true;
        }
        return false;
    }
    
    private static void placeBook(final Level bqb, final BlockPos fu, final BlockState cfj, final ItemStack bki) {
        final BlockEntity cdl5 = bqb.getBlockEntity(fu);
        if (cdl5 instanceof LecternBlockEntity) {
            final LecternBlockEntity ced6 = (LecternBlockEntity)cdl5;
            ced6.setBook(bki.split(1));
            resetBookState(bqb, fu, cfj, true);
            bqb.playSound(null, fu, SoundEvents.BOOK_PUT, SoundSource.BLOCKS, 1.0f, 1.0f);
        }
    }
    
    public static void resetBookState(final Level bqb, final BlockPos fu, final BlockState cfj, final boolean boolean4) {
        bqb.setBlock(fu, (((StateHolder<O, BlockState>)cfj).setValue((Property<Comparable>)LecternBlock.POWERED, false)).<Comparable, Boolean>setValue((Property<Comparable>)LecternBlock.HAS_BOOK, boolean4), 3);
        updateBelow(bqb, fu, cfj);
    }
    
    public static void signalPageChange(final Level bqb, final BlockPos fu, final BlockState cfj) {
        changePowered(bqb, fu, cfj, true);
        bqb.getBlockTicks().scheduleTick(fu, cfj.getBlock(), 2);
        bqb.levelEvent(1043, fu, 0);
    }
    
    private static void changePowered(final Level bqb, final BlockPos fu, final BlockState cfj, final boolean boolean4) {
        bqb.setBlock(fu, ((StateHolder<O, BlockState>)cfj).<Comparable, Boolean>setValue((Property<Comparable>)LecternBlock.POWERED, boolean4), 3);
        updateBelow(bqb, fu, cfj);
    }
    
    private static void updateBelow(final Level bqb, final BlockPos fu, final BlockState cfj) {
        bqb.updateNeighborsAt(fu.below(), cfj.getBlock());
    }
    
    @Override
    public void tick(final BlockState cfj, final ServerLevel zd, final BlockPos fu, final Random random) {
        changePowered(zd, fu, cfj, false);
    }
    
    @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;
        }
        if (cfj1.<Boolean>getValue((Property<Boolean>)LecternBlock.HAS_BOOK)) {
            this.popBook(cfj1, bqb, fu);
        }
        if (cfj1.<Boolean>getValue((Property<Boolean>)LecternBlock.POWERED)) {
            bqb.updateNeighborsAt(fu.below(), this);
        }
        super.onRemove(cfj1, bqb, fu, cfj4, boolean5);
    }
    
    private void popBook(final BlockState cfj, final Level bqb, final BlockPos fu) {
        final BlockEntity cdl5 = bqb.getBlockEntity(fu);
        if (cdl5 instanceof LecternBlockEntity) {
            final LecternBlockEntity ced6 = (LecternBlockEntity)cdl5;
            final Direction fz7 = cfj.<Direction>getValue((Property<Direction>)LecternBlock.FACING);
            final ItemStack bki8 = ced6.getBook().copy();
            final float float9 = 0.25f * fz7.getStepX();
            final float float10 = 0.25f * fz7.getStepZ();
            final ItemEntity bbg11 = new ItemEntity(bqb, fu.getX() + 0.5 + float9, fu.getY() + 1, fu.getZ() + 0.5 + float10, bki8);
            bbg11.setDefaultPickUpDelay();
            bqb.addFreshEntity(bbg11);
            ced6.clearContent();
        }
    }
    
    @Override
    public boolean isSignalSource(final BlockState cfj) {
        return true;
    }
    
    @Override
    public int getSignal(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final Direction fz) {
        return cfj.<Boolean>getValue((Property<Boolean>)LecternBlock.POWERED) ? 15 : 0;
    }
    
    @Override
    public int getDirectSignal(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final Direction fz) {
        return (fz == Direction.UP && cfj.<Boolean>getValue((Property<Boolean>)LecternBlock.POWERED)) ? 15 : 0;
    }
    
    @Override
    public boolean hasAnalogOutputSignal(final BlockState cfj) {
        return true;
    }
    
    @Override
    public int getAnalogOutputSignal(final BlockState cfj, final Level bqb, final BlockPos fu) {
        if (cfj.<Boolean>getValue((Property<Boolean>)LecternBlock.HAS_BOOK)) {
            final BlockEntity cdl5 = bqb.getBlockEntity(fu);
            if (cdl5 instanceof LecternBlockEntity) {
                return ((LecternBlockEntity)cdl5).getRedstoneSignal();
            }
        }
        return 0;
    }
    
    @Override
    public InteractionResult use(final BlockState cfj, final Level bqb, final BlockPos fu, final Player bec, final InteractionHand anf, final BlockHitResult deh) {
        if (cfj.<Boolean>getValue((Property<Boolean>)LecternBlock.HAS_BOOK)) {
            if (!bqb.isClientSide) {
                this.openScreen(bqb, fu, bec);
            }
            return InteractionResult.sidedSuccess(bqb.isClientSide);
        }
        final ItemStack bki8 = bec.getItemInHand(anf);
        if (bki8.isEmpty() || bki8.getItem().is(ItemTags.LECTERN_BOOKS)) {
            return InteractionResult.PASS;
        }
        return InteractionResult.CONSUME;
    }
    
    @Nullable
    @Override
    public MenuProvider getMenuProvider(final BlockState cfj, final Level bqb, final BlockPos fu) {
        if (!cfj.<Boolean>getValue((Property<Boolean>)LecternBlock.HAS_BOOK)) {
            return null;
        }
        return super.getMenuProvider(cfj, bqb, fu);
    }
    
    private void openScreen(final Level bqb, final BlockPos fu, final Player bec) {
        final BlockEntity cdl5 = bqb.getBlockEntity(fu);
        if (cdl5 instanceof LecternBlockEntity) {
            bec.openMenu((MenuProvider)cdl5);
            bec.awardStat(Stats.INTERACT_WITH_LECTERN);
        }
    }
    
    @Override
    public boolean isPathfindable(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final PathComputationType czg) {
        return false;
    }
    
    static {
        FACING = HorizontalDirectionalBlock.FACING;
        POWERED = BlockStateProperties.POWERED;
        HAS_BOOK = BlockStateProperties.HAS_BOOK;
        SHAPE_BASE = Block.box(0.0, 0.0, 0.0, 16.0, 2.0, 16.0);
        SHAPE_POST = Block.box(4.0, 2.0, 4.0, 12.0, 14.0, 12.0);
        SHAPE_COMMON = Shapes.or(LecternBlock.SHAPE_BASE, LecternBlock.SHAPE_POST);
        SHAPE_TOP_PLATE = Block.box(0.0, 15.0, 0.0, 16.0, 15.0, 16.0);
        SHAPE_COLLISION = Shapes.or(LecternBlock.SHAPE_COMMON, LecternBlock.SHAPE_TOP_PLATE);
        SHAPE_WEST = Shapes.or(Block.box(1.0, 10.0, 0.0, 5.333333, 14.0, 16.0), Block.box(5.333333, 12.0, 0.0, 9.666667, 16.0, 16.0), Block.box(9.666667, 14.0, 0.0, 14.0, 18.0, 16.0), LecternBlock.SHAPE_COMMON);
        SHAPE_NORTH = Shapes.or(Block.box(0.0, 10.0, 1.0, 16.0, 14.0, 5.333333), Block.box(0.0, 12.0, 5.333333, 16.0, 16.0, 9.666667), Block.box(0.0, 14.0, 9.666667, 16.0, 18.0, 14.0), LecternBlock.SHAPE_COMMON);
        SHAPE_EAST = Shapes.or(Block.box(15.0, 10.0, 0.0, 10.666667, 14.0, 16.0), Block.box(10.666667, 12.0, 0.0, 6.333333, 16.0, 16.0), Block.box(6.333333, 14.0, 0.0, 2.0, 18.0, 16.0), LecternBlock.SHAPE_COMMON);
        SHAPE_SOUTH = Shapes.or(Block.box(0.0, 10.0, 15.0, 16.0, 14.0, 10.666667), Block.box(0.0, 12.0, 10.666667, 16.0, 16.0, 6.333333), Block.box(0.0, 14.0, 6.333333, 16.0, 18.0, 2.0), LecternBlock.SHAPE_COMMON);
    }
}
