package land.chipmunk.chipmunkmod.modules;

import land.chipmunk.chipmunkmod.data.ChomeNSBotCommand;
import land.chipmunk.chipmunkmod.listeners.Listener;
import land.chipmunk.chipmunkmod.listeners.ListenerManager;
import land.chipmunk.chipmunkmod.util.UUIDUtilities;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.network.chat.contents.TranslatableContents;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class ChomeNSBotCommandSuggestions implements Listener {
    public static final String BOT_SELECTOR_ID = "chomens_bot_selector";
    public static final String REQUEST_SUGGESTIONS_ID = "chomens_bot_request_command_suggestion";

    public static ChomeNSBotCommandSuggestions INSTANCE = new ChomeNSBotCommandSuggestions(Minecraft.getInstance());

    private final Minecraft client;

    public boolean receivedSuggestions = false; // can be set through eval

    public String botSelector = null;

    public List<ChomeNSBotCommand> commands = new ArrayList<>();

    public ChomeNSBotCommandSuggestions (final Minecraft client) {
        this.client = client;

        ListenerManager.addListener(this);
    }

    public void init () { }

    @Override
    public void coreMoved () {
        if (!receivedSuggestions) forceRequest();
    }

    public void forceRequest () {
        final LocalPlayer player = client.player;

        if (botSelector == null || player == null) return;

        final String selector = UUIDUtilities.selector(player.getUUID());

        final Component component = Component
                .text(REQUEST_SUGGESTIONS_ID)
                .append(Component.text(selector));

        final String serialized = GsonComponentSerializer.gson().serialize(component);

        // minecraft:tellraw @p[nbt={UUID:[I;6,9,6,9]} {...}
        CommandCore.INSTANCE.run(
                String.format(
                        "%s %s %s",
                        (KaboomCheck.INSTANCE.isKaboom ? "minecraft:tellraw" : "tellraw"),
                        botSelector,
                        serialized
                )
        );
    }

    @Override
    public void overlayMessageReceived (final net.minecraft.network.chat.Component message) {
        if (
                !(message.getContents() instanceof final TranslatableContents translatableTextContent)
                        || !translatableTextContent.getKey().isEmpty()
                        || translatableTextContent.getArgs().length != 2
                        || !(translatableTextContent.getArgs()[0] instanceof final String id)
                        || !id.equals(BOT_SELECTOR_ID)
                        || !(translatableTextContent.getArgs()[1] instanceof final String selector)
        ) return;

        this.botSelector = selector;
    }

    @Override
    public void chatMessageReceived (final net.minecraft.network.chat.Component message) {
        final List<net.minecraft.network.chat.Component> children = message.getSiblings();
        if (children.isEmpty()) return;

        final net.minecraft.network.chat.Component textComponent = children.getFirst();
        if (!textComponent.getString().equals(REQUEST_SUGGESTIONS_ID)) return;

        commands = children.stream()
                .skip(1)
                .map(ChomeNSBotCommand::fromText)
                .filter(Objects::nonNull)
                .toList();

        receivedSuggestions = true;
    }
}
