package io.github.aquerr.eaglefactions;

import io.github.aquerr.eaglefactions.commands.CreateCommand;
import io.github.aquerr.eaglefactions.commands.EagleFactionsCommand;
import io.github.aquerr.eaglefactions.commands.HelpCommand;

import com.google.inject.Inject;
import io.github.aquerr.eaglefactions.config.FactionsConfig;
import org.slf4j.Logger;

import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.args.GenericArguments;
import org.spongepowered.api.command.spec.CommandSpec;
import org.spongepowered.api.config.ConfigDir;
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 java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Plugin(id = PluginInfo.Id, name = PluginInfo.Name, version = PluginInfo.Version, description = PluginInfo.Description)
public class EagleFactions
{

    public static Map<List<String>, CommandSpec> _subcommands;

    @Inject
    private Logger _logger;
    public Logger getLogger(){return _logger;}

    private static EagleFactions eagleFactions;
    public static EagleFactions getEagleFactions() {return eagleFactions;}

    @Inject
    @ConfigDir(sharedRoot = false)
    private Path configDir;
    public Path getConfigDir(){return configDir;}



    @Listener
    public void onServerInitialization(GameInitializationEvent event)
    {

        //TODO:Change color of loggs.
       getLogger ().info("EagleFactions is loading...");
       getLogger ().debug ("Preparing wings...");

        getLogger().info("Configs...");
       SetupConfigs();

       getLogger().info("Commands...");
       InitializeCommands();

    }

    private void SetupConfigs()
    {
        // Create Config Directory for EagleFactions
        if (!Files.exists(configDir))
        {
            Path oldConfig = configDir.resolveSibling("io.github.aquerr.eaglefactions");

            if (Files.exists(oldConfig) && Files.isDirectory(oldConfig))
            {
                try
                {
                    Files.move(oldConfig, configDir);
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
            else
            {
                try
                {
                    Files.createDirectories(configDir);
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }

        // Create data Directory for EagleFactions
        if (!Files.exists(configDir.resolve("data")))
        {
            try
            {
                Files.createDirectories(configDir.resolve("data"));
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }

        getLogger().info("Setting up configs...");

        // Create config.conf
        //Config.getConfig().setup();
        // Create messages.conf
        //MessageConfig.getConfig().setup();
        // Create teams.conf
        //TODO:Error occour while loading FactionsConfig.
        //FactionsConfig.getConfig().setup();
        // Create claims.conf
        //ClaimsConfig.getConfig().setup();
        // Create claims.conf
        //ClaimsConfig.getConfig().setup();
        // Start Tax Service
        //Utils.startTaxService();
    }

    private void InitializeCommands()
    {
        getLogger ().info ("Initializing commands...");

        _subcommands = new HashMap<List<String>, CommandSpec>();

        _subcommands.put (Arrays.asList ("help"), CommandSpec.builder ()
                .description (Text.of ("Help"))
                .permission ("eaglefactions.command.help")
                .executor (new HelpCommand ())
                .build());

        _subcommands.put (Arrays.asList ("create"), CommandSpec.builder ()
        .description (Text.of ("Create Faction Command"))
        .permission ("eaglefactions.command.create")
        .arguments (GenericArguments.onlyOne (GenericArguments.string (Text.of ("faction name"))))
        .executor (new CreateCommand ())
        .build ());

        CommandSpec commandEagleFactions = CommandSpec.builder ()
                .description (Text.of ("Factions"))
                .permission ("eaglefactions.command.use")
                .executor (new EagleFactionsCommand ())
                .children (_subcommands)
                .build ();



        Sponge.getCommandManager ().register (this, commandEagleFactions, "factions", "f");

        getLogger ().info ("EagleFactions is ready to use!");
        getLogger ().info ("Thank you for choosing this plugin!");
        getLogger ().info ("Current version " + PluginInfo.Version);
        getLogger ().info ("Have a great time with EagleFactions! :D");
    }

}

@@ -1911,24 +1911,23 @@
   //
- 
 Create 
-C
+c
 onfig 
-D
+d
 irec
@@ -1940,32 +1940,33 @@
 or EagleFactions
+.
 %0A        if (!Fi
@@ -1962,305 +1962,58 @@
     
-if (!Files.exists(configDir))%0A        %7B%0A            Path oldConfig = configDir.resolveSibling(%22io.github.aquerr.eaglefactions%22);%0A%0A            if (Files.exists(oldConfig) && Files.isDirectory(oldConfig))%0A            %7B%0A                try%0A                %7B%0A                    Files.move(oldConfig, 
+try%0A        %7B%0A            Files.createDirectories(
 conf
@@ -2032,32 +2032,18 @@
     
-        %7D%0A      
+%7D%0A
         
   ca
@@ -2030,34 +2030,32 @@
       %7D%0A        
-  
 catch (IOExcepti
@@ -2068,32 +2068,18 @@
         
-        %7B%0A      
+%7B%0A
         
@@ -2074,34 +2074,32 @@
   %7B%0A            
-  
 e.printStackTrac
@@ -2115,53 +2115,131 @@
     
-        %7D%0A            %7D%0A            else%0A    
+%7D%0A%0A        // Create settings & data directory for EagleFactions%0A        if (!Files.exists(configDir.resolve(%22settings%22)))%0A
     
@@ -2248,36 +2248,32 @@
   %7B%0A            
-    
 try%0A            
@@ -2268,34 +2268,26 @@
             
- 
-   %7B%0A    
+%7B%0A
             
@@ -2315,32 +2315,52 @@
 tories(configDir
+.resolve(%22settings%22)
 );%0A             
@@ -2350,36 +2350,32 @@
 ));%0A            
-    
 %7D%0A              
@@ -2364,36 +2364,32 @@
   %7D%0A            
- 
-   
 catch (IOExcepti
@@ -2398,36 +2398,32 @@
  e)%0A            
-    
 %7B%0A              
@@ -2416,36 +2416,32 @@
                 
- 
-   
 e.printStackTrac
@@ -2449,36 +2449,32 @@
 ();%0A            
-    
 %7D%0A            %7D%0A
@@ -2471,75 +2471,10 @@
     
- 
-   %7D%0A        %7D%0A%0A        // Create data Directory for EagleFactions
+%7D%0A
 %0A   

Remove unnecessary check for old configs