package com.tiedup.remake.network.conversation;

import com.tiedup.remake.core.TiedUpMod;
import com.tiedup.remake.dialogue.conversation.ConversationTopic;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraftforge.network.NetworkEvent;

/**
 * Packet sent from server to client to open the conversation GUI.
 * Contains the entity ID, NPC name, and available conversation topics.
 *
 */
public class PacketOpenConversation {

    private final int entityId;
    private final String npcName;
    private final List<String> availableTopics;

    public PacketOpenConversation(
        int entityId,
        String npcName,
        List<ConversationTopic> topics
    ) {
        this.entityId = entityId;
        this.npcName = npcName;
        this.availableTopics = new ArrayList<>();
        for (ConversationTopic topic : topics) {
            this.availableTopics.add(topic.name());
        }
    }

    private PacketOpenConversation(
        int entityId,
        String npcName,
        List<String> topicNames,
        boolean raw
    ) {
        this.entityId = entityId;
        this.npcName = npcName;
        this.availableTopics = topicNames;
    }

    public void encode(FriendlyByteBuf buf) {
        buf.writeInt(entityId);
        buf.writeUtf(npcName, 64);
        buf.writeInt(availableTopics.size());
        for (String topic : availableTopics) {
            buf.writeUtf(topic, 64);
        }
    }

    public static PacketOpenConversation decode(FriendlyByteBuf buf) {
        int entityId = buf.readInt();
        String npcName = buf.readUtf(64);
        int topicCount = buf.readInt();
        List<String> topics = new ArrayList<>(topicCount);
        for (int i = 0; i < topicCount; i++) {
            topics.add(buf.readUtf(64));
        }
        return new PacketOpenConversation(entityId, npcName, topics, true);
    }

    public void handle(Supplier<NetworkEvent.Context> ctx) {
        ctx.get().enqueueWork(() -> handleClient());
        ctx.get().setPacketHandled(true);
    }

    private void handleClient() {
        // DISABLED: Conversation system not in use
        TiedUpMod.LOGGER.info(
            "[PacketOpenConversation] Conversation system disabled - ignoring request"
        );

        /*
        // Convert topic names back to enum values
        List<ConversationTopic> topics = new ArrayList<>();
        for (String topicName : availableTopics) {
            try {
                topics.add(ConversationTopic.valueOf(topicName));
            } catch (IllegalArgumentException e) {
                TiedUpMod.LOGGER.warn(
                    "[PacketOpenConversation] Unknown topic: {}",
                    topicName
                );
            }
        }

        // Open the conversation screen
        // Delegate to client-only helper to avoid class loading on server
        com.tiedup.remake.client.network.ClientPacketHandler.openConversationScreen(entityId, npcName, topics);
        */
    }

    public int getEntityId() {
        return entityId;
    }

    public String getNpcName() {
        return npcName;
    }

    public List<String> getAvailableTopics() {
        return availableTopics;
    }
}
