package com.dairymoose.modernlife.blocks;

import com.dairymoose.modernlife.blocks.gui.WirelessPowerScreen;
import com.dairymoose.modernlife.core.ModernLifeCommon;
import com.dairymoose.modernlife.tileentities.IChannelHolder;
import com.dairymoose.modernlife.tileentities.PowerReceiverBlockEntity;
import com.dairymoose.modernlife.tileentities.PowerTransmitterBlockEntity;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.Mirror;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.block.entity.BlockEntity;
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.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.BooleanOp;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.DistExecutor;

/* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/PowerReceiverBlock.class */
public class PowerReceiverBlock extends Block implements EntityBlock {
    protected static final VoxelShape SHAPE = (VoxelShape) Stream.of((Object[]) new VoxelShape[]{Block.box(6.5d, 2.0d, 6.5d, 9.5d, 5.0d, 9.5d), Block.box(0.0d, 0.0d, 0.0d, 16.0d, 4.0d, 16.0d), Block.box(2.0d, 4.0d, 2.0d, 14.0d, 8.0d, 14.0d), Block.box(4.0d, 8.0d, 4.0d, 12.0d, 12.0d, 12.0d), Block.box(6.0d, 12.0d, 6.0d, 10.0d, 16.0d, 10.0d), Block.box(0.0d, 0.0d, 0.0d, 16.0d, 1.0d, 16.0d)}).reduce((v1, v2) -> {
        return Shapes.join((VoxelShape) v1, (VoxelShape) v2, BooleanOp.OR);
    }).get();
    public static final DirectionProperty FACING = BlockStateProperties.HORIZONTAL_FACING;
    public static final BooleanProperty POWERED = BlockStateProperties.POWERED;

    @OnlyIn(Dist.CLIENT)
    public void appendHoverText(ItemStack itemStack, @Nullable BlockGetter blockReader, List<Component> list, TooltipFlag tooltipFlag) {
        list.add(new TextComponent("Receives power on the selected channel from all transmitters"));
        list.add(new TextComponent("Right click block to change current channel"));
    }

    public PowerReceiverBlock(Properties p_i48377_1_) {
        super(p_i48377_1_);
        registerDefaultState((BlockState) ((BlockState) this.stateDefinition.any().setValue(FACING, Direction.SOUTH)).setValue(POWERED, Boolean.FALSE));
    }

    public BlockState rotate(BlockState state, Rotation rotation) {
        return (BlockState) state.setValue(FACING, rotation.rotate(state.getValue(FACING)));
    }

    public BlockState mirror(BlockState state, Mirror mirror) {
        return state.rotate(mirror.getRotation(state.getValue(FACING)));
    }

    public InteractionResult use(BlockState p_225533_1_, Level p_225533_2_, BlockPos p_225533_3_, Player p_225533_4_, InteractionHand p_225533_5_, BlockHitResult p_225533_6_) {
        if (p_225533_2_.isClientSide) {
            DistExecutor.runWhenOn(Dist.CLIENT, () -> {
                return new Runnable() { // from class: com.dairymoose.modernlife.blocks.PowerReceiverBlock.1
                    @Override // java.lang.Runnable
                    public void run() {
                        IChannelHolder blockEntity = (IChannelHolder) p_225533_2_.getBlockEntity(p_225533_3_);
                        if (blockEntity != null) {
                            IChannelHolder channelHolder = blockEntity;
                            WirelessPowerScreen screen = new WirelessPowerScreen(p_225533_3_, channelHolder.getCurrentChannel());
                            Minecraft.getInstance().setScreen(screen);
                        }
                    }
                };
            });
        }
        return InteractionResult.CONSUME;
    }

    public void setPlacedBy(Level world, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack itemStack) {
        if (!world.isClientSide) {
            BlockEntity tileEntity = world.getBlockEntity(pos);
            if (tileEntity instanceof PowerReceiverBlockEntity) {
                PowerReceiverBlockEntity receiver = (PowerReceiverBlockEntity) tileEntity;
                receiver.setCurrentChannel(0);
            }
        }
    }

