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.item.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.Level;
import java.util.Random;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.block.state.StateDefinition;
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.properties.BooleanProperty;

public class ObserverBlock extends DirectionalBlock {
    public static final BooleanProperty POWERED;
    
    public ObserverBlock(final Properties c) {
        super(c);
        this.registerDefaultState((((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)ObserverBlock.FACING, Direction.SOUTH)).<Comparable, Boolean>setValue((Property<Comparable>)ObserverBlock.POWERED, false));
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(ObserverBlock.FACING, ObserverBlock.POWERED);
    }
    
    @Override
    public BlockState rotate(final BlockState byj, final Rotation btu) {
        return ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Direction>setValue((Property<Comparable>)ObserverBlock.FACING, btu.rotate(byj.<Direction>getValue((Property<Direction>)ObserverBlock.FACING)));
    }
    
    @Override
    public BlockState mirror(final BlockState byj, final Mirror bsu) {
        return byj.rotate(bsu.getRotation(byj.<Direction>getValue((Property<Direction>)ObserverBlock.FACING)));
    }
    
    @Override
    public void tick(final BlockState byj, final ServerLevel xd, final BlockPos fk, final Random random) {
        if (byj.<Boolean>getValue((Property<Boolean>)ObserverBlock.POWERED)) {
            xd.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Boolean>setValue((Property<Comparable>)ObserverBlock.POWERED, false), 2);
        }
        else {
            xd.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Boolean>setValue((Property<Comparable>)ObserverBlock.POWERED, true), 2);
            xd.getBlockTicks().scheduleTick(fk, this, 2);
        }
        this.updateNeighborsInFront(xd, fk, byj);
    }
    
    @Override
    public BlockState updateShape(final BlockState byj1, final Direction fp, final BlockState byj3, final LevelAccessor bjx, final BlockPos fk5, final BlockPos fk6) {
        if (byj1.<Comparable>getValue((Property<Comparable>)ObserverBlock.FACING) == fp && !byj1.<Boolean>getValue((Property<Boolean>)ObserverBlock.POWERED)) {
            this.startSignal(bjx, fk5);
        }
        return super.updateShape(byj1, fp, byj3, bjx, fk5, fk6);
    }
    
    private void startSignal(final LevelAccessor bjx, final BlockPos fk) {
        if (!bjx.isClientSide() && !bjx.getBlockTicks().hasScheduledTick(fk, this)) {
            bjx.getBlockTicks().scheduleTick(fk, this, 2);
        }
    }
    
    protected void updateNeighborsInFront(final Level bjw, final BlockPos fk, final BlockState byj) {
        final Direction fp5 = byj.<Direction>getValue((Property<Direction>)ObserverBlock.FACING);
        final BlockPos fk2 = fk.relative(fp5.getOpposite());
        bjw.neighborChanged(fk2, this, fk);
        bjw.updateNeighborsAtExceptFromFacing(fk2, this, fp5);
    }
    
    @Override
    public boolean isSignalSource(final BlockState byj) {
        return true;
    }
    
    @Override
    public int getDirectSignal(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final Direction fp) {
        return byj.getSignal(bjg, fk, fp);
    }
    
    @Override
    public int getSignal(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final Direction fp) {
        if (byj.<Boolean>getValue((Property<Boolean>)ObserverBlock.POWERED) && byj.<Comparable>getValue((Property<Comparable>)ObserverBlock.FACING) == fp) {
            return 15;
        }
        return 0;
    }
    
    @Override
    public void onPlace(final BlockState byj1, final Level bjw, final BlockPos fk, final BlockState byj4, final boolean boolean5) {
        if (byj1.getBlock() == byj4.getBlock()) {
            return;
        }
        if (!bjw.isClientSide() && byj1.<Boolean>getValue((Property<Boolean>)ObserverBlock.POWERED) && !bjw.getBlockTicks().hasScheduledTick(fk, this)) {
            final BlockState byj5 = ((AbstractStateHolder<O, BlockState>)byj1).<Comparable, Boolean>setValue((Property<Comparable>)ObserverBlock.POWERED, false);
            bjw.setBlock(fk, byj5, 18);
            this.updateNeighborsInFront(bjw, fk, byj5);
        }
    }
    
    @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 (!bjw.isClientSide && byj1.<Boolean>getValue((Property<Boolean>)ObserverBlock.POWERED) && bjw.getBlockTicks().hasScheduledTick(fk, this)) {
            this.updateNeighborsInFront(bjw, fk, ((AbstractStateHolder<O, BlockState>)byj1).<Comparable, Boolean>setValue((Property<Comparable>)ObserverBlock.POWERED, false));
        }
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bcq) {
        return ((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).<Comparable, Direction>setValue((Property<Comparable>)ObserverBlock.FACING, bcq.getNearestLookingDirection().getOpposite().getOpposite());
    }
    
    static {
        POWERED = BlockStateProperties.POWERED;
    }
}
