const Q = require('./q'),
    nedbCollection = require('nedbCollection'),
    taskStatus = require('taskStatus');
class upload {
    constructor() {
        this.nedb = new nedbCollection('upload.d');
    }
    nextTask() {
        return this.nedb.findOne({ 'status': taskStatus.wait }, { ctime: 1,sort:1 });
    }
    count() {
        return this.nedb.count();
    }
    remove(_id) {
        return this.nedb.delete({ '_id': _id });
    }
    modifyMd5(_id, md5, url) {
        return this.nedb.update({ '_id': _id }, { 'md5': md5, 'url': url });
    }
    clear() {
        return this.nedb.delete({});
    }
    stop(selector) {
        if (typeof selector === 'string') {
            selector = { _id: selector };
        }
        return this.status(selector, taskStatus.stop);
    }
    updateRange(_id, end_len) {
        return this.nedb.update({ '_id': _id }, { end_len: end_len });
    }
    status(selector, status) {
        if (typeof selector === 'string') {
            selector = { _id: selector };
        }
        return this.nedb.update(selector, { 'status': status });
    }
    find(selector) {
        return this.nedb.find(selector);
    }
    findAll(sort) {
        return this.nedb.find({},sort);
    }
    findOne(selector, sort) {
        return this.nedb.findOne(selector, sort);
    }
    add(datas) {
        if (!datas || (Array.isArray(datas) && !datas.length))
            return Q((resolve, reject) => { resolve(false); });
        return this.nedb.insert(datas);
    }
    bluck(datas , call) {
        if (!datas || (Array.isArray(datas) && !datas.length))
            return Q((resolve, reject) => { resolve(false); });

        let temp = datas.splice(0, 50);
        if (temp.length === 0) {
            return Q((resolve, reject) => { resolve(true); });
        }
        return this.nedb.insert(temp).then(e => {
            if (files.length === 0) {
                return true;
            }
            return this.bluck(datas);
        });
    }
}
module.exports = upload;