;
(function () {

    const event = require('myevents')
        , myStorage = require('local-storage')
        , dbServer = require('db-server')
        , taskStatus = require('taskstatus')
        , downManager = require('downmanager');

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

    return;
    function task_down_Controller($scope, $timeout, fileTool) {
        let vm = $scope;
        vm.showStartBtn = false;
        vm.initPage = false;
        bindDown(vm, $timeout);

        vm.pause = function (file,$timeout) {
            if (file.status === 1 || file.status === 0) {
                file.status = taskStatus.pause;
                return downManager.pause(file._id);
            }
            let then = downManager.startNext(file._id);
            if (then) {
                    file.status = taskStatus.wait;
                    bindDown(vm, $timeout);
                return;
            }
            file.status = taskStatus.wait;
        };

        vm.delTask = (file, index) => {
            downManager.remove(file._id).then(e =>{
                vm.tasklist.splice(index, 1);
            });
            event.emit('updataDownTaskNum');
        };

        vm.clearQueue = () => {
            fileTool.alert("确定取消全部下载任务？").then((ok) => {
                if (!ok) return;
                let load = layer.load(1);
                downManager.clearQueue().then( e =>{
                    vm.tasklist = [];
                    event.emit('updataDownTaskNum');
                    layer.close(load);
                });
            });
        };

        vm.stopQueue = () => {
            downManager.stopQueue().then(e => {
                bindDown(vm, $timeout);
                $timeout(() => {
                    vm.showStartBtn = true;
                })
            });
        };

        vm.startQueue = () => {
            downManager.startQueue().then(e => {
                bindDown(vm, $timeout);
                $timeout(() => {
                    vm.showStartBtn = false;
                })
            });
        };

        //下载 变化 控制函数
        event.on('down_change_ctr', function (options) {
            if (!Array.isArray(vm.tasklist)) return;
            let idx = vm.tasklist.findIndex(e => {
                return (e.path === options.path) || (e._id === options._id);
            });
            //  如果不存在 那么重新刷新一下列表
            if (idx === -1) {
                return bindDown(vm, $timeout);
            }
            // 如果存在 则更新视图
            $timeout(() => {
                if (!vm.tasklist[idx]) return;
                vm.tasklist && Object.assign(vm.tasklist[idx], options);
            })
        });

        //下载 完成 控制函数
        event.on('down_complete_ctr', function (options) {
            if (!Array.isArray(vm.tasklist)) return;

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

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

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

        //下载 任务 控制函数
        event.on('down_taskList', function (tasks) {
            if (!Array.isArray(tasks) || !tasks.length) return;
            $timeout(() => {
                vm.tasklist.push(...tasks);
            });
        });

        $scope.$on('$destroy', () => {
            event.removeAllListeners('down_change_ctr');
            event.removeAllListeners('down_complete_ctr');
            event.removeAllListeners('down_taskList');
        });


        function bindDown(vm, $timeout) {
            let load = layer.load(1);
            dbServer.down.findAll({'ctime':1,'sort':1}).then(e => {
                // console.error(e)
                layer.close(load);
                vm.initPage = true;
                $timeout(() => {
                    vm.tasklist = e;
                })
            })
        }
    }

})();
