package com.dairymoose.modernlife.blocks;

import com.dairymoose.modernlife.core.ModernLifeConfig;
import com.dairymoose.modernlife.util.ModernLifeUtil;
import javax.annotation.Nullable;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.util.Mth;
import net.minecraft.util.StringRepresentable;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.HorizontalDirectionalBlock;
import net.minecraft.world.level.block.Mirror;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.level.block.state.properties.EnumProperty;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.material.PushReaction;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/StreetLightBlock.class */
public class StreetLightBlock extends Block {
    private static final Logger LOGGER = LogManager.getLogger();
    protected static final VoxelShape SHAPE_NORTH = Block.box(4.0d, 0.0d, 4.0d, 12.0d, 16.0d, 12.0d);
    protected static final VoxelShape SHAPE_EAST = ModernLifeUtil.RotateVoxelShapeClockwise(SHAPE_NORTH);
    protected static final VoxelShape SHAPE_SOUTH = ModernLifeUtil.RotateVoxelShapeClockwise(SHAPE_EAST);
    protected static final VoxelShape SHAPE_WEST = ModernLifeUtil.RotateVoxelShapeClockwise(SHAPE_SOUTH);
    protected static final VoxelShape SHAPE_NORTH_TOP = Block.box(4.0d, 0.0d, 4.0d, 12.0d, 12.0d, 12.0d);
    protected static final VoxelShape SHAPE_EAST_TOP = ModernLifeUtil.RotateVoxelShapeClockwise(SHAPE_NORTH_TOP);
    protected static final VoxelShape SHAPE_SOUTH_TOP = ModernLifeUtil.RotateVoxelShapeClockwise(SHAPE_EAST_TOP);
    protected static final VoxelShape SHAPE_WEST_TOP = ModernLifeUtil.RotateVoxelShapeClockwise(SHAPE_SOUTH_TOP);
    public static int MID_PART_COUNT = 3;
    public static final DirectionProperty FACING = HorizontalDirectionalBlock.FACING;
    public static final EnumProperty<PartType> PART = EnumProperty.create("part", PartType.class);

    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/StreetLightBlock$PartType.class */
    public enum PartType implements StringRepresentable {
        bottom,
        mid,
        top;

