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.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.BlockBehaviour;
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((((StateHolder<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 cfj, final Rotation cap) {
        return ((StateHolder<O, BlockState>)cfj).<Comparable, Direction>setValue((Property<Comparable>)ObserverBlock.FACING, cap.rotate(cfj.<Direction>getValue((Property<Direction>)ObserverBlock.FACING)));
    }
    
    @Override
    public BlockState mirror(final BlockState cfj, final Mirror bzj) {
        return cfj.rotate(bzj.getRotation(cfj.<Direction>getValue((Property<Direction>)ObserverBlock.FACING)));
    }
    
    @Override
    public void tick(final BlockState cfj, final ServerLevel zd, final BlockPos fu, final Random random) {
        if (cfj.<Boolean>getValue((Property<Boolean>)ObserverBlock.POWERED)) {
            zd.setBlock(fu, ((StateHolder<O, BlockState>)cfj).<Comparable, Boolean>setValue((Property<Comparable>)ObserverBlock.POWERED, false), 2);
        }
        else {
            zd.setBlock(fu, ((StateHolder<O, BlockState>)cfj).<Comparable, Boolean>setValue((Property<Comparable>)ObserverBlock.POWERED, true), 2);
            zd.getBlockTicks().scheduleTick(fu, this, 2);
        }
        this.updateNeighborsInFront(zd, fu, cfj);
    }
    
    @Override
    public BlockState updateShape(final BlockState cfj1, final Direction fz, final BlockState cfj3, final LevelAccessor bqc, final BlockPos fu5, final BlockPos fu6) {
        if (cfj1.<Comparable>getValue((Property<Comparable>)ObserverBlock.FACING) == fz && !cfj1.<Boolean>getValue((Property<Boolean>)ObserverBlock.POWERED)) {
            this.startSignal(bqc, fu5);
        }
        return super.updateShape(cfj1, fz, cfj3, bqc, fu5, fu6);
    }
    
    private void startSignal(final LevelAccessor bqc, final BlockPos fu) {
        if (!bqc.isClientSide() && !bqc.getBlockTicks().hasScheduledTick(fu, this)) {
            bqc.getBlockTicks().scheduleTick(fu, this, 2);
        }
    }
    
    protected void updateNeighborsInFront(final Level bqb, final BlockPos fu, final BlockState cfj) {
        final Direction fz5 = cfj.<Direction>getValue((Property<Direction>)ObserverBlock.FACING);
        final BlockPos fu2 = fu.relative(fz5.getOpposite());
        bqb.neighborChanged(fu2, this, fu);
        bqb.updateNeighborsAtExceptFromFacing(fu2, this, fz5);
    }
    
    @Override
    public boolean isSignalSource(final BlockState cfj) {
        return true;
    }
    
    @Override
    public int getDirectSignal(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final Direction fz) {
        return cfj.getSignal(bpg, fu, fz);
    }
    
    @Override
    public int getSignal(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final Direction fz) {
        if (cfj.<Boolean>getValue((Property<Boolean>)ObserverBlock.POWERED) && cfj.<Comparable>getValue((Property<Comparable>)ObserverBlock.FACING) == fz) {
            return 15;
        }
        return 0;
    }
    
    @Override
    public void onPlace(final BlockState cfj1, final Level bqb, final BlockPos fu, final BlockState cfj4, final boolean boolean5) {
        if (cfj1.is(cfj4.getBlock())) {
            return;
        }
        if (!bqb.isClientSide() && cfj1.<Boolean>getValue((Property<Boolean>)ObserverBlock.POWERED) && !bqb.getBlockTicks().hasScheduledTick(fu, this)) {
            final BlockState cfj5 = ((StateHolder<O, BlockState>)cfj1).<Comparable, Boolean>setValue((Property<Comparable>)ObserverBlock.POWERED, false);
            bqb.setBlock(fu, cfj5, 18);
            this.updateNeighborsInFront(bqb, fu, cfj5);
        }
    }
    
    @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 (!bqb.isClientSide && cfj1.<Boolean>getValue((Property<Boolean>)ObserverBlock.POWERED) && bqb.getBlockTicks().hasScheduledTick(fu, this)) {
            this.updateNeighborsInFront(bqb, fu, ((StateHolder<O, BlockState>)cfj1).<Comparable, Boolean>setValue((Property<Comparable>)ObserverBlock.POWERED, false));
        }
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bin) {
        return ((StateHolder<O, BlockState>)this.defaultBlockState()).<Comparable, Direction>setValue((Property<Comparable>)ObserverBlock.FACING, bin.getNearestLookingDirection().getOpposite().getOpposite());
    }
    
    static {
        POWERED = BlockStateProperties.POWERED;
    }
}
