const dbstore = require('nedb')
    , appCache = require('app-cache')
    , utils = require('utility')
    , fs = require('fs')
    ;

    var Collections = {};

class provider {
    constructor(_collection) {
        // this.db  =  new dbstore({ filename: `E:\\work\\client\\winsoft\\readdir\\db\\` + _collection, autoload: true });
        // return ;
        if (!Collections[appCache.cur_user.loginName]) {
            Collections[appCache.cur_user.loginName] = {};
        }
        var md5 = utils.md5(_collection);
        if (!Collections[appCache.cur_user.loginName][md5]) {
            Collections[appCache.cur_user.loginName][md5] = new dbstore({ filename: `${process.env.LOCALAPPDATA}/winsoft/db/${appCache.cur_user.loginName}/` + _collection, autoload: true });
        }
        //  在Collections中取出当前用户存储信息
        this.db = Collections[appCache.cur_user.loginName][md5];

    }

    insert(data) {
        var self = this;
        if (typeof data === 'string') {
            return Promise.resolve(false);
        }
        if (Array.isArray(data))
            data.forEach(v => { v.ctime = Date.now(); });
        else
            data.ctime = Date.now();
        return Q((resolve, reject) => {
            if (typeof data != 'object') {
                return resolve(false);
            }
            self.db.insert(data, function (err, newDoc) {
                if (err) {
                    reject(err);
                } else {
                    resolve(newDoc);
                }
            });
        });
    }

    find(search, sort = { ctime: 1 }) {
        let self = this;
        let promise = new Promise(function (resolve, reject) {
            //  exec()回调函数
            self.db.find(search || {}).sort(sort).exec(function (err, docs) {
                err ? reject(err) : resolve(docs);
            });
        });
        return promise;
    }

    findOne(search, sort) {
        var self = this;
        return Q((resolve, reject) => {
            if (!sort)
                self.db.findOne(search || {}, function (err, docs) { if (err) reject(err); else resolve(docs); });
            else
                self.db.findOne(search || {}).sort(sort).exec(function (err, docs) {
                    err ? reject(err) : resolve(docs);
                });
        });
    }

    update(selector, set) {
        var self = this;
        return Q((resolve, reject) => {
            if (typeof selector != 'object') {
                return resolve(false);
            }
            self.db.update(selector, { $set: set }, { multi: true, upsert: false }, function (err, numReplaced) {
                if (err) {
                    reject(err);
                } else {
                    resolve(numReplaced);
                }
            });
        });
    }

    delete(selector, multi) {
        var self = this;
        return Q((resolve, reject) => {
            if (typeof selector != 'object') {
                return resolve(false);
            }
            self.db.remove(selector, { multi: true }, function (err, numReplaced) {
                if (err) {
                    reject(err);
                } else {
                    resolve(numReplaced);
                }
            });
        });
    }

    count(selector = {}) {
        var self = this;
        return Q((resolve, reject) => {
            if (typeof selector != 'object') {
                return resolve(false);
            }
            self.db.count(selector, function (err, numReplaced) {
                if (err) {
                    reject(err);
                } else {
                    resolve(numReplaced);
                }
            });
        });
    }

}

function Q(caller) {
    let promise = new Promise(function (resolve, reject) {
        caller(resolve, reject);
    });
    return promise;
}
module.exports = provider;
