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.ChipmunkMod;
import land.chipmunk.chipmunkmod.command.CommandManager;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.network.chat.Component;

import static com.mojang.brigadier.arguments.StringArgumentType.*;
import static land.chipmunk.chipmunkmod.command.CommandManager.argument;
import static land.chipmunk.chipmunkmod.command.CommandManager.literal;

public class PrefixCommand {
    public static void register (final CommandDispatcher<FabricClientCommandSource> dispatcher) {
        dispatcher.register(
                literal("prefix")
                        .then(
                                argument("newPrefix", greedyString())
                                        .executes(PrefixCommand::execute)
                        )
        );
    }

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

        final String prefix = getString(context, "newPrefix");

        ChipmunkMod.CONFIG.commands.prefix = prefix;
        CommandManager.INSTANCE.prefix = prefix;

        source.sendFeedback(Component.literal("Set the command prefix to: " + prefix));

        return Command.SINGLE_SUCCESS;
    }
}
