;
(function() {
    const
        EventEmitter = require('myevents'),
        apiService = require('app-service'),
        appCache = require('app-cache');

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

    function share_main_Controller($scope, fileTool, $mdDialog, $timeout,files) {
        var vm = $scope;
        let search_type = 'all';
        vm.files = files ;
        vm.sideClick = function(id) {
            search_type = id;
            getFiles();
        }
        vm.itemClick = (share_file) => {
            // console.log(share_file);
            $mdDialog.show({
                controller: 'share_detail_Controller',
                templateUrl: './app/share/detail.html',
                parent: angular.element(document.body),
                clickOutsideToClose: false,
                title: 'Attention',
                fullscreen: false,
                locals: {
                    share_file: share_file
                }
            })
        }

        vm.menuBtn = [ 
        {
            id: 'refresh',
            events: () => {
                getFiles();
            },
            icon: 'icon-refresh',
            text: '刷新'
        }];


        vm.OnItemClick = (num, files) => {

            return false;
        };

        vm.OnCheckAll = (is_all, files) => {

            return false;
        }

        vm.OnContextmenu = (ev, files) => {
            contextmenu(ev, files, vm, fileTool, $mdDialog)
            return false;
        }

        function getFiles() {
            apiService.share_to_me(search_type).then((d) => {
                $timeout(() => {
                    vm.files = d.data;
                }, 10)
            });
        } 
        function contextmenu(ev, files, scope, fileTool, $mdDialog) {
            function storageMenuInit() {
                let menu = new nw.Menu();
                menu.append(new nw.MenuItem({
                    label: ' 详情              ',
                    click: () => {
                         vm.itemClick(files[0]);
                    }
                }));
                menu.append(new nw.MenuItem({
                    label: ' 下载到本地               ',
                    click: () => {
                        fileTool.downshareFile()(files);
                    }
                }));
                menu.popup(ev.clientX, ev.clientY);
            }
            storageMenuInit();
        }
    }

})();
