package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.AbstractStateHolder;
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.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.world.entity.player.Player;
import net.minecraft.sounds.SoundSource;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
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.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.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(((((AbstractStateHolder<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 byj) {
        return RenderShape.MODEL;
    }
    
    @Override
    public VoxelShape getOcclusionShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk) {
        return LecternBlock.SHAPE_COMMON;
    }
    
    @Override
    public boolean useShapeForLightOcclusion(final BlockState byj) {
        return true;
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bcq) {
        return ((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).<Comparable, Direction>setValue((Property<Comparable>)LecternBlock.FACING, bcq.getHorizontalDirection().getOpposite());
    }
    
    @Override
    public VoxelShape getCollisionShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        return LecternBlock.SHAPE_COLLISION;
    }
    
    @Override
    public VoxelShape getShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        switch (byj.<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 byj, final Rotation btu) {
        return ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Direction>setValue((Property<Comparable>)LecternBlock.FACING, btu.rotate(byj.<Direction>getValue((Property<Direction>)LecternBlock.FACING)));
    }
    
    @Override
    public BlockState mirror(final BlockState byj, final Mirror bsu) {
        return byj.rotate(bsu.getRotation(byj.<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 bjg) {
        return new LecternBlockEntity();
    }
    
    public static boolean tryPlaceBook(final Level bjw, final BlockPos fk, final BlockState byj, final ItemStack ben) {
        if (!byj.<Boolean>getValue((Property<Boolean>)LecternBlock.HAS_BOOK)) {
            if (!bjw.isClientSide) {
                placeBook(bjw, fk, byj, ben);
            }
            return true;
        }
        return false;
    }
    
    private static void placeBook(final Level bjw, final BlockPos fk, final BlockState byj, final ItemStack ben) {
        final BlockEntity bwl5 = bjw.getBlockEntity(fk);
        if (bwl5 instanceof LecternBlockEntity) {
            final LecternBlockEntity bxd6 = (LecternBlockEntity)bwl5;
            bxd6.setBook(ben.split(1));
            resetBookState(bjw, fk, byj, true);
            bjw.playSound(null, fk, SoundEvents.BOOK_PUT, SoundSource.BLOCKS, 1.0f, 1.0f);
        }
    }
    
    public static void resetBookState(final Level bjw, final BlockPos fk, final BlockState byj, final boolean boolean4) {
        bjw.setBlock(fk, (((AbstractStateHolder<O, BlockState>)byj).setValue((Property<Comparable>)LecternBlock.POWERED, false)).<Comparable, Boolean>setValue((Property<Comparable>)LecternBlock.HAS_BOOK, boolean4), 3);
        updateBelow(bjw, fk, byj);
    }
    
    public static void signalPageChange(final Level bjw, final BlockPos fk, final BlockState byj) {
        changePowered(bjw, fk, byj, true);
        bjw.getBlockTicks().scheduleTick(fk, byj.getBlock(), 2);
        bjw.levelEvent(1043, fk, 0);
    }
    
    private static void changePowered(final Level bjw, final BlockPos fk, final BlockState byj, final boolean boolean4) {
        bjw.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Boolean>setValue((Property<Comparable>)LecternBlock.POWERED, boolean4), 3);
        updateBelow(bjw, fk, byj);
    }
    
    private static void updateBelow(final Level bjw, final BlockPos fk, final BlockState byj) {
        bjw.updateNeighborsAt(fk.below(), byj.getBlock());
    }
    
    @Override
    public void tick(final BlockState byj, final ServerLevel xd, final BlockPos fk, final Random random) {
        changePowered(xd, fk, byj, false);
    }
    
    @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;
        }
        if (byj1.<Boolean>getValue((Property<Boolean>)LecternBlock.HAS_BOOK)) {
            this.popBook(byj1, bjw, fk);
        }
        if (byj1.<Boolean>getValue((Property<Boolean>)LecternBlock.POWERED)) {
            bjw.updateNeighborsAt(fk.below(), this);
        }
        super.onRemove(byj1, bjw, fk, byj4, boolean5);
    }
    
    private void popBook(final BlockState byj, final Level bjw, final BlockPos fk) {
        final BlockEntity bwl5 = bjw.getBlockEntity(fk);
        if (bwl5 instanceof LecternBlockEntity) {
            final LecternBlockEntity bxd6 = (LecternBlockEntity)bwl5;
            final Direction fp7 = byj.<Direction>getValue((Property<Direction>)LecternBlock.FACING);
            final ItemStack ben8 = bxd6.getBook().copy();
            final float float9 = 0.25f * fp7.getStepX();
            final float float10 = 0.25f * fp7.getStepZ();
            final ItemEntity awb11 = new ItemEntity(bjw, fk.getX() + 0.5 + float9, fk.getY() + 1, fk.getZ() + 0.5 + float10, ben8);
            awb11.setDefaultPickUpDelay();
            bjw.addFreshEntity(awb11);
            bxd6.clearContent();
        }
    }
    
    @Override
    public boolean isSignalSource(final BlockState byj) {
        return true;
    }
    
    @Override
    public int getSignal(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final Direction fp) {
        return byj.<Boolean>getValue((Property<Boolean>)LecternBlock.POWERED) ? 15 : 0;
    }
    
    @Override
    public int getDirectSignal(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final Direction fp) {
        return (fp == Direction.UP && byj.<Boolean>getValue((Property<Boolean>)LecternBlock.POWERED)) ? 15 : 0;
    }
    
    @Override
    public boolean hasAnalogOutputSignal(final BlockState byj) {
        return true;
    }
    
    @Override
    public int getAnalogOutputSignal(final BlockState byj, final Level bjw, final BlockPos fk) {
        if (byj.<Boolean>getValue((Property<Boolean>)LecternBlock.HAS_BOOK)) {
            final BlockEntity bwl5 = bjw.getBlockEntity(fk);
            if (bwl5 instanceof LecternBlockEntity) {
                return ((LecternBlockEntity)bwl5).getRedstoneSignal();
            }
        }
        return 0;
    }
    
    @Override
    public InteractionResult use(final BlockState byj, final Level bjw, final BlockPos fk, final Player ayj, final InteractionHand ajk, final BlockHitResult cvg) {
        if (byj.<Boolean>getValue((Property<Boolean>)LecternBlock.HAS_BOOK)) {
            if (!bjw.isClientSide) {
                this.openScreen(bjw, fk, ayj);
            }
            return InteractionResult.SUCCESS;
        }
        final ItemStack ben8 = ayj.getItemInHand(ajk);
        if (ben8.isEmpty() || ben8.getItem().is(ItemTags.LECTERN_BOOKS)) {
            return InteractionResult.PASS;
        }
        return InteractionResult.CONSUME;
    }
    
    @Nullable
    @Override
    public MenuProvider getMenuProvider(final BlockState byj, final Level bjw, final BlockPos fk) {
        if (!byj.<Boolean>getValue((Property<Boolean>)LecternBlock.HAS_BOOK)) {
            return null;
        }
        return super.getMenuProvider(byj, bjw, fk);
    }
    
    private void openScreen(final Level bjw, final BlockPos fk, final Player ayj) {
        final BlockEntity bwl5 = bjw.getBlockEntity(fk);
        if (bwl5 instanceof LecternBlockEntity) {
            ayj.openMenu((MenuProvider)bwl5);
            ayj.awardStat(Stats.INTERACT_WITH_LECTERN);
        }
    }
    
    @Override
    public boolean isPathfindable(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final PathComputationType cqr) {
        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);
    }
}
