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

import com.dairymoose.modernlife.core.ModernLifeCommon;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ServerGamePacketListener;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import net.minecraft.world.entity.player.Player;
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/ServerboundRequestCanvasPacket.class */
public class ServerboundRequestCanvasPacket implements Packet<ServerGamePacketListener> {
    private long uniqueId;
    private static final Logger LOGGER = LogManager.getLogger();
    private static Map<Player, Map<Long, RequestInfo>> requestTimestamps = new HashMap();

    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/network/play/client/ServerboundRequestCanvasPacket$RequestInfo.class */
    public static class RequestInfo {
        long timestamp;

        private RequestInfo() {
        }
    }

    public ServerboundRequestCanvasPacket() {
    }

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

    public ServerboundRequestCanvasPacket(long uniqueId) {
        this.uniqueId = uniqueId;
    }

    public void read(FriendlyByteBuf packet) {
        this.uniqueId = packet.readLong();
    }

    public void write(FriendlyByteBuf packet) {
        packet.writeLong(this.uniqueId);
    }

    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 void handle(ServerGamePacketListener p_148833_1_) {
        ModernLifeCommon.LOGGER.debug("Handle ServerboundRequestCanvasPacket");
        if (this.uniqueId > 0 && (p_148833_1_ instanceof ServerGamePacketListenerImpl)) {
            ServerGamePacketListenerImpl serverHandler = (ServerGamePacketListenerImpl) p_148833_1_;
            long currentTime = System.currentTimeMillis();
            long timestamp = 0;
            Map<Long, RequestInfo> requestMap = requestTimestamps.get(serverHandler.player);
            if (requestMap != null) {
                RequestInfo requestInfo = requestMap.get(Long.valueOf(this.uniqueId));
                if (requestInfo != null) {
                    timestamp = requestInfo.timestamp;
                }
            } else {
                requestMap = new HashMap();
            }
            long timeDiff = currentTime - timestamp;
            if (timeDiff >= 30000) {
                RequestInfo requestInfo2 = new RequestInfo();
                requestInfo2.timestamp = currentTime;
                requestMap.put(Long.valueOf(this.uniqueId), requestInfo2);
                requestTimestamps.put(serverHandler.player, requestMap);
                ServerboundMultipartCameraPacket.pushImageUpdate(this.uniqueId, serverHandler.player);
                ModernLifeCommon.LOGGER.debug("sent reply to canvas request");
            }
        }
    }
}
