package com.feed_the_beast.ftbcurseappbot.persistance;

import com.feed_the_beast.ftbcurseappbot.Main;
import com.feed_the_beast.ftbcurseappbot.persistance.data.VersionInfo;
import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoDatabase;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import org.jongo.Jongo;

import java.util.Arrays;

@Slf4j
public class MongoConnection {
    @Getter
    private static MongoClient client;
    @Getter
    private static MongoDatabase database;
    @Getter
    private static Jongo jongo;
    public static void start() {
        CommentedConfigurationNode config = Main.getConfig().getNode("mongo");
        if(config.getNode("enabled").getBoolean()) {
            log.info("starting up mongo client");
            String username = config.getNode("username").getString();
            String pass = config.getNode("password").getString();
            String url = config.getNode("url").getString();
            String db  = config.getNode("database").getString();

            int port = config.getNode("port").getInt(27017);
            if(username != null && !username.isEmpty() && pass != null && !pass.isEmpty()) {
                MongoCredential credential = MongoCredential.createCredential(username, db, pass.toCharArray());
                client = new MongoClient(new ServerAddress(url, port), Arrays.asList(credential));
            } else {
                client = new MongoClient(url, port);
            }
            database = client.getDatabase(db);
            jongo = new Jongo(client.getDB(db));
            log.info("started up mongo client");
            VersionInfo info  = jongo.getCollection("dbinfo").findOne("{service: 'ftbcursebot'}").as(VersionInfo.class);
            if(info == null) {
                jongo.getCollection("dbinfo").save(new VersionInfo());
                log.info("created VersionInfo for database");
            } else {
            log.info("mongo DB version: " + info.getVersion());
            }
        }
    }
}

@@ -479,21 +479,26 @@
 va.util.
-Array
+Collection
 s;%0A%0A@Slf
@@ -681,16 +681,17 @@
  jongo;%0A
+%0A
     publ
@@ -710,16 +710,17 @@
 id start
+ 
 () %7B%0A   
@@ -805,16 +805,17 @@
       if
+ 
 (config.
@@ -1117,17 +1117,16 @@
 ring db 
- 
 = config
@@ -1235,16 +1235,17 @@
       if
+ 
 (usernam
@@ -1503,17 +1503,29 @@
 t), 
-Arrays.as
+Collections.singleton
 List
@@ -1801,17 +1801,16 @@
 fo info 
- 
 = jongo.
@@ -1906,16 +1906,17 @@
       if
+ 
 (info ==
@@ -2082,32 +2082,36 @@
 e %7B%0A            
+    
 log.info(%22mongo 
@@ -2164,22 +2164,686 @@
   %7D%0A        
-%7D
+    VersionInfo current = new VersionInfo();%0A            if (info.getVersion() %3C current.getVersion()) %7B%0A                log.info(%22database needs to be migrated from version %22 + info.getVersion() + %22 to %22 + current.getVersion());%0A                migrate(info, current);%0A            %7D%0A        %7D%0A    %7D%0A%0A    /**%0A     * this is where we would handle when DB migrations are needed past creating the main collections%0A     * @param dbVersion version in the database%0A     * @param expected version the bot is expecting%0A     */%0A    private static void migrate (VersionInfo dbVersion, VersionInfo expected) %7B%0A        jongo.getCollection(%22dbinfo%22).save(expected.getVersion());
 %0A    %7D%0A%7D%0A

add handler to support future use of database migrations