package com.tiedup.remake.network.personality;

import com.tiedup.remake.core.TiedUpMod;
import java.util.function.Supplier;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.loading.FMLEnvironment;
import net.minecraftforge.network.NetworkEvent;

/**
 * Packet sent from server to client to open the Command Wand screen.
 * Contains all personality data needed for the GUI.
 *
 * Simplified: removed discovery, fear, relationship, secondaryTrait fields.
 */
public class PacketOpenCommandWandScreen {

    private final java.util.UUID entityUUID;
    private final String npcName;
    private final String personalityTypeName;
    private final String activeCommandName;
    private final float hunger;
    private final float rest;
    private final float mood;
    private final String followDistanceMode;
    private final String homeType;
    private final boolean autoRestEnabled;
    private final String cellName;
    private final String cellQualityName;
    private final String activeJobLevelName;
    private final int activeJobXp;
    private final int activeJobXpMax;

    /**
     * Constructor without cell info or job experience (defaults to "").
     */
    public PacketOpenCommandWandScreen(
        java.util.UUID entityUUID,
        String npcName,
        String personalityTypeName,
        String activeCommandName,
        float hunger,
        float rest,
        float mood,
        String followDistanceMode,
        String homeType,
        boolean autoRestEnabled
    ) {
        this(
            entityUUID,
            npcName,
            personalityTypeName,
            activeCommandName,
            hunger,
            rest,
            mood,
            followDistanceMode,
            homeType,
            autoRestEnabled,
            "",
            "",
            "",
            0,
            10
        );
    }

    /**
     * Constructor with cell info but no job experience.
     */
    public PacketOpenCommandWandScreen(
        java.util.UUID entityUUID,
        String npcName,
        String personalityTypeName,
        String activeCommandName,
        float hunger,
        float rest,
        float mood,
        String followDistanceMode,
        String homeType,
        boolean autoRestEnabled,
        String cellName,
        String cellQualityName
    ) {
        this(
            entityUUID,
            npcName,
            personalityTypeName,
            activeCommandName,
            hunger,
            rest,
            mood,
            followDistanceMode,
            homeType,
            autoRestEnabled,
            cellName,
            cellQualityName,
            "",
            0,
            10
        );
    }

    /**
     * Full constructor with cell info and job experience.
     */
    public PacketOpenCommandWandScreen(
        java.util.UUID entityUUID,
        String npcName,
        String personalityTypeName,
        String activeCommandName,
        float hunger,
        float rest,
        float mood,
        String followDistanceMode,
        String homeType,
        boolean autoRestEnabled,
        String cellName,
        String cellQualityName,
        String activeJobLevelName,
        int activeJobXp,
        int activeJobXpMax
    ) {
        this.entityUUID = entityUUID;
        this.npcName = npcName;
        this.personalityTypeName = personalityTypeName;
        this.activeCommandName = activeCommandName;
        this.hunger = hunger;
        this.rest = rest;
        this.mood = mood;
        this.followDistanceMode = followDistanceMode;
        this.homeType = homeType;
        this.autoRestEnabled = autoRestEnabled;
        this.cellName = cellName;
        this.cellQualityName = cellQualityName;
        this.activeJobLevelName = activeJobLevelName;
        this.activeJobXp = activeJobXp;
        this.activeJobXpMax = activeJobXpMax;
    }

    public void encode(FriendlyByteBuf buf) {
        buf.writeUUID(entityUUID);
        buf.writeUtf(npcName, 64);
        buf.writeUtf(personalityTypeName, 32);
        buf.writeUtf(activeCommandName, 32);
        buf.writeFloat(hunger);
        buf.writeFloat(rest);
        buf.writeFloat(mood);
        buf.writeUtf(followDistanceMode, 16);
        buf.writeUtf(homeType, 16);
        buf.writeBoolean(autoRestEnabled);
        buf.writeUtf(cellName, 64);
        buf.writeUtf(cellQualityName, 16);
        buf.writeUtf(activeJobLevelName, 32);
        buf.writeInt(activeJobXp);
        buf.writeInt(activeJobXpMax);
    }

    public static PacketOpenCommandWandScreen decode(FriendlyByteBuf buf) {
        return new PacketOpenCommandWandScreen(
            buf.readUUID(),
            buf.readUtf(64), // npcName
            buf.readUtf(32), // personalityTypeName
            buf.readUtf(32), // activeCommandName
            buf.readFloat(), // hunger
            buf.readFloat(), // rest
            buf.readFloat(), // mood
            buf.readUtf(16), // followDistanceMode
            buf.readUtf(16), // homeType
            buf.readBoolean(), // autoRestEnabled
            buf.readUtf(64), // cellName
            buf.readUtf(16), // cellQualityName
            buf.readUtf(32), // activeJobLevelName
            buf.readInt(), // activeJobXp
            buf.readInt() // activeJobXpMax
        );
    }

    public void handle(Supplier<NetworkEvent.Context> ctx) {
        ctx
            .get()
            .enqueueWork(() -> {
                if (FMLEnvironment.dist == Dist.CLIENT) {
                    handleClient();
                }
            });
        ctx.get().setPacketHandled(true);
    }

    @OnlyIn(Dist.CLIENT)
    private void handleClient() {
        net.minecraft.client.Minecraft mc =
            net.minecraft.client.Minecraft.getInstance();

        TiedUpMod.LOGGER.debug(
            "[PacketOpenCommandWandScreen] Opening screen for {} (entity {})",
            npcName,
            entityUUID
        );

        mc.setScreen(
            new com.tiedup.remake.client.gui.screens.CommandWandScreen(
                entityUUID,
                npcName,
                personalityTypeName,
                activeCommandName,
                hunger,
                rest,
                mood,
                followDistanceMode,
                homeType,
                autoRestEnabled,
                cellName,
                cellQualityName,
                activeJobLevelName,
                activeJobXp,
                activeJobXpMax
            )
        );
    }

    // --- Getters for GUI use ---

    public java.util.UUID getEntityUUID() {
        return entityUUID;
    }

    public String getNpcName() {
        return npcName;
    }

    public String getPersonalityTypeName() {
        return personalityTypeName;
    }

    public String getActiveCommandName() {
        return activeCommandName;
    }

    public float getHunger() {
        return hunger;
    }

    public float getRest() {
        return rest;
    }

    public float getMood() {
        return mood;
    }

    public String getFollowDistanceMode() {
        return followDistanceMode;
    }

    public String getHomeType() {
        return homeType;
    }

    public boolean isAutoRestEnabled() {
        return autoRestEnabled;
    }

    public String getCellName() {
        return cellName;
    }

    public String getCellQualityName() {
        return cellQualityName;
    }

    public String getActiveJobLevelName() {
        return activeJobLevelName;
    }

    public int getActiveJobXp() {
        return activeJobXp;
    }

    public int getActiveJobXpMax() {
        return activeJobXpMax;
    }
}
