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

import com.dairymoose.modernlife.core.ModernLifeCommon;
import com.dairymoose.modernlife.tileentities.PhotocopierBlockEntity;
import java.util.function.Supplier;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
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.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/ServerboundCanvasCopyRequestPacket.class */
public class ServerboundCanvasCopyRequestPacket implements Packet<ServerGamePacketListener> {
    private static final Logger LOGGER = LogManager.getLogger();
    private long uniqueId;
    private int numberOfCopies;
    private BlockPos pos;
    private CompoundTag nbt;

    public ServerboundCanvasCopyRequestPacket() {
        this.uniqueId = -1L;
        this.numberOfCopies = 0;
    }

    public ServerboundCanvasCopyRequestPacket(long uniqueId, int numberOfCopies, CompoundTag nbt, BlockPos pos) {
        this.uniqueId = -1L;
        this.numberOfCopies = 0;
        this.uniqueId = uniqueId;
        this.numberOfCopies = numberOfCopies;
        this.pos = pos;
        this.nbt = nbt == null ? new CompoundTag() : nbt;
    }

    public ServerboundCanvasCopyRequestPacket(FriendlyByteBuf buffer) {
        this.uniqueId = -1L;
        this.numberOfCopies = 0;
        read(buffer);
    }

    public void read(FriendlyByteBuf buffer) {
        this.uniqueId = buffer.readLong();
        this.numberOfCopies = buffer.readInt();
        this.pos = buffer.readBlockPos();
        this.nbt = buffer.readNbt();
    }

    public void write(FriendlyByteBuf buffer) {
        buffer.writeLong(this.uniqueId);
        buffer.writeInt(this.numberOfCopies);
        buffer.writeBlockPos(this.pos);
        buffer.writeNbt(this.nbt);
    }

    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 int sanitizeNumberOfCopies(int numberOfCopies) {
        if (numberOfCopies < 0) {
            numberOfCopies = 0;
        }
        if (numberOfCopies > 5) {
            numberOfCopies = 5;
        }
        return numberOfCopies;
    }

    public void handle(ServerGamePacketListener listener) {
        ModernLifeCommon.LOGGER.debug("Handle ServerboundCanvasCopyRequestPacket");
        if (this.uniqueId >= 0 && (listener instanceof ServerGamePacketListenerImpl)) {
            ServerGamePacketListenerImpl handler = (ServerGamePacketListenerImpl) listener;
            this.numberOfCopies = sanitizeNumberOfCopies(this.numberOfCopies);
            ServerLevel level = handler.getPlayer().getLevel();
            if (level.isLoaded(this.pos)) {
                BlockEntity entity = level.getBlockEntity(this.pos);
                if (entity instanceof PhotocopierBlockEntity) {
                    PhotocopierBlockEntity photocopier = (PhotocopierBlockEntity) entity;
                    photocopier.addTaskToQueue(this.uniqueId, this.nbt, this.numberOfCopies);
                }
            }
        }
    }
}
