;
(function () {
    const
        appCache = require('app-cache')
        , nedbCollection = require('nedbCollection')
        , downTask = require('downtask')
        , uploadTask = require('uploadtask')
        , dbServer = require('db-server')
        , event = require('myevents')
        ;

    let nedb = new nedbCollection('/loacl.db');
    angular.module('app.controllers')
        .controller('task_index_Controller', taskIndex_Controller);
    taskIndex_Controller.$inject = ['$scope', '$timeout', '$state', '$document', '$location', '$window'];
    return;

    function taskIndex_Controller($scope, $timeout, $state, $document, $location, $window) {

        var vm = $scope;

        vm.$parent.$parent.isTask = true;

        let href = $document.context.URL;

        $state.transitionTo(appCache.task_controller);

        $scope.currentNavItem = appCache.task_panel;

        vm.updataUploadTaskNum = function (lenUpload) {

            if (Number.isFinite(lenUpload)) {
                let len = vm.lenUpload + lenUpload;
                if (len > 10) {
                    $timeout(() => {
                        vm.lenUpload = len;
                    })
                    return;
                }
            }
            return dbServer.upload.count().then(e => {
                $timeout(() => {
                    vm.lenUpload = e;
                })
            })
        };
        vm.updataDownTaskNum = function (lenDown) {
            if (Number.isFinite(lenDown)) {
                let len = vm.lenDown + lenDown;
                if (len > 10) {
                    $timeout(() => {
                        vm.lenDown = len;
                    })
                    return;
                }
            }
            return dbServer.down.count().then(e => {
                $timeout(() => {
                    vm.lenDown = e;
                })
            });
        };
        event.on('closeTaskPanel', () => {
            location.href = href;
        });

        $scope.$on('$destroy', () => {
            vm.$parent.$parent.isTask = false;
            event.removeAllListeners('closeTaskPanel');
            event.removeAllListeners('updataDownTaskNum');
            event.removeAllListeners('updataUploadTaskNum');
        });

        vm.updataDownTaskNum().then(e => {
            event.on('updataDownTaskNum', vm.updataDownTaskNum);
        });
        vm.updataUploadTaskNum().then(e => {
            event.on('updataUploadTaskNum', vm.updataUploadTaskNum);
        });
    }

})();