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.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.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((((AbstractStateHolder<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 byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        return DaylightDetectorBlock.SHAPE;
    }
    
    @Override
    public boolean useShapeForLightOcclusion(final BlockState byj) {
        return true;
    }
    
    @Override
    public int getSignal(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final Direction fp) {
        return byj.<Integer>getValue((Property<Integer>)DaylightDetectorBlock.POWER);
    }
    
    public static void updateSignalStrength(final BlockState byj, final Level bjw, final BlockPos fk) {
        if (!bjw.dimension.isHasSkyLight()) {
            return;
        }
        int integer4 = bjw.getBrightness(LightLayer.SKY, fk) - bjw.getSkyDarken();
        float float5 = bjw.getSunAngle(1.0f);
        final boolean boolean6 = byj.<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 (byj.<Integer>getValue((Property<Integer>)DaylightDetectorBlock.POWER) != integer4) {
            bjw.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Integer>setValue((Property<Comparable>)DaylightDetectorBlock.POWER, integer4), 3);
        }
    }
    
    @Override
    public InteractionResult use(final BlockState byj, final Level bjw, final BlockPos fk, final Player ayj, final InteractionHand ajk, final BlockHitResult cvg) {
        if (!ayj.mayBuild()) {
            return super.use(byj, bjw, fk, ayj, ajk, cvg);
        }
        if (bjw.isClientSide) {
            return InteractionResult.SUCCESS;
        }
        final BlockState byj2 = ((AbstractStateHolder<O, BlockState>)byj).<Comparable>cycle((Property<Comparable>)DaylightDetectorBlock.INVERTED);
        bjw.setBlock(fk, byj2, 4);
        updateSignalStrength(byj2, bjw, fk);
        return InteractionResult.SUCCESS;
    }
    
    @Override
    public RenderShape getRenderShape(final BlockState byj) {
        return RenderShape.MODEL;
    }
    
    @Override
    public boolean isSignalSource(final BlockState byj) {
        return true;
    }
    
    @Override
    public BlockEntity newBlockEntity(final BlockGetter bjg) {
        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);
    }
}
