;
(function () {
    'use strict';
    const event = require('myevents'),
        appCache = require('app-cache'),
        uploadtask = require('uploadtask'),
        apiService = require('app-service'),
        sysConfig = require('sys-config'),
        app_cache = require('app-cache'),
        myStorage = require('local-storage'),
        dbServer = require('db-server'),
        downTask = require('downtask');

    angular
        .module('app.widgets')
        .directive('widgetsHeader', header);

    header.$inject = ['header_menu', '$mdDialog', '$timeout', '$state'];

    function header(header_menu, $mdDialog, $timeout, $state) {
        var directive = {
            link: link,
            templateUrl: `/web_content/app/widgets/header/header.html`,
            restrict: 'EA',
            replace: true
        };

        return directive;

        function link(scope, element, attrs) {
            let local_config = app_cache.local_config();
            let vm = scope,
                fun = {
                    is_show_task: true,
                    cur_id: 'storage_index',
                    cur_user: appCache.cur_user,
                    isFullscreen: false,
                    header_menu: header_menu,
                    'checked': (id) => {
                        vm.cur_id = id;
                        vm.is_show_task = true;
                    },
                    Bmin: () => {
                        nwWindow.minimize();
                    },
                    Bmax: () => {
                        vm.isFullscreen = !vm.isFullscreen;
                        vm.isFullscreen ? nwWindow.maximize() : nwWindow.unmaximize();
                    },
                    signOut: () => {
                        var storage = window.localStorage;
                        storage.logout = false;
                        $state.go('login');
                    },
                    letBack: () => {
                        vm.is_show_task ? $state.go('task') : event.emit('closeTaskPanel');
                        vm.is_show_task = !vm.is_show_task;
                    }
                }
            Object.assign(vm, fun);

            changeSpace();
            //  获取用户 剩余空间刷新头部显示
            function getUserSpace(size) {
                if (isFinite(size) && app_cache.use_space) {
                    app_cache.use_space.totalAmount += size;
                    $timeout(() => {
                        changeSpace();
                    }, 10);
                    return;
                }
                apiService.use_space().then((d) => {
                    if (!d || !d.data) return;
                    app_cache.use_space = d.data;
                    $timeout(() => {
                        changeSpace();
                    }, 1);
                });
            }

            vm.Bclose = function () {
                let local_config = appCache.local_config();

                if (local_config.close.default) {
                    if (local_config.close.type == 'min') {
                        nwWindow.hide(); return
                    }
                    let gui = require('nw.gui');
                    gui.App.quit();
                    return
                }
                $mdDialog.show({
                    controller: ctr,
                    templateUrl: './app/widgets/header/close.html',
                    clickOutsideToClose: false,
                    fullscreen: false,
                }).then(function (isHide) {
                    if (isHide) {
                        nwWindow.hide();
                    }
                }, function () {
                    var gui = require('nw.gui');
                    gui.App.quit();
                });
                ctr.$inject = ['$scope'];
                return;

                function ctr($scope) {

                    $scope.colse_default = local_config.close.default;
                    $scope.colseType = local_config.close.type;

                    $scope.save = function () {
                        if ($scope.colse_default) {
                            local_config.close = {
                                default: $scope.colse_default,
                                type: $scope.colseType
                            }
                            myStorage.set('local_config', local_config);
                        }
                        $scope.colseType == 'min' ? $mdDialog.hide(true) : $mdDialog.cancel();
                    }
                    $scope.closeApp = function () {
                        $mdDialog.hide(false);
                    }
                }
            }

            vm.feedBack = function () {
                $mdDialog.show({
                    controller: ($scope, $timeout) => {

                        $scope.submit = function (bol) {
                            if (!bol) {
                                return $mdDialog.cancel();
                            }

                            $mdDialog.hide();
                        }
                    },
                    templateUrl: "./app/widgets/header/feedBack.html",
                    clickOutsideToClose: false,
                    fullscreen: false,
                });
            }

            // vm.$on("toFooter", function (e, len, bol = false) {
            //     if (bol && len > 0) {
            //         scope.$broadcast("footer", len, bol);
            //         return;
            //     }
            //     //传入的checkitem数目为0时
            //     if (bol && len == 0) {
            //         len = vm.len;
            //         scope.$broadcast("footer", len);
            //         return;
            //     }
            //     vm.len = len;
            //     scope.$broadcast("footer", len);

            // });

            event.on('task_num_change', function () {
                task_len();
            });
            event.on('getUserSpace', function (size) {
                getUserSpace(size);
            });
            scope.$watch('is_show_task', task_len);

            function task_len() {
                if (vm.is_show_task)
                    dbServer.upload.count().then(lenUpload => {
                        return dbServer.down.count().then(lenDown => {
                            return lenDown + lenUpload;
                        });
                    }).then((len) => {
                        $timeout(() => {
                            vm.task_len = len;
                        })
                    });
            }

            function changeSpace() {
                let use_space = app_cache.use_space;
                let storage_space = (use_space.totalAmount / sysConfig.storage_space) * 100;
                vm.space = {
                    percent: storage_space > 100 ? 100 : storage_space,
                    use_size: use_space.totalAmount > sysConfig.storage_space? sysConfig.storage_space : use_space.totalAmount ,
                    max_size: sysConfig.storage_space
                };
            }
        }


    }
})();
