package com.tiedup.remake.network.cell;

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

/**
 * Packet to open the CellSelector screen on the client.
 * Server -> Client
 */
public class PacketOpenCellSelector extends AbstractClientPacket {

    private final UUID targetEntityUUID; // The entity whose collar we want to assign
    private final List<CellOption> cells;

    /**
     * Cell option for the selector.
     */
    public static class CellOption {

        public final UUID cellId;
        public final String displayName;
        public final int prisonerCount;
        public final int maxPrisoners;

        public CellOption(
            UUID cellId,
            String displayName,
            int prisonerCount,
            int maxPrisoners
        ) {
            this.cellId = cellId;
            this.displayName = displayName;
            this.prisonerCount = prisonerCount;
            this.maxPrisoners = maxPrisoners;
        }
    }

    public PacketOpenCellSelector(
        UUID targetEntityUUID,
        List<CellOption> cells
    ) {
        this.targetEntityUUID = targetEntityUUID;
        this.cells = cells != null ? cells : new ArrayList<>();
    }

    public void encode(FriendlyByteBuf buf) {
        buf.writeUUID(this.targetEntityUUID);
        buf.writeInt(this.cells.size());

        for (CellOption cell : this.cells) {
            buf.writeUUID(cell.cellId);
            buf.writeUtf(cell.displayName, 64);
            buf.writeInt(cell.prisonerCount);
            buf.writeInt(cell.maxPrisoners);
        }
    }

    public static PacketOpenCellSelector decode(FriendlyByteBuf buf) {
        UUID targetEntityUUID = buf.readUUID();
        int cellCount = buf.readInt();

        List<CellOption> cells = new ArrayList<>();
        for (int i = 0; i < cellCount; i++) {
            UUID cellId = buf.readUUID();
            String displayName = buf.readUtf(64);
            int prisonerCount = buf.readInt();
            int maxPrisoners = buf.readInt();

            cells.add(
                new CellOption(cellId, displayName, prisonerCount, maxPrisoners)
            );
        }

        return new PacketOpenCellSelector(targetEntityUUID, cells);
    }

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

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

        private static void handle(PacketOpenCellSelector pkt) {
            net.minecraft.client.Minecraft mc =
                net.minecraft.client.Minecraft.getInstance();
            if (mc.player == null) return;

            TiedUpMod.LOGGER.info(
                "[PacketOpenCellSelector] Opening cell selector with {} options for target {}",
                pkt.cells.size(),
                pkt.targetEntityUUID.toString().substring(0, 8)
            );

            mc.setScreen(
                new com.tiedup.remake.client.gui.screens.CellSelectorScreen(
                    pkt.targetEntityUUID,
                    pkt.cells
                )
            );
        }
    }

    public UUID getTargetEntityUUID() {
        return targetEntityUUID;
    }

    public List<CellOption> getCells() {
        return cells;
    }
}
