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.level.block.state.StateDefinition;
import net.minecraft.world.level.block.entity.DaylightDetectorBlockEntity;
import net.minecraft.world.level.block.entity.BlockEntity;
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.util.Mth;
import net.minecraft.world.level.LightLayer;
import net.minecraft.world.level.Level;
import net.minecraft.core.Direction;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.state.properties.Property;
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.IntegerProperty;

public class DaylightDetectorBlock extends BaseEntityBlock {
    public static final IntegerProperty POWER;
    public static final BooleanProperty INVERTED;
    protected static final VoxelShape SHAPE;
    
    public DaylightDetectorBlock(final Properties c) {
        super(c);
        this.registerDefaultState((((StateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)DaylightDetectorBlock.POWER, 0)).<Comparable, Boolean>setValue((Property<Comparable>)DaylightDetectorBlock.INVERTED, false));
    }
    
    @Override
    public VoxelShape getShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        return DaylightDetectorBlock.SHAPE;
    }
    
    @Override
    public boolean useShapeForLightOcclusion(final BlockState cfj) {
        return true;
    }
    
    @Override
    public int getSignal(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final Direction fz) {
        return cfj.<Integer>getValue((Property<Integer>)DaylightDetectorBlock.POWER);
    }
    
    public static void updateSignalStrength(final BlockState cfj, final Level bqb, final BlockPos fu) {
        if (!bqb.dimensionType().hasSkyLight()) {
            return;
        }
        int integer4 = bqb.getBrightness(LightLayer.SKY, fu) - bqb.getSkyDarken();
        float float5 = bqb.getSunAngle(1.0f);
        final boolean boolean6 = cfj.<Boolean>getValue((Property<Boolean>)DaylightDetectorBlock.INVERTED);
        if (boolean6) {
            integer4 = 15 - integer4;
        }
        else if (integer4 > 0) {
            final float float6 = (float5 < 3.1415927f) ? 0.0f : 6.2831855f;
            float5 += (float6 - float5) * 0.2f;
            integer4 = Math.round(integer4 * Mth.cos(float5));
        }
        integer4 = Mth.clamp(integer4, 0, 15);
        if (cfj.<Integer>getValue((Property<Integer>)DaylightDetectorBlock.POWER) != integer4) {
            bqb.setBlock(fu, ((StateHolder<O, BlockState>)cfj).<Comparable, Integer>setValue((Property<Comparable>)DaylightDetectorBlock.POWER, integer4), 3);
        }
    }
    
    @Override
    public InteractionResult use(final BlockState cfj, final Level bqb, final BlockPos fu, final Player bec, final InteractionHand anf, final BlockHitResult deh) {
        if (!bec.mayBuild()) {
            return super.use(cfj, bqb, fu, bec, anf, deh);
        }
        if (bqb.isClientSide) {
            return InteractionResult.SUCCESS;
        }
        final BlockState cfj2 = ((StateHolder<O, BlockState>)cfj).<Comparable>cycle((Property<Comparable>)DaylightDetectorBlock.INVERTED);
        bqb.setBlock(fu, cfj2, 4);
        updateSignalStrength(cfj2, bqb, fu);
        return InteractionResult.CONSUME;
    }
    
    @Override
    public RenderShape getRenderShape(final BlockState cfj) {
        return RenderShape.MODEL;
    }
    
    @Override
    public boolean isSignalSource(final BlockState cfj) {
        return true;
    }
    
    @Override
    public BlockEntity newBlockEntity(final BlockGetter bpg) {
        return new DaylightDetectorBlockEntity();
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(DaylightDetectorBlock.POWER, DaylightDetectorBlock.INVERTED);
    }
    
    static {
        POWER = BlockStateProperties.POWER;
        INVERTED = BlockStateProperties.INVERTED;
        SHAPE = Block.box(0.0, 0.0, 0.0, 16.0, 6.0, 16.0);
    }
}