        public String getSerializedName() {
            return name().toLowerCase();
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* renamed from: com.dairymoose.modernlife.blocks.StreetLightBlock$1 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/StreetLightBlock$1.class */
    public class C00591 implements StatePredicate {
        C00591() {
        }

        public boolean test(BlockState state, BlockGetter reader, BlockPos pos) {
            if (state.getValue(StreetLightBlock.PART) == PartType.top) {
                return true;
            }
            return false;
        }
    }

    public StreetLightBlock(Properties props) {
        super(props.lightLevel(StreetLightBlock::getLightLevel).emissiveRendering(new StatePredicate() { // from class: com.dairymoose.modernlife.blocks.StreetLightBlock.1
            C00591() {
            }

            public boolean test(BlockState state, BlockGetter reader, BlockPos pos) {
                if (state.getValue(StreetLightBlock.PART) == PartType.top) {
                    return true;
                }
                return false;
            }
        }));
        registerDefaultState((BlockState) ((BlockState) this.stateDefinition.any().setValue(FACING, Direction.NORTH)).setValue(PART, PartType.bottom));
    }

    public static int getLightLevel(BlockState state) {
        if (state.getValue(PART) == PartType.top) {
            return 15;
        }
        return 0;
    }

    public VoxelShape getShape(BlockState p_220053_1_, BlockGetter p_220053_2_, BlockPos p_220053_3_, CollisionContext p_220053_4_) {
        if (p_220053_1_.getValue(PART) == PartType.top) {
            return SHAPE_NORTH_TOP;
        }
        Direction lvt_5_1_ = p_220053_1_.getValue(FACING);
        switch (C00602.$SwitchMap$net$minecraft$core$Direction[lvt_5_1_.ordinal()]) {
            case 1:
            default:
                return SHAPE_SOUTH;
            case 2:
                return SHAPE_EAST;
            case 3:
                return SHAPE_WEST;
            case 4:
                return SHAPE_NORTH;
        }
    }

    /* renamed from: com.dairymoose.modernlife.blocks.StreetLightBlock$2 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/StreetLightBlock$2.class */
    static /* synthetic */ class C00602 {
        static final /* synthetic */ int[] $SwitchMap$net$minecraft$core$Direction = new int[Direction.values().length];

        static {
            try {
                $SwitchMap$net$minecraft$core$Direction[Direction.SOUTH.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$net$minecraft$core$Direction[Direction.EAST.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
            try {
                $SwitchMap$net$minecraft$core$Direction[Direction.WEST.ordinal()] = 3;
            } catch (NoSuchFieldError e3) {
            }
            try {
                $SwitchMap$net$minecraft$core$Direction[Direction.NORTH.ordinal()] = 4;
            } catch (NoSuchFieldError e4) {
            }
        }
    }

    public BlockState updateShape(BlockState p_196271_1_, Direction p_196271_2_, BlockState p_196271_3_, LevelAccessor p_196271_4_, BlockPos p_196271_5_, BlockPos p_196271_6_) {
        PartType lvt_7_1_ = (PartType) p_196271_1_.getValue(PART);
        if (lvt_7_1_ == PartType.mid) {
            if ((p_196271_2_ == Direction.UP || p_196271_2_ == Direction.DOWN) && !p_196271_3_.is(this)) {
                return Blocks.AIR.defaultBlockState();
            }
            return super.updateShape(p_196271_1_, p_196271_2_, p_196271_3_, p_196271_4_, p_196271_5_, p_196271_6_);
        }
        if (p_196271_2_.getAxis() == Direction.Axis.Y) {
            if ((lvt_7_1_ == PartType.bottom) == (p_196271_2_ == Direction.UP)) {
                if (lvt_7_1_ != PartType.top) {
                    return (!p_196271_3_.is(this) || p_196271_3_.getValue(PART) == lvt_7_1_) ? Blocks.AIR.defaultBlockState() : (BlockState) p_196271_1_.setValue(FACING, p_196271_3_.getValue(FACING));
                }
                if (p_196271_3_.is(this)) {
                    return p_196271_1_;
                }
                return Blocks.AIR.defaultBlockState();
            }
        }
        return (lvt_7_1_ == PartType.bottom && p_196271_2_ == Direction.DOWN && !p_196271_1_.canSurvive(p_196271_4_, p_196271_5_)) ? Blocks.AIR.defaultBlockState() : super.updateShape(p_196271_1_, p_196271_2_, p_196271_3_, p_196271_4_, p_196271_5_, p_196271_6_);
    }

    protected static void preventCreativeDropFromBottomPart(Level p_241471_0_, BlockPos p_241471_1_, BlockState p_241471_2_, Player p_241471_3_) {
        PartType doubleblockhalf = (PartType) p_241471_2_.getValue(PART);
        if (doubleblockhalf == PartType.mid || doubleblockhalf == PartType.top) {
            BlockPos blockpos = p_241471_1_.below();
            BlockState blockstate = p_241471_0_.getBlockState(blockpos);
            if (blockstate.getBlock() == p_241471_2_.getBlock() && blockstate.getValue(PART) == PartType.bottom) {
                p_241471_0_.setBlock(blockpos, Blocks.AIR.defaultBlockState(), 35);
                p_241471_0_.levelEvent(p_241471_3_, 2001, blockpos, Block.getId(blockstate));
            }
        }
    }

    public void playerWillDestroy(Level p_176208_1_, BlockPos p_176208_2_, BlockState p_176208_3_, Player p_176208_4_) {
        if (!p_176208_1_.isClientSide && p_176208_4_.isCreative()) {
            preventCreativeDropFromBottomPart(p_176208_1_, p_176208_2_, p_176208_3_, p_176208_4_);
        }
        super.playerWillDestroy(p_176208_1_, p_176208_2_, p_176208_3_, p_176208_4_);
    }

    public BlockState rotate(BlockState p_52790_, Rotation p_52791_) {
        return (BlockState) p_52790_.setValue(FACING, p_52791_.rotate(p_52790_.getValue(FACING)));
    }

    public BlockState mirror(BlockState p_52787_, Mirror p_52788_) {
        return p_52788_ == Mirror.NONE ? p_52787_ : p_52787_.rotate(p_52788_.getRotation(p_52787_.getValue(FACING)));
    }

    @Nullable
    public BlockState getStateForPlacement(BlockPlaceContext p_196258_1_) {
        BlockPos lvt_2_1_ = p_196258_1_.getClickedPos();
        if (lvt_2_1_.getY() < p_196258_1_.getLevel().getMaxBuildHeight() && p_196258_1_.getLevel().getBlockState(lvt_2_1_.above()).canBeReplaced(p_196258_1_)) {
            p_196258_1_.getLevel();
            return (BlockState) ((BlockState) defaultBlockState().setValue(FACING, p_196258_1_.getHorizontalDirection().getOpposite())).setValue(PART, PartType.bottom);
        }
        return null;
    }

    public void setPlacedBy(Level p_180633_1_, BlockPos p_180633_2_, BlockState p_180633_3_, LivingEntity p_180633_4_, ItemStack p_180633_5_) {
        Integer configPartCount = (Integer) ModernLifeConfig.SERVER.streetLightMiddlePartCount.get();
        if (configPartCount != null) {
            MID_PART_COUNT = configPartCount.intValue();
        }
        p_180633_1_.setBlock(p_180633_2_.above(MID_PART_COUNT + 1), (BlockState) p_180633_3_.setValue(PART, PartType.top), 3);
        for (int i = 0; i < MID_PART_COUNT; i++) {
            int aboveIdx = MID_PART_COUNT - i;
            p_180633_1_.setBlock(p_180633_2_.above(aboveIdx), (BlockState) p_180633_3_.setValue(PART, PartType.mid), 3);
        }
    }

    public boolean hasXairBlocksAbove(LevelReader world, BlockPos pos, int x) {
        BlockPos currentPos = pos;
        for (int i = 0; i < x; i++) {
            currentPos = currentPos.above();
            if (!world.getBlockState(currentPos).isAir()) {
                return false;
            }
        }
        return true;
    }

    public boolean canSurvive(BlockState p_196260_1_, LevelReader p_196260_2_, BlockPos p_196260_3_) {
        BlockPos lvt_4_1_ = p_196260_3_.below();
        BlockState lvt_5_1_ = p_196260_2_.getBlockState(lvt_4_1_);
        if (p_196260_1_.getValue(PART) == PartType.bottom) {
            return lvt_5_1_.isFaceSturdy(p_196260_2_, lvt_4_1_, Direction.UP) && hasXairBlocksAbove(p_196260_2_, p_196260_3_, MID_PART_COUNT + 1);
        }
        return lvt_5_1_.is(this);
    }

    public PushReaction getPistonPushReaction(BlockState p_149656_1_) {
        return PushReaction.DESTROY;
    }

    @OnlyIn(Dist.CLIENT)
    public long getSeed(BlockState p_209900_1_, BlockPos p_209900_2_) {
        return Mth.getSeed(p_209900_2_.getX(), p_209900_2_.below(p_209900_1_.getValue(PART) == PartType.bottom ? 0 : 1).getY(), p_209900_2_.getZ());
    }

    protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> p_206840_1_) {
        p_206840_1_.add(new Property[]{PART, FACING});
    }
}
