package gg.norisk.heroes.server.database

import com.mongodb.MongoClientSettings
import com.mongodb.MongoCredential
import com.mongodb.ServerAddress
import com.mongodb.kotlin.client.coroutine.MongoClient
import com.mongodb.kotlin.client.coroutine.MongoDatabase
import org.bson.UuidRepresentation

object MongoManager {
    var isConnected: Boolean = false
    lateinit var client: MongoClient
    lateinit var database: MongoDatabase
    val databaseName
        get() = System.getProperty(
            "ffa_database",
            "ffa"
        )

    fun connect() {
        client = MongoClient.create(
            MongoClientSettings.builder()
                .uuidRepresentation(UuidRepresentation.STANDARD)
                .credential(
                    MongoCredential.createCredential(
                        "root",
                        "admin",
                        "nWcbOgR5R0L9QePfU03cotqPrpGwbu".toCharArray()
                    )
                ).applyToClusterSettings {
                    it.hosts(
                        listOf(
                            ServerAddress(
                                "194.164.200.242",
                                System.getenv("MONGODB_PORT").toIntOrNull() ?: 27017
                            )
                        )
                    )
                }.build()
        )
        database = client.getDatabase(databaseName)
    }
}