package com.dairymoose.modernlife.network.play.client;

import com.dairymoose.modernlife.core.CustomBlocks;
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.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.Supplier;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ServerGamePacketListener;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.network.NetworkEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/* loaded from: outputsrg.jar:com/dairymoose/modernlife/network/play/client/ServerboundWirelessChannelPacket.class */
public class ServerboundWirelessChannelPacket implements Packet<ServerGamePacketListener> {
    private int newChannel;
    private BlockPos wirelessBlockPos;
    private static final Logger LOGGER = LogManager.getLogger();

    public ServerboundWirelessChannelPacket() {
    }

    public ServerboundWirelessChannelPacket(FriendlyByteBuf buffer) {
        read(buffer);
    }

    public ServerboundWirelessChannelPacket(int newChannel, BlockPos wirelessBlockPos) {
        this.newChannel = newChannel;
        this.wirelessBlockPos = wirelessBlockPos;
    }

    public void read(FriendlyByteBuf p_148837_1_) {
        this.newChannel = p_148837_1_.readInt();
        this.wirelessBlockPos = p_148837_1_.readBlockPos();
    }

    public void write(FriendlyByteBuf p_148840_1_) {
        p_148840_1_.writeInt(this.newChannel);
        p_148840_1_.writeBlockPos(this.wirelessBlockPos);
    }

    public void handle(Supplier<NetworkEvent.Context> ctx) {
        ctx.get().enqueueWork(() -> {
            ((NetworkEvent.Context) ctx.get()).getSender();
            handle((ServerGamePacketListener) ((NetworkEvent.Context) ctx.get()).getNetworkManager().getPacketListener());
        });
        ctx.get().setPacketHandled(true);
    }

    public static <T extends BlockEntity> void purgeRemovedChannelHolders(Set<T> data) {
        if (data == null) {
            return;
        }
        List<T> toRemove = new ArrayList<>();
        for (T tileEntity : data) {
            if (tileEntity.hasLevel() && !tileEntity.getLevel().isLoaded(tileEntity.getBlockPos())) {
                toRemove.add(tileEntity);
            }
        }
        for (T t : toRemove) {
            ModernLifeCommon.LOGGER.debug("Purge entity: " + t);
            data.remove(t);
        }
    }

    public void handle(ServerGamePacketListener p_148833_1_) {
        IChannelHolder iChannelHolder;
        ModernLifeCommon.LOGGER.debug("Handle ServerboundWirelessChannelPacket");
        if (this.wirelessBlockPos != null && (p_148833_1_ instanceof ServerGamePacketListenerImpl)) {
            ServerGamePacketListenerImpl serverHandler = (ServerGamePacketListenerImpl) p_148833_1_;
            new CompoundTag();
            ServerLevel level = serverHandler.player.getLevel();
            ModernLifeCommon.LOGGER.debug("world = " + level);
            if (level != null && level.isLoaded(this.wirelessBlockPos)) {
                BlockState wirelessBlockState = level.getBlockState(this.wirelessBlockPos);
                ModernLifeCommon.LOGGER.debug("wirelessBlockState = " + wirelessBlockState);
                if (wirelessBlockState != null) {
                    if ((wirelessBlockState.is(CustomBlocks.BLOCK_POWER_TRANSMITTER.get()) || wirelessBlockState.is(CustomBlocks.BLOCK_POWER_RECEIVER.get())) && this.newChannel >= 0 && this.newChannel <= 9999) {
                        ModernLifeCommon.LOGGER.debug("wirelessBlockPos = " + this.wirelessBlockPos);
                        IChannelHolder blockEntity = level.getChunkAt(this.wirelessBlockPos).getBlockEntity(this.wirelessBlockPos);
                        ModernLifeCommon.LOGGER.debug("tileEntity = " + blockEntity);
                        if ((blockEntity instanceof IChannelHolder) && (iChannelHolder = blockEntity) != null) {
                            iChannelHolder.setCurrentChannel(this.newChannel);
                            Set<PowerTransmitterBlockEntity> transmitters = PowerTransmitterBlockEntity.perChannelPowerTransmitters.get(Integer.valueOf(this.newChannel));
                            int transmitterCount = 0;
                            if (transmitters != null) {
                                purgeRemovedChannelHolders(transmitters);
                                transmitterCount = transmitters.size();
                            }
                            Set<PowerReceiverBlockEntity> receivers = PowerReceiverBlockEntity.perChannelPowerReceivers.get(Integer.valueOf(this.newChannel));
                            int receiverCount = 0;
                            if (receivers != null) {
                                purgeRemovedChannelHolders(receivers);
                                receiverCount = receivers.size();
                            }
                            String outputText = "Channel " + this.newChannel + " now has " + transmitterCount + " transmitters and " + receiverCount + " receivers";
                            serverHandler.player.sendMessage(new TextComponent(outputText), serverHandler.player.getUUID());
                            if (transmitters != null) {
                                for (PowerTransmitterBlockEntity transmitterNow : transmitters) {
                                    String trans = "t=" + transmitterNow;
                                    ModernLifeCommon.LOGGER.debug(trans);
                                }
                            }
                            if (receivers != null) {
                                for (PowerReceiverBlockEntity receiverNow : receivers) {
                                    String recv = "r=" + receiverNow;
                                    ModernLifeCommon.LOGGER.debug(recv);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
