package com.yihu.hos.common.mongo;

import com.mongodb.client.MongoCollection;
import org.bson.Document;

import java.util.List;

/**
 * 提供对MongoDB的封装，减化对Mongo使用.
 * 主要就是减少层级关系，过滤掉资源释放等处理。
 * 注意：
 * 部分接口为了保持高效的情况，建议还是使用原生驱动。
 * 可以用getCollection取到原生MongoCollection<Document>
 * usage:
 * 1.使用MongoDBKit.addConfig()
 * 2.使用MongoDBKit.start();
 * 3.使用MongoDB做查询
 * 4.使用use()切换连接查询
 *
 * @created Airhead 2016/2/17.
 */
public class MongoDB {
    private static MongoDBPro mongoDBPro;

    static void init() {
        mongoDBPro = MongoDBPro.use();
    }

    public static MongoDBPro use(String configName) {
        return MongoDBPro.use(configName);
    }

    public static MongoDBPro db(String databaseName) {
        return mongoDBPro.db(databaseName);
    }

    public static long count(String collectionName) {
        return mongoDBPro.count(collectionName);
    }

    public static long count(String collectionName, String filter) {
        return mongoDBPro.count(collectionName, filter);
    }

    public static long count(String collectionName, String filter, String options) {
        return mongoDBPro.count(collectionName, filter, options);
    }

    public static List<String> find(String collectionName) {
        return mongoDBPro.find(collectionName);
    }

    public static List<String> find(String collectionName, String filter) {
        return mongoDBPro.find(collectionName, filter);
    }

    public static List<String> find(String collectionName, String filter, String projection) {
        return mongoDBPro.find(collectionName, filter, projection);
    }

    public static List<String> find(String collectionName, String filter, String projection, String options) {
        return mongoDBPro.find(collectionName, filter, projection, options);
    }

    public static void insertOne(String collectionName, String document) {
        mongoDBPro.insertOne(collectionName, document);
    }

    public static void insertMany(String collectionName, List<String> documents) {
        mongoDBPro.insertMany(collectionName, documents);
    }

    public static void insertMany(String collectionName, List<String> documents, String options) {
        mongoDBPro.insertMany(collectionName, documents, options);
    }

    public static long deleteOne(String collectionName, String filter) {
        return mongoDBPro.deleteOne(collectionName, filter);
    }

    public static long deleteMany(String collectionName, String filter) {
        return mongoDBPro.deleteMany(collectionName, filter);
    }

    public static long replaceOne(String collectionName, String filter, String replacement) {
        return mongoDBPro.replaceOne(collectionName, filter, replacement);
    }

    public static long replaceOne(String collectionName, String filter, String replacement, String updateOptions) {
        return mongoDBPro.replaceOne(collectionName, filter, replacement, updateOptions);
    }

    public static long updateOne(String collectionName, String filter, String update) {
        return mongoDBPro.replaceOne(collectionName, filter, update);
    }

    public static long updateOne(String collectionName, String filter, String update, String updateOptions) {
        return mongoDBPro.replaceOne(collectionName, filter, update, updateOptions);
    }

    public static long updateMany(String collectionName, String filter, String update) {
        return mongoDBPro.updateMany(collectionName, filter, update);
    }

    public static long updateMany(String collectionName, String filter, String update, String updateOptions) {
        return mongoDBPro.updateMany(collectionName, filter, update, updateOptions);
    }

    public static String findOneAndDelete(String collectionName, String filter) {
        return mongoDBPro.findOneAndDelete(collectionName, filter);
    }

    public static String findOneAndDelete(String collectionName, String filter, String options) {
        return mongoDBPro.findOneAndDelete(collectionName, filter, options);
    }

    public static String findOneAndReplace(String collectionName, String filter, String replacement) {
        return mongoDBPro.findOneAndReplace(collectionName, filter, replacement);
    }

    public static String findOneAndReplace(String collectionName, String filter, String replacement, String options) {
        return mongoDBPro.findOneAndReplace(collectionName, filter, replacement, options);
    }

    public static String findOneAndUpdate(String collectionName, String filter, String update, String options) {
        return mongoDBPro.findOneAndUpdate(collectionName, filter, update, options);
    }

    public static String findOneAndUpdate(String collectionName, String filter, String update) {
        return mongoDBPro.findOneAndUpdate(collectionName, filter, update);
    }

    public static void drop(String collectionName) {
        mongoDBPro.drop(collectionName);
    }

    public static String createIndex(String collectionName, String keys) {
        return mongoDBPro.createIndex(collectionName, keys);
    }

    public static String createIndex(String collectionName, String keys, String indexOptions) {
        return mongoDBPro.createIndex(collectionName, keys, indexOptions);
    }

    public static List<String> listIndexes(String collectionName) {
        return mongoDBPro.listIndexes(collectionName);
    }

    public static void dropIndex(String collectionName, String indexName) {
        mongoDBPro.dropIndex(collectionName, indexName);
    }

    public static void dropIndexes(String collectionName) {
        mongoDBPro.dropIndexes(collectionName);
    }

    public static void renameCollection(String collectionName, String newCollectionName) {
        mongoDBPro.renameCollection(collectionName, newCollectionName);
    }

    public static void renameCollection(String collectionName, String newCollectionName, String renameCollectionOptions) {
        mongoDBPro.renameCollection(collectionName, newCollectionName, renameCollectionOptions);
    }

    public static MongoCollection<Document> getCollection(String collectionName) {
        return mongoDBPro.getCollection(collectionName);
    }

    public static List<String> listCollectionNames(){
        return mongoDBPro.listCollectionNames();
    }
}
