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

import com.dairymoose.modernlife.blocks.CanvasBlock;
import com.dairymoose.modernlife.core.CustomBlocks;
import com.dairymoose.modernlife.core.ModernLifeCommon;
import com.dairymoose.modernlife.tileentities.PrinterBlockEntity;
import java.util.function.Supplier;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
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.ServerPlayer;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ItemLike;
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/ServerboundPrintSizePacket.class */
public class ServerboundPrintSizePacket implements Packet<ServerGamePacketListener> {
    private static final Logger LOGGER = LogManager.getLogger();
    private int printSize;
    private float blockWidth;
    private float blockHeight;
    private float xOffset;
    private float yOffset;
    private BlockPos pos;

    public ServerboundPrintSizePacket() {
        this.printSize = -1;
        this.blockWidth = 0.0f;
        this.blockHeight = 0.0f;
        this.xOffset = 0.0f;
        this.yOffset = 0.0f;
    }

    public ServerboundPrintSizePacket(int printSize, BlockPos pos) {
        this.printSize = -1;
        this.blockWidth = 0.0f;
        this.blockHeight = 0.0f;
        this.xOffset = 0.0f;
        this.yOffset = 0.0f;
        this.printSize = printSize;
        this.pos = pos;
    }

    public ServerboundPrintSizePacket(float blockWidth, float blockHeight, float xOffset, float yOffset, BlockPos pos) {
        this.printSize = -1;
        this.blockWidth = 0.0f;
        this.blockHeight = 0.0f;
        this.xOffset = 0.0f;
        this.yOffset = 0.0f;
        this.printSize = 0;
        this.blockWidth = blockWidth;
        this.blockHeight = blockHeight;
        this.xOffset = xOffset;
        this.yOffset = yOffset;
        this.pos = pos;
    }

    public ServerboundPrintSizePacket(FriendlyByteBuf buffer) {
        this.printSize = -1;
        this.blockWidth = 0.0f;
        this.blockHeight = 0.0f;
        this.xOffset = 0.0f;
        this.yOffset = 0.0f;
        read(buffer);
    }

    public void read(FriendlyByteBuf buffer) {
        this.printSize = buffer.readInt();
        this.blockWidth = buffer.readFloat();
        this.blockHeight = buffer.readFloat();
        this.xOffset = buffer.readFloat();
        this.yOffset = buffer.readFloat();
        this.pos = buffer.readBlockPos();
    }

    public void write(FriendlyByteBuf buffer) {
        buffer.writeInt(this.printSize);
        buffer.writeFloat(this.blockWidth);
        buffer.writeFloat(this.blockHeight);
        buffer.writeFloat(this.xOffset);
        buffer.writeFloat(this.yOffset);
        buffer.writeBlockPos(this.pos);
    }

    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 float sanitizeWidthHeight(float input) {
        if (input <= 1.0f) {
            input = 1.0f;
        }
        if (input > 10.0f) {
            input = 10.0f;
        }
        return input;
    }

    public static float sanitizeOffset(float input) {
        if (input < -1.0f) {
            input = -1.0f;
        }
        if (input > 1.0f) {
            input = 1.0f;
        }
        return input;
    }

