package com.dairymoose.modernlife.blocks;

import com.dairymoose.modernlife.blocks.gui.WirelessPowerScreen;
import com.dairymoose.modernlife.core.CustomBlocks;
import com.dairymoose.modernlife.core.ModernLifeCommon;
import com.dairymoose.modernlife.tileentities.IChannelHolder;
import com.dairymoose.modernlife.tileentities.PowerTransmitterBlockEntity;
import java.util.List;
import javax.annotation.Nullable;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
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.DirectionProperty;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.phys.BlockHitResult;
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 net.minecraftforge.fml.DistExecutor;

/* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/PowerTransmitterBlock.class */
public class PowerTransmitterBlock extends Block implements EntityBlock {
    public static final DirectionProperty FACING = BlockStateProperties.HORIZONTAL_FACING;
    public static final IntegerProperty POWER = BlockStateProperties.POWER;

    public PowerTransmitterBlock(Properties p_i48377_1_) {
        super(p_i48377_1_);
    }

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

    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.PowerTransmitterBlock.1
                    @Override // java.lang.Runnable
                    public void run() {
                        IChannelHolder blockEntity = (IChannelHolder) p_225533_2_.getBlockEntity(p_225533_3_);
                        if (blockEntity instanceof IChannelHolder) {
                            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 PowerTransmitterBlockEntity) {
                PowerTransmitterBlockEntity transmitter = (PowerTransmitterBlockEntity) tileEntity;
                transmitter.setCurrentChannel(0);
            }
        }
    }

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

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

    public VoxelShape getCollisionShape(BlockState state, BlockGetter blockReader, BlockPos blockPos, CollisionContext ctx) {
        return super.getCollisionShape(state, blockReader, blockPos, ctx);
    }

    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 PowerTransmitterBlockEntity) {
            PowerTransmitterBlockEntity transmitter = (PowerTransmitterBlockEntity) entity;
            ModernLifeCommon.LOGGER.debug("remove transmitter from channel: " + transmitter.getCurrentChannel());
            transmitter.removeFromChannel(transmitter.getCurrentChannel());
            transmitter.updateReceivers(world);
        }
        super.onRemove(state, world, pos, p_196243_4_, p_196243_5_);
    }

    public void neighborChanged(BlockState state, Level world, BlockPos pos, Block block, BlockPos neighbor, boolean p_220069_6_) {
        if (!world.getBlockState(neighbor).is(CustomBlocks.BLOCK_POWER_RECEIVER.get()) && state.is(CustomBlocks.BLOCK_POWER_TRANSMITTER.get())) {
            BlockEntity entity = world.getBlockEntity(pos);
            if (entity instanceof PowerTransmitterBlockEntity) {
                world.setBlock(pos, (BlockState) state.setValue(POWER, Integer.valueOf(world.getBestNeighborSignal(pos))), 2);
                PowerTransmitterBlockEntity transmitter = (PowerTransmitterBlockEntity) entity;
                ModernLifeCommon.LOGGER.debug("add transmitter to channel: " + transmitter.getCurrentChannel());
                transmitter.addToChannel(transmitter.getCurrentChannel());
                transmitter.updateReceivers(world);
            }
        }
    }

    public BlockState getStateForPlacement(BlockPlaceContext context) {
        return (BlockState) ((BlockState) defaultBlockState().setValue(FACING, context.getHorizontalDirection())).setValue(POWER, Integer.valueOf(context.getLevel().getBestNeighborSignal(context.getClickedPos())));
    }

    public VoxelShape getShape(BlockState p_220053_1_, BlockGetter p_220053_2_, BlockPos p_220053_3_, CollisionContext p_220053_4_) {
        return Block.box(0.0d, 0.0d, 0.0d, 16.0d, 16.0d, 16.0d);
    }
}
