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.CommandCore;
import land.chipmunk.chipmunkmod.modules.custom_chat.CustomChat;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.minecraft.network.chat.Component;
import java.io.IOException;

import static land.chipmunk.chipmunkmod.command.CommandManager.literal;

public class ReloadConfigCommand {
    public static void register (final CommandDispatcher<FabricClientCommandSource> dispatcher) {
        dispatcher.register(
                literal("reloadconfig")
                        .executes(ReloadConfigCommand::reload)
        );
    }

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

        try {
            ChipmunkMod.CONFIG = ChipmunkMod.loadConfig();

            CommandCore.INSTANCE.reloadFromConfig();
            CustomChat.INSTANCE.reloadFromConfig();

            source.sendFeedback(Component.literal("Successfully reloaded the config"));
        } catch (final IOException e) {
            source.sendError(Component.literal("Could not load config, check the logs for stacktrace"));
            ChipmunkMod.LOGGER.error("Could not load the config!", e);
        }

        return Command.SINGLE_SUCCESS;
    }
}
