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.modules.custom_chat.CustomChat;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.minecraft.network.chat.Component;

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 CustomChatCommand {
    private static final GsonComponentSerializer GSON = GsonComponentSerializer.gson();

    public static void register (final CommandDispatcher<FabricClientCommandSource> dispatcher) {
        dispatcher.register(
                literal("customchat")
                        .then(
                                literal("enabled")
                                        .then(
                                                argument("boolean", bool())
                                                        .executes(CustomChatCommand::enabled)
                                        )
                        )
                        .then(
                                literal("onlyUseWhenNecessary")
                                        .then(
                                                argument("boolean", bool())
                                                        .executes(CustomChatCommand::onlyUseWhenNecessary)
                                        )
                        )
                        .then(
                                literal("format")
                                        .then(
                                                argument("format", greedyString())
                                                        .executes(CustomChatCommand::setFormat)
                                        )
                        )
        );
    }

    public static int enabled (final CommandContext<FabricClientCommandSource> context) {
        final FabricClientCommandSource source = context.getSource();
        final boolean bool = getBool(context, "boolean");
        CustomChat.INSTANCE.enabled = bool;
        source.sendFeedback(Component.literal("Custom chat is now " + (bool ? "on" : "off")));

        return Command.SINGLE_SUCCESS;
    }

    public static int onlyUseWhenNecessary (final CommandContext<FabricClientCommandSource> context) {
        final FabricClientCommandSource source = context.getSource();
        final boolean bool = getBool(context, "boolean");
        CustomChat.INSTANCE.onlyUseWhenNecessary = bool;

        if (bool) source.sendFeedback(Component.literal("Custom chat now only gets used when necessary"));
        else source.sendFeedback(Component.literal("Custom chat now always get used"));

        return Command.SINGLE_SUCCESS;
    }

    public static int setFormat (final CommandContext<FabricClientCommandSource> context) {
        final FabricClientCommandSource source = context.getSource();
        final String format = getString(context, "format");
        CustomChat.INSTANCE.format = GSON.deserializeOr(format, ChipmunkMod.CONFIG.customChat.format);
        source.sendFeedback(Component.literal("Set the custom chat format to: " + format));

        return Command.SINGLE_SUCCESS;
    }
}