    public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
        return PowerReceiverBlockEntity.POWER_RECEIVER.create(pos, state);
    }

    public void onRemove(BlockState state, Level world, BlockPos pos, BlockState p_196243_4_, boolean p_196243_5_) {
        BlockEntity entity = world.getBlockEntity(pos);
        if (entity instanceof PowerReceiverBlockEntity) {
            PowerReceiverBlockEntity receiver = (PowerReceiverBlockEntity) entity;
            ModernLifeCommon.LOGGER.debug("remove receiver from channel: " + receiver.getCurrentChannel());
            receiver.removeFromChannel(receiver.getCurrentChannel());
        }
        if (!p_196243_5_ && !state.is(p_196243_4_.getBlock())) {
            updateNeighbours(state, world, pos);
            super.onRemove(state, world, pos, p_196243_4_, p_196243_5_);
        }
    }

    private int getPowerFromTransmitter(BlockPos pos, BlockGetter reader) {
        BlockEntity entity = reader.getBlockEntity(pos);
        ModernLifeCommon.LOGGER.debug("get power: " + entity);
        if (entity instanceof PowerReceiverBlockEntity) {
            ModernLifeCommon.LOGGER.debug("entity is instance");
            PowerReceiverBlockEntity receiver = (PowerReceiverBlockEntity) entity;
            Set<PowerTransmitterBlockEntity> transmitters = PowerTransmitterBlockEntity.perChannelPowerTransmitters.get(Integer.valueOf(receiver.getCurrentChannel()));
            int powerSum = 0;
            if (transmitters != null) {
                ModernLifeCommon.LOGGER.debug("found " + transmitters.size() + " transmitters for channel " + receiver.getCurrentChannel());
                for (PowerTransmitterBlockEntity transmitter : transmitters) {
                    BlockState state = transmitter.getBlockState();
                    powerSum += ((Integer) state.getValue(BlockStateProperties.POWER)).intValue();
                }
                if (powerSum > 15) {
                    powerSum = 15;
                }
                ModernLifeCommon.LOGGER.debug("got power sum = " + powerSum);
            } else {
                ModernLifeCommon.LOGGER.debug("transmitters is null for channel " + receiver.getCurrentChannel());
            }
            return powerSum;
        }
        return 0;
    }

    public boolean isSignalSource(BlockState p_60571_) {
        return true;
    }

    public boolean useShapeForLightOcclusion(BlockState p_220074_1_) {
        return true;
    }

    public int getSignal(BlockState state, BlockGetter reader, BlockPos pos, Direction p_180656_4_) {
        ModernLifeCommon.LOGGER.debug("getSignal for state=" + state + " and pos=" + pos);
        return getPowerFromTransmitter(pos, reader);
    }

    public int getDirectSignal(BlockState p_176211_1_, BlockGetter p_176211_2_, BlockPos p_176211_3_, Direction p_176211_4_) {
        ModernLifeCommon.LOGGER.debug("getDirectSignal");
        return getPowerFromTransmitter(p_176211_3_, p_176211_2_);
    }

    private void updateNeighbours(BlockState p_196378_1_, Level p_196378_2_, BlockPos p_196378_3_) {
        p_196378_2_.updateNeighborsAt(p_196378_3_, this);
        p_196378_2_.updateNeighborsAt(p_196378_3_.relative(p_196378_1_.getValue(FACING).getOpposite()), this);
    }

    public VoxelShape getShape(BlockState bs, BlockGetter reader, BlockPos pos, CollisionContext sel) {
        return SHAPE;
    }

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

    public BlockState getStateForPlacement(BlockPlaceContext context) {
        Direction[] var2 = context.getNearestLookingDirections();
        for (Direction lvt_5_1_ : var2) {
            if (lvt_5_1_.getAxis() != Direction.Axis.Y) {
                BlockState lvt_6_2_ = (BlockState) defaultBlockState().setValue(FACING, lvt_5_1_.getOpposite());
                if (lvt_6_2_.canSurvive(context.getLevel(), context.getClickedPos())) {
                    return lvt_6_2_;
                }
            }
        }
        return defaultBlockState();
    }
}
