package com.loganmagnan.eventcore.managers.mongo;

import com.loganmagnan.eventcore.EventCore;
import com.loganmagnan.eventcore.utils.ColorUtils;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import lombok.Getter;
import org.bson.Document;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import java.util.Collections;

@Getter
public class MongoManager {

    private EventCore plugin = EventCore.getInstance();
    private final MongoManager instance;

    private final FileConfiguration config = this.plugin.getMainConfig().getConfig();

    private MongoClient mongoClient;
    private MongoDatabase mongoDatabase;

    private final boolean uriMode = config.getBoolean("MONGO.URI-MODE");
    private final String host = config.getString("MONGO.NORMAL.HOST");
    private final int port = config.getInt("MONGO.NORMAL.PORT");
    private final String database = config.getString("MONGO.NORMAL.DATABASE");
    private final boolean auth = config.getBoolean("MONGO.NORMAL.AUTH.ENABLED");
    private final String user = config.getString("MONGO.NORMAL.AUTH.USERNAME");
    private final String password = config.getString("MONGO.NORMAL.AUTH.PASSWORD");
    private final String authDatabase = config.getString("MONGO.NORMAL.AUTH.AUTH-DATABASE");
    private final String uriString = config.getString("MONGO.URI.CONNECTION-STRING");
    private final String uriDatabase = config.getString("MONGO.URI.DATABASE-NAME");

    private boolean connected;

    private MongoCollection<Document> players;

    public MongoManager() {
        instance = this;
        try {
            if (this.uriMode) {
                MongoClientURI mongoClientURI = new MongoClientURI(this.uriString);

                MongoClient mongoClient = new MongoClient(mongoClientURI);
                mongoClient.getDatabase(this.uriDatabase);
                mongoDatabase = mongoClient.getDatabase(this.uriDatabase);
            } else {
                if (auth) {
                    final MongoCredential credential = MongoCredential.createCredential(user, authDatabase, password.toCharArray());
                    mongoClient = new MongoClient(new ServerAddress(host, port), Collections.singletonList(credential));
                } else {
                    mongoClient = new MongoClient(host, port);
                }

                mongoDatabase = mongoClient.getDatabase(database);
            }
            connected = true;
            Bukkit.getConsoleSender().sendMessage(ColorUtils.getMessageType("&b[EventCore] &aSuccessfully connected to the database!"));
            this.players = this.mongoDatabase.getCollection("players");
        } catch (Exception exception) {
            connected = false;
            Bukkit.getConsoleSender().sendMessage(ColorUtils.getMessageType("&b[EventCore] &cFailed to connect to the database!"));
            exception.printStackTrace();
            Bukkit.getPluginManager().disablePlugin(this.plugin);
            Bukkit.getConsoleSender().sendMessage(ColorUtils.getMessageType("&b[EventCore] &cDisabling EventCore..."));
        }
    }

    public void disconnect() {
        if (this.mongoClient != null) {
            this.mongoClient.close();
            this.connected = false;
            Bukkit.getConsoleSender().sendMessage(ColorUtils.getMessageType("&b[EventCore] &aSuccessfully disconnected from the database!"));
        }
    }
}