package org.monospark.actioncontrol;

import java.io.IOException;
import java.nio.file.Path;

import org.monospark.actioncontrol.config.ConfigRegistry;
import org.monospark.actioncontrol.rule.ActionRule;
import org.slf4j.Logger;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.spec.CommandExecutor;
import org.spongepowered.api.command.spec.CommandSpec;
import org.spongepowered.api.config.ConfigDir;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Cancellable;
import org.spongepowered.api.event.Event;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.game.state.GameInitializationEvent;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;

import com.google.inject.Inject;

@Plugin(id = "org.monospark.actioncontrol", name = "ActionControl", version = "1.0", description = "Description")
public final class ActionControl {

    @Inject
    @ConfigDir(sharedRoot = false)
    private Path privateConfigDir;

    @Inject
    private Logger logger;

    @Listener
    public void onServerInit(GameInitializationEvent event) {
        Sponge.getCommandManager().register(this, createReloadCommandSpec(), "actioncontrol");

        for (ActionRule<?> rule : ActionRule.ALL) {
            registerActionRule(rule);
        }

        loadConfig();
    }

    private <E extends Event & Cancellable> void registerActionRule(ActionRule<E> rule) {
        Sponge.getGame().getEventManager().registerListener(this, rule.getEventClass(), rule);
    }

    private CommandSpec createReloadCommandSpec() {
        CommandSpec reloadCommand = CommandSpec.builder()
                .description(Text.of("Reload the ActionControl config"))
                .permission("actioncontrol.reload")
                .executor(new CommandExecutor() {

            @Override
            public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
                boolean successful = loadConfig();
                if (src instanceof Player) {
                    Player player = (Player) src;
                    if (successful) {
                        player.sendMessage(Text.builder("Successfully reloaded the config file")
                                .color(TextColors.GREEN)
                                .build());
                    } else {
                        player.sendMessage(Text.builder("An error occured while loading the config. "
                                + "Check the console for details.")
                                .color(TextColors.RED)
                                .build());
                    }
                }
                return CommandResult.success();
            }
        }).build();

        return CommandSpec.builder().child(reloadCommand, "reload").build();
    }

    private boolean loadConfig() {
        try {
            ConfigRegistry.getRegistry().loadConfigs(privateConfigDir);
            logger.info("Successfully loaded the config file");
            return true;
        } catch (IOException e) {
            logger.error("An error occured while loading the config file.", e);
            logger.info("Fix the config and reload the plugin or restart the server to make it work again.");
            return false;
        }
    }
}

@@ -1629,32 +1629,33 @@
       loadConfig
+s
 ();%0A    %7D%0A%0A    p
@@ -1996,19 +1996,19 @@
 %22Reload 
-the
+all
  ActionC
@@ -2020,16 +2020,22 @@
 l config
+ files
 %22))%0A    
@@ -2309,16 +2309,17 @@
 adConfig
+s
 ();%0A    
@@ -2541,16 +2541,17 @@
 fig file
+s
 %22)%0A     
@@ -2762,26 +2762,29 @@
 loading 
-the
+a
  config
+ file
 . %22%0A    
@@ -3182,16 +3182,17 @@
 adConfig
+s
 () %7B%0A   
@@ -3334,16 +3334,17 @@
 fig file
+s
 %22);%0A    
@@ -3455,19 +3455,17 @@
 loading 
-the
+a
  config 

Fix messages not taking multiple configs into account