;
(function () {

    const event = require('myevents')
        , dbServer = require('db-server')
        , uploadTask = require('uploadtask')
        , taskStatus = require('taskstatus')
        , uploadManager = require('uploadmanager')
    ;

    angular.module('app.controllers')
        .controller('task_upload_Controller', task_upload_Controller);
    task_upload_Controller.$inject = ['$scope', '$timeout', 'fileTool'];
    return;

    function task_upload_Controller($scope, $timeout, fileTool) {
        let vm = $scope;
        vm.showStartBtn = false;

        bindUpload(vm, $timeout);

        event.on('upload_change_ctr', function (options) {
            if (!Array.isArray(vm.tasklist)) return;
            let idx = vm.tasklist.findIndex(e => {
                return (e._id === options._id) || (e.md5 === options.md5);
            });
            if (idx === -1) {
                // return bindUpload(vm, $timeout);
                return;
            }
            $timeout(() => {
                if (!vm.tasklist[idx]) return;
                vm.tasklist && Object.assign(vm.tasklist[idx], options);
            })
        });

        event.on('upload_complete_ctr', function (options) {
            if (!Array.isArray(vm.tasklist)) return;

            let idx = vm.tasklist.findIndex(e => {
                return (e._id === options._id) || (e.md5 === options.md5);
            });

            if (idx === -1) {
                return bindUpload(vm, $timeout);
            }

            $timeout(() => {
                vm.tasklist && vm.tasklist.splice(idx, 1);
            })
        });

        event.on('upload_taskList', function (tasks) {
            if (!Array.isArray(tasks) || !tasks.length) return;

            $timeout(() => {
                vm.tasklist.unshift(...tasks);
            })
        });

        $scope.$on('$destroy', () => {
            event.removeAllListeners('upload_change_ctr');
            event.removeAllListeners('upload_complete_ctr');
        });

        vm.pause = function (file) {
            if (file.status === 1 || file.status === 0) {
                file.status = taskStatus.pause;
                return uploadManager.stop(file._id);
            }
            let then = uploadManager.startNext(file._id);
            if (!then) return
            file.status = taskStatus.wait;
        };

        vm.delTask = (file) => {
            let idx = vm.tasklist.findIndex(e => {
                return (e._id === file._id) || (e.md5 === file._id);
            });
            uploadManager.delete(file._id);
            vm.tasklist.splice(idx, 1);
            event.emit('updataUploadTaskNum');
        };

        vm.clearQueue = () => {
            fileTool.alert("确定取消全部上传任务？").then((ok) => {
                if (!ok) return;

                let load = layer.load(1);
                uploadManager.clearQueue().then(e => {
                    bindUpload(vm, $timeout);
                    event.emit('updataUploadTaskNum',null);
                    layer.close(load);
                });
            });
        }
        vm.stopQueue = () => {
            let load = layer.load(1);
            uploadManager.stopQueue().then(e => {
                bindUpload(vm, $timeout);
                layer.close(load);
                $timeout(() => {
                    vm.showStartBtn = true;
                })
            });
        }
        vm.startQueue = () => {
            let load = layer.load(1);
            uploadManager.startQueue().then(e => {
                bindUpload(vm, $timeout);
                layer.close(load);
                $timeout(() => {
                    vm.showStartBtn = false;
                })
            });
        }
    }

    function bindUpload(sc, $timeout) {
        dbServer.upload.findAll({'ctime':1,'sort':1}).then(e => {
            $timeout(() => {
                sc.tasklist = e;
            })
        })
    }
})();