    public void handle(ServerGamePacketListener p_148833_1_) {
        ListTag imageList;
        ModernLifeCommon.LOGGER.debug("Handle ServerboundPrintSizePacket");
        if (this.printSize >= 0 && this.printSize <= CanvasBlock.MAX_SIZE && (p_148833_1_ instanceof ServerGamePacketListenerImpl)) {
            ServerGamePacketListenerImpl handler = (ServerGamePacketListenerImpl) p_148833_1_;
            ModernLifeCommon.LOGGER.debug("check camera");
            ServerPlayer serverPlayer = handler.player;
            ItemStack itemStack = handler.player.getMainHandItem();
            if (itemStack.getItem() != CustomBlocks.ITEM_CAMERA.get()) {
                if (itemStack.getItem() == CustomBlocks.ITEM_CANVAS.get()) {
                    itemStack.getOrCreateTag().putInt("Size", this.printSize);
                    if (this.printSize == 0) {
                        itemStack.getTag().putFloat("BlockWidth", sanitizeWidthHeight(this.blockWidth));
                        itemStack.getTag().putFloat("BlockHeight", sanitizeWidthHeight(this.blockHeight));
                        itemStack.getTag().putFloat("xOffset", sanitizeOffset(this.xOffset));
                        itemStack.getTag().putFloat("yOffset", sanitizeOffset(this.yOffset));
                        return;
                    }
                    return;
                }
                return;
            }
            ListTag listTag = itemStack.getTag().get("ImageList");
            if ((listTag instanceof ListTag) && (imageList = listTag) != null) {
                int printedPhotoCount = 0;
                if (!((Player) serverPlayer).level.isLoaded(this.pos)) {
                    return;
                }
                BlockEntity tileEntity = ((Player) serverPlayer).level.getBlockEntity(this.pos);
                if (tileEntity instanceof PrinterBlockEntity) {
                    BlockState blockState = ((Player) serverPlayer).level.getBlockState(this.pos);
                    MenuProvider containerProvider = CustomBlocks.BLOCK_PRINTER.get().getMenuProvider(blockState, ((Player) serverPlayer).level, this.pos);
                    if (containerProvider != null) {
                        serverPlayer.openMenu(containerProvider);
                    }
                    PrinterBlockEntity printer = (PrinterBlockEntity) tileEntity;
                    if (printer.freeSlotCount() != 0) {
                        int i = 0;
                        while (i < imageList.size()) {
                            CompoundTag compoundTag = imageList.get(i);
                            if (compoundTag instanceof CompoundTag) {
                                CompoundTag nbt = compoundTag;
                                if (nbt.contains("UniqueId")) {
                                    int free = printer.getFirstFreeSlot();
                                    if (free == -1) {
                                        break;
                                    }
                                    imageList.remove(i);
                                    i--;
                                    long uniqueId = nbt.getLong("UniqueId");
                                    itemStack.setDamageValue(itemStack.getDamageValue() - 1);
                                    ItemStack newItemStack = new ItemStack(new ItemLike() { // from class: com.dairymoose.modernlife.network.play.client.ServerboundPrintSizePacket.1
                                        C01401() {
                                        }

                                        public Item asItem() {
                                            return CustomBlocks.ITEM_CANVAS.get();
                                        }
                                    });
                                    newItemStack.getOrCreateTag().putLong("UniqueId", uniqueId);
                                    if (itemStack.getTag().contains("TextureSize")) {
                                        newItemStack.getTag().putInt("TextureSize", itemStack.getTag().getInt("TextureSize"));
                                    }
                                    if (itemStack.getTag().contains("TextureWidth")) {
                                        newItemStack.getTag().putInt("TextureWidth", itemStack.getTag().getInt("TextureWidth"));
                                    }
                                    if (itemStack.getTag().contains("TextureHeight")) {
                                        newItemStack.getTag().putInt("TextureHeight", itemStack.getTag().getInt("TextureHeight"));
                                    }
                                    newItemStack.getTag().putInt("Size", this.printSize);
                                    if (this.printSize == 0) {
                                        newItemStack.getTag().putFloat("BlockWidth", sanitizeWidthHeight(this.blockWidth));
                                        newItemStack.getTag().putFloat("BlockHeight", sanitizeWidthHeight(this.blockHeight));
                                        newItemStack.getTag().putFloat("xOffset", sanitizeOffset(this.xOffset));
                                        newItemStack.getTag().putFloat("yOffset", sanitizeOffset(this.yOffset));
                                    }
                                    newItemStack.setHoverName(new TextComponent("Photograph"));
                                    printer.setItem(free, newItemStack);
                                    printedPhotoCount++;
                                } else {
                                    continue;
                                }
                            }
                            i++;
                        }
                        if (printedPhotoCount > 0) {
                            String photosText = "photos";
                            if (printedPhotoCount == 1) {
                                photosText = "photo";
                            }
                            serverPlayer.sendMessage(new TextComponent("Printed " + String.valueOf(printedPhotoCount) + " " + photosText), serverPlayer.getUUID());
                        }
                    } else if (imageList.size() > 0) {
                        serverPlayer.sendMessage(new TextComponent("Printer tray is full!  Clear some space to print your photos"), serverPlayer.getUUID());
                    }
                }
            }
            ModernLifeCommon.LOGGER.debug("finished checking camera");
        }
    }

    /* renamed from: com.dairymoose.modernlife.network.play.client.ServerboundPrintSizePacket$1 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/network/play/client/ServerboundPrintSizePacket$1.class */
    public class C01401 implements ItemLike {
        C01401() {
        }

        public Item asItem() {
            return CustomBlocks.ITEM_CANVAS.get();
        }
    }
}
