(function () {
    const
        transfer = require('transfer')
        , myStorage = require('local-storage')
        , downManager = require('downmanager')
        , readdir = require('readdir')
        , dbServer = require('db-server')
        , taskStatus = require('taskStatus')
        , uploadManager = require('uploadmanager')
        , nedbCollection = require('nedbCollection');

    transfer['window_load.task'] = () => {

        dbServer.down.find({ 'status': { $in: [taskStatus.doing] } }).then(d => {
            if (!d || !d.length) {
                return downManager.startNext();
            }
            d.forEach((e) => {
                downManager.startNext(e._id);
            })
        });

        dbServer.upload.find({ 'status': { $in: [taskStatus.doing, taskStatus.read] } }).then(d => {
            if (!d || !d.length) {
                return uploadManager.startNext();
            }
            d.forEach((e) => {
                uploadManager.startNext(e._id);
            })
        }).then(e => {
            readdir();
        });

    }
})();
