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

import com.dairymoose.modernlife.blocks.EaselBlock;
import com.dairymoose.modernlife.core.CustomBlocks;
import com.dairymoose.modernlife.core.ModernLifeCommon;
import com.dairymoose.modernlife.tileentities.EaselBlockEntity;
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.item.ItemStack;
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/ServerboundCanvasPacket.class */
public class ServerboundCanvasPacket implements Packet<ServerGamePacketListener> {
    private byte[] image;
    private BlockPos easelPos;
    private static final Logger LOGGER = LogManager.getLogger();
    private boolean enabled = false;

    public ServerboundCanvasPacket() {
    }

    public ServerboundCanvasPacket(byte[] image, BlockPos easelPos) {
        this.image = image;
        this.easelPos = easelPos;
        ModernLifeCommon.LOGGER.debug("image length = " + image.length);
    }

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

    public void read(FriendlyByteBuf buffer) {
        this.image = buffer.readByteArray();
        this.easelPos = buffer.readBlockPos();
    }

    public void write(FriendlyByteBuf buffer) {
        buffer.writeByteArray(this.image);
        buffer.writeBlockPos(this.easelPos);
    }

    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_) {
        if (!this.enabled) {
            return;
        }
        ModernLifeCommon.LOGGER.debug("Handle ServerboundCanvasPacket");
        if (this.easelPos != 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.easelPos)) {
                BlockState easelState = level.getBlockState(this.easelPos);
                ModernLifeCommon.LOGGER.debug("easelState = " + easelState);
                if (easelState != null && easelState.is(CustomBlocks.BLOCK_EASEL.get()) && !((Boolean) easelState.getValue(EaselBlock.EMPTY)).booleanValue() && this.image != null && this.image.length != 0 && this.image.length < 100000) {
                    ModernLifeCommon.LOGGER.debug("easelPos = " + this.easelPos);
                    BlockEntity tileEntity = level.getChunkAt(this.easelPos).getBlockEntity(this.easelPos);
                    ModernLifeCommon.LOGGER.debug("tileEntity = " + tileEntity);
                    if (tileEntity instanceof EaselBlockEntity) {
                        EaselBlockEntity easelBlockEntity = (EaselBlockEntity) tileEntity;
                        ItemStack currentItem = easelBlockEntity.getItem(0).copy();
                        if (easelBlockEntity != null) {
                            level.setBlockAndUpdate(this.easelPos, (BlockState) easelState.setValue(EaselBlock.EMPTY, true));
                            currentItem.getOrCreateTag().putByteArray("image", this.image);
                            serverHandler.player.addItem(currentItem);
                            easelBlockEntity.setItem(0, ItemStack.EMPTY);
                        }
                    }
                }
            }
        }
    }

    public byte[] getImage() {
        return this.image;
    }
}
