package de.svenkubiak.embeddedmongodb;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

import org.slf4j.LoggerFactory;

import com.mongodb.MongoClient;

import de.flapdoodle.embed.mongo.MongodProcess;
import de.flapdoodle.embed.mongo.MongodStarter;
import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
import de.flapdoodle.embed.mongo.config.Net;
import de.flapdoodle.embed.mongo.distribution.Version;

/**
 * @author svenkubiak
 *
 */
public enum EmbeddedMongo {
    DB;
    private static final int MIN_PORT = 1025;
    private static final int MAX_PORT = 50000;
    private static final String ALGORITHM = "SHA1PRNG";
    private transient MongodProcess mongodProcess;
    private boolean active;
    private boolean mongoIPv6;
    private String mongoHost = "localhost";
    private int mongoPort;
    
    private EmbeddedMongo() {
        try {
            this.mongoPort = SecureRandom.getInstance(ALGORITHM).nextInt(MAX_PORT) + MIN_PORT;
        } catch (NoSuchAlgorithmException e) {
            LoggerFactory.getLogger(EmbeddedMongo.class).error("Failed to init EmbeddedMongo", e);
        }
    }
    
    public EmbeddedMongo port(int port) {
        this.mongoPort = port;
        return this;
    }
    
    public EmbeddedMongo host(String host) {
        this.mongoHost = host;
        return this;
    }
    
    public EmbeddedMongo ipv6(boolean ipv6) {
        this.mongoIPv6 = ipv6;
        return this;
    }
    
    public void start() {
        if (!this.active) {
            try {
                this.mongodProcess = MongodStarter.getDefaultInstance().prepare(new MongodConfigBuilder()
                .version(Version.Main.V3_0)
                .net(new Net(this.mongoHost, this.mongoPort, this.mongoIPv6))
                .build()).start();
                
                LoggerFactory.getLogger(EmbeddedMongo.class).info("Successfully started EmbeddedMongoDB @ {}:{}", this.mongoHost, this.mongoPort);
                this.active = true;
            } catch (IOException e) {
                LoggerFactory.getLogger(EmbeddedMongo.class).error("Failed to start EmbeddedMongoDB", e);
            }  
        }
    }
    
    public void stop() {
        if (this.active) {
            this.mongodProcess.stop();   
            this.active = false;
            
            LoggerFactory.getLogger(EmbeddedMongo.class).info("Successfully stopped EmbeddedMongoDB @ {}:{}", this.mongoHost, this.mongoPort);
        }
    }
    
    public MongoClient getMongoClient() {
        return new MongoClient(this.getHost(), this.getPort());
    }

    public int getPort() {
        return this.mongoPort;
    }
    
    public String getHost() {
        return this.mongoHost;
    }
    
    public boolean isIPv6() {
        return this.mongoIPv6;
    }
}
@@ -867,21 +867,17 @@
 goPort;%0A
-    
 %0A
+
     priv
@@ -1024,16 +1024,22 @@
  catch (
+final 
 NoSuchAl
@@ -1167,37 +1167,33 @@
         %7D%0A    %7D%0A
-    
 %0A
+
     public Embed
@@ -1268,37 +1268,33 @@
 urn this;%0A    %7D%0A
-    
 %0A
+
     public Embed
@@ -1372,37 +1372,33 @@
 urn this;%0A    %7D%0A
-    
 %0A
+
     public Embed
@@ -1477,37 +1477,33 @@
 urn this;%0A    %7D%0A
-    
 %0A
+
     public void 
@@ -1705,17 +1705,17 @@
 Main.V3_
-0
+2
 )%0A      
@@ -1825,25 +1825,9 @@
 ();%0A
-                
 %0A
+
     
@@ -2026,16 +2026,22 @@
  catch (
+final 
 IOExcept
@@ -2168,18 +2168,16 @@
        %7D
-  
 %0A       
@@ -2177,37 +2177,33 @@
         %7D%0A    %7D%0A
-    
 %0A
+
     public void 
@@ -2276,19 +2276,16 @@
 .stop();
-   
 %0A       
@@ -2314,21 +2314,9 @@
 se;%0A
-            
 %0A
+
     
@@ -2466,29 +2466,25 @@
     %7D%0A    %7D%0A
-    
 %0A
+
     public M
@@ -2640,37 +2640,33 @@
 ongoPort;%0A    %7D%0A
-    
 %0A
+
     public Strin
@@ -2716,20 +2716,16 @@
 ;%0A    %7D%0A
-    
 %0A    pub

version changed to v3_2 from v3_0