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.SelfCare;
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 AutoPrefixCommand {
    public static void register (final CommandDispatcher<FabricClientCommandSource> dispatcher) {
        dispatcher.register(
                literal("autoprefix")
                        .then(
                                argument("prefix", greedyString())
                                        .executes(AutoPrefixCommand::execute)
                        )
        );
    }

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

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

        SelfCare.INSTANCE.targetPrefix = prefix;

        if (prefix.equalsIgnoreCase("off")) {
            source.sendFeedback(Component.literal("Successfully disabled auto prefix"));
        } else {
            SelfCare.INSTANCE.hasPrefix = false;
            source.sendFeedback(Component.literal("Set your prefix to: " + prefix));
        }

        return Command.SINGLE_SUCCESS;
    }
}
