;
(function () {

    const
        EventEmitter = require('myevents'),
        appCache = require('app-cache');


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

    function main_Controller($scope, $timeout, fileTool, $mdDialog) {
        var vm = $scope;

        vm.changeLeftMenu = appCache.storage_left_menu;
        vm.ifDisabled = true;
        vm.ifRecycle = false;
        vm.allow_search = true;

        vm.search_key = '';

        vm.isStorage = 'storage';
        let clearTime;
        vm.file_search = () => {
            $timeout.cancel(clearTime);
            clearTime = $timeout(() => {
                if (!vm.search_key) {
                    return;
                }
                vm.search_key = vm.search_key.trim();
                if (vm.search_key.length === 0) {
                    return;
                }
                EventEmitter.emit('crumbClk', 1, vm.search_key);
            }, 300);
        };
        vm.enter_key = (ev) => {
            if (ev.key === "Enter") {
                vm.file_search();
            }
        };
        top_menu($scope, $timeout, fileTool, $mdDialog);
    }

    function top_menu(vm, $timeout, fileTool, $mdDialog) {
        vm.menuBtn = [{
            id: 'downloadFile',
            events: () => {
                let files = getCheckFiles();
                fileTool.downFile($mdDialog)(files);
            },
            icon: 'icon-down',
            text: '下载',
            ifDisable: vm.ifDisabled
        }, {
            id: 'shareFile',
            events: () => {
                let files = getCheckFiles();
                fileTool.shareFile($mdDialog)(files);
            },
            icon: 'icon-share',
            text: '分享',
                ifDisable: vm.ifDisabled
        }, {
            id: 'deleteFile',
            events: () => {
                let files = getCheckFiles();
                fileTool.deleteFile(files);
            },
            icon: 'icon-del',
            text: '删除',
            ifDisable: vm.ifDisabled
        }, {
            id: 'newFile',
            events: () => {
                fileTool.newFile();
            },
            icon: 'icon-floder',
            text: '新建文件夹',
            ifDisable: false
        }, {
            id: 'refresh',
            events: () => { 
                if (!vm.share) {
                   return EventEmitter.emit('fileListRefresh');
                } else {
                    EventEmitter.emit('shareListRefresh');
                }
            },
            icon: 'icon-refresh',
            text: '刷新',
            ifDisable: false
        }];

        function getCheckFiles() {

            let tbody;
            tbody || (tbody = $('#J-tbody'));
            let tr_file_active = Array.prototype.slice.call(tbody.find('.active'));
            let files = [];
            return tr_file_active.map((v) => {
                let data = $(v).data();
                return (data['$scope'].file);
            });
        }
    }
})();
