package land.chipmunk.chipmunkmod.commands;

import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import land.chipmunk.chipmunkmod.modules.CommandCore;
import land.chipmunk.chipmunkmod.util.TextUtilities;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.text.Text;

import java.util.concurrent.CompletableFuture;

import static com.mojang.brigadier.arguments.BoolArgumentType.bool;
import static com.mojang.brigadier.arguments.BoolArgumentType.getBool;
import static com.mojang.brigadier.arguments.StringArgumentType.getString;
import static com.mojang.brigadier.arguments.StringArgumentType.greedyString;
import static land.chipmunk.chipmunkmod.command.CommandManager.argument;
import static land.chipmunk.chipmunkmod.command.CommandManager.literal;

public class CoreCommand {
    public static void register (final CommandDispatcher<FabricClientCommandSource> dispatcher) {
        dispatcher.register(
                literal("core")
                        .then(
                                literal("run")
                                        .then(
                                                argument("command", greedyString())
                                                        .executes(c -> run(c))
                                        )
                        )

                        .then(
                                literal("runTracked")
                                        .then(
                                                argument("command", greedyString())
                                                        .executes(c -> runTracked(c))
                                        )
                        )

                        .then(literal("refill").executes(c -> refill(c)))
                        .then(literal("move").executes(c -> move(c)))

                        .then(
                                literal("runFillCommand")
                                        .then(
                                                argument("enabled", bool())
                                                        .executes(c -> runFillCommand(c))
                                        )
                        )
        );
    }

    public static int run (final CommandContext<FabricClientCommandSource> context) {
        CommandCore.INSTANCE.run(getString(context, "command"));

        return Command.SINGLE_SUCCESS;
    }

    public static int runTracked (final CommandContext<FabricClientCommandSource> context) {
        final FabricClientCommandSource source = context.getSource();

        final String command = getString(context, "command");

        final CompletableFuture<NbtCompound> future = CommandCore.INSTANCE.runTracked(command);
        future.thenApply(tag -> {
            try {
                final String output = tag.getString("LastOutput");
                if (output != null) source.sendFeedback(TextUtilities.fromJson(output));
            } catch (final Exception e) {
                e.printStackTrace();
            }

            return tag;
        });

        return Command.SINGLE_SUCCESS;
    }

    public static int refill (final CommandContext<FabricClientCommandSource> context) {
        CommandCore.INSTANCE.refill();

        return Command.SINGLE_SUCCESS;
    }

    public static int move (final CommandContext<FabricClientCommandSource> context) {
        final FabricClientCommandSource source = context.getSource();

        CommandCore.INSTANCE.move(source.getClient().player.getPos());

        return Command.SINGLE_SUCCESS;
    }

    public static int runFillCommand (final CommandContext<FabricClientCommandSource> context) {
        final FabricClientCommandSource source = context.getSource();

        final boolean bool = getBool(context, "enabled");

        CommandCore.INSTANCE.runFillCommand = bool;

        source.sendFeedback(Text.literal("Running fill commands is now " + (bool ? "enabled" : "disabled")));

        return Command.SINGLE_SUCCESS;
    }
}
