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

import com.dairymoose.modernlife.core.ModernLifeCommon;
import com.dairymoose.modernlife.network.play.client.ServerboundMultipartCameraPacket;
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.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/ServerboundCanvasChecksumPacket.class */
public class ServerboundCanvasChecksumPacket implements Packet<ServerGamePacketListener> {
    private static final Logger LOGGER = LogManager.getLogger();
    private long uniqueId;
    private int checksum;

    public ServerboundCanvasChecksumPacket() {
        this.uniqueId = -1L;
        this.checksum = 0;
    }

    public ServerboundCanvasChecksumPacket(long uniqueId, int checksum) {
        this.uniqueId = -1L;
        this.checksum = 0;
        this.uniqueId = uniqueId;
        this.checksum = checksum;
    }

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

    public void read(FriendlyByteBuf buffer) {
        this.uniqueId = buffer.readLong();
        this.checksum = buffer.readInt();
    }

    public void write(FriendlyByteBuf buffer) {
        buffer.writeLong(this.uniqueId);
        buffer.writeInt(this.checksum);
    }

    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 listener) {
        Integer hashCode;
        ModernLifeCommon.LOGGER.debug("Handle ServerboundCanvasChecksumPacket");
        if (this.uniqueId >= 0 && (listener instanceof ServerGamePacketListenerImpl)) {
            ServerGamePacketListenerImpl handler = (ServerGamePacketListenerImpl) listener;
            long timestamp = System.currentTimeMillis();
            ServerboundMultipartCameraPacket.CanvasHashCodeData chcd = ServerboundMultipartCameraPacket.canvasHashCodes.get(Long.valueOf(this.uniqueId));
            if (chcd != null) {
                long timeDiff = timestamp - chcd.timestamp;
                if (timeDiff >= 5000) {
                    ModernLifeCommon.LOGGER.debug("Recalculate image checksum for ID=" + this.uniqueId);
                    chcd = null;
                }
            }
            if (chcd == null) {
                ServerboundMultipartCameraPacket.PngSizeHolder sizeHolder = new ServerboundMultipartCameraPacket.PngSizeHolder();
                byte[] compressed = ServerboundMultipartCameraPacket.loadPng(this.uniqueId, sizeHolder);
                hashCode = Integer.valueOf(ModernLifeCommon.getHashCodeForBytes(compressed));
                ServerboundMultipartCameraPacket.CanvasHashCodeData chcd2 = new ServerboundMultipartCameraPacket.CanvasHashCodeData();
                chcd2.hashCode = hashCode;
                chcd2.timestamp = timestamp;
                ServerboundMultipartCameraPacket.canvasHashCodes.put(Long.valueOf(this.uniqueId), chcd2);
            } else {
                hashCode = chcd.hashCode;
            }
            Logger logger = ModernLifeCommon.LOGGER;
            int i = this.checksum;
            logger.debug("Checksum verification for " + this.uniqueId + ", server=" + logger + ", client=" + hashCode);
            if (hashCode.intValue() != this.checksum) {
                String playerName = "";
                if (handler.player != null && handler.player.getName() != null) {
                    playerName = handler.player.getName().getString();
                }
                Logger logger2 = ModernLifeCommon.LOGGER;
                logger2.info("Client checksum mismatch on image " + this.uniqueId + ", pushing image update to player " + logger2);
                ServerboundMultipartCameraPacket.pushImageUpdate(this.uniqueId, handler.player);
            }
        }
    }
}
