package com.tiedup.remake.network.cell;

import com.tiedup.remake.network.base.AbstractClientPacket;
import java.util.UUID;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

/**
 * Server → Client packet: opens the Cell Core menu with all cell info.
 * Carries all data so the Info panel works without a round-trip.
 */
public class PacketOpenCoreMenu extends AbstractClientPacket {

    private final BlockPos corePos;
    private final UUID cellId;
    private final String cellName;
    private final String stateName;
    private final int interiorVolume;
    private final int wallCount;
    private final int breachCount;
    private final int prisonerCount;
    private final int bedCount;
    private final int doorCount;
    private final int anchorCount;
    private final boolean hasSpawn;
    private final boolean hasDelivery;
    private final boolean hasDisguise;

    public PacketOpenCoreMenu(
        BlockPos corePos,
        UUID cellId,
        String cellName,
        String stateName,
        int interiorVolume,
        int wallCount,
        int breachCount,
        int prisonerCount,
        int bedCount,
        int doorCount,
        int anchorCount,
        boolean hasSpawn,
        boolean hasDelivery,
        boolean hasDisguise
    ) {
        this.corePos = corePos;
        this.cellId = cellId;
        this.cellName = cellName;
        this.stateName = stateName;
        this.interiorVolume = interiorVolume;
        this.wallCount = wallCount;
        this.breachCount = breachCount;
        this.prisonerCount = prisonerCount;
        this.bedCount = bedCount;
        this.doorCount = doorCount;
        this.anchorCount = anchorCount;
        this.hasSpawn = hasSpawn;
        this.hasDelivery = hasDelivery;
        this.hasDisguise = hasDisguise;
    }

    public static void encode(PacketOpenCoreMenu msg, FriendlyByteBuf buf) {
        buf.writeBlockPos(msg.corePos);
        buf.writeUUID(msg.cellId);
        buf.writeUtf(msg.cellName, 64);
        buf.writeUtf(msg.stateName, 32);
        buf.writeVarInt(msg.interiorVolume);
        buf.writeVarInt(msg.wallCount);
        buf.writeVarInt(msg.breachCount);
        buf.writeVarInt(msg.prisonerCount);
        buf.writeVarInt(msg.bedCount);
        buf.writeVarInt(msg.doorCount);
        buf.writeVarInt(msg.anchorCount);
        buf.writeBoolean(msg.hasSpawn);
        buf.writeBoolean(msg.hasDelivery);
        buf.writeBoolean(msg.hasDisguise);
    }

    public static PacketOpenCoreMenu decode(FriendlyByteBuf buf) {
        return new PacketOpenCoreMenu(
            buf.readBlockPos(),
            buf.readUUID(),
            buf.readUtf(64),
            buf.readUtf(32),
            buf.readVarInt(),
            buf.readVarInt(),
            buf.readVarInt(),
            buf.readVarInt(),
            buf.readVarInt(),
            buf.readVarInt(),
            buf.readVarInt(),
            buf.readBoolean(),
            buf.readBoolean(),
            buf.readBoolean()
        );
    }

    @Override
    @OnlyIn(Dist.CLIENT)
    protected void handleClientImpl() {
        ClientHandler.handle(this);
    }

    @OnlyIn(Dist.CLIENT)
    private static class ClientHandler {

        private static void handle(PacketOpenCoreMenu pkt) {
            net.minecraft.client.Minecraft.getInstance().setScreen(
                new com.tiedup.remake.client.gui.screens.CellCoreScreen(
                    pkt.corePos,
                    pkt.cellId,
                    pkt.cellName,
                    pkt.stateName,
                    pkt.interiorVolume,
                    pkt.wallCount,
                    pkt.breachCount,
                    pkt.prisonerCount,
                    pkt.bedCount,
                    pkt.doorCount,
                    pkt.anchorCount,
                    pkt.hasSpawn,
                    pkt.hasDelivery,
                    pkt.hasDisguise
                )
            );
        }
    }
}
