;
(function () {
    'use strict';
    const
        lodash = require('lodash'),
        path = require('path'),
        myStorage = require('local-storage'),
        app_cache = require('app-cache'),
        EventEmitter = require('myevents'),
        apiService = require('app-service'),
        fs = require('fs');

    angular.module('app.controllers')
        .controller('share_detail_Controller', share_detail_Controller);

    share_detail_Controller.$inject = ['$scope', '$timeout', 'share_file', '$mdDialog', 'fileTool'];

    function share_detail_Controller($scope, $timeout, share_file, $mdDialog, fileTool) {

        let vm = $scope;
        vm.share_file = angular.copy(share_file);
        vm.breadcrumbs = [{
            id: share_file._id,
            text: "我的云盘"
        }];
        vm.OnItemClick = (num, files) => {
            return false;
        };
        vm.crumb_change = (idx) => {
            if (idx >= (vm.breadcrumbs.length - 1)) {
                return;
            }
            let cur_item = vm.breadcrumbs[idx],
                len = vm.breadcrumbs.length - idx - 1;

            vm.breadcrumbs.splice(idx + 1, len);
            vm.loadFile(cur_item.id);
        };
        vm.item_click = (file) => {
            if (file.isdir === 0) {
                return;
            }
            vm.breadcrumbs.push({
                id: file._id,
                text: file.name
            });
            vm.loadFile(file._id);
        };

        vm.OnCheckAll = (is_all, files) => {
            return false;
        };

        vm.OnContextmenu = (ev, files) => {
            contextmenu(ev, files, vm, fileTool, $mdDialog, share_file)
            return false;
        };
        vm.close = $mdDialog.hide;
        vm.loadFile = (pid) => {
            if (!pid || share_file._id === pid) {
                apiService.share_package_files(share_file._id).then((v) => {
                    $timeout(function () {
                        vm.files = v.data;
                    });
                });
            } else {
                apiService.item_list(pid).then((d) => {
                    $scope.$apply(function () {
                        vm.files = d.data;
                    });
                });
            }
        };
        vm.loadFile();
    }

    function contextmenu(ev, files, scope, fileTool, $mdDialog, share_file) {

        function storageMenuInit() {
            let menu = new nw.Menu();
            menu.append(new nw.MenuItem({
                label: '   保存到我的云盘      ',
                enabled: app_cache.cur_user.id !== share_file.user.id,
                click: () => {
                    fileTool.saveFile(files).then(function () {
                    }, function () {  
                        files.forEach(v => {
                            apiService.analysis_add(share_file, v._id, 'save').then(function (d) { });
                        });
                        EventEmitter.emit('getUserSpace');
                    });
                }
            }));
            menu.append(new nw.MenuItem({
                label: '   下载到本地         ',
                click: () => {
                    files.forEach(v => {
                        apiService.analysis_add(share_file, v._id).then(function (d) { });
                    });
                    fileTool.downFile()(files);
                }
            }));

            menu.append(new nw.MenuItem({ type: 'separator' }));
            menu.append(new nw.MenuItem({
                label: '   属性        ',
                enabled: files.length === 1,
                click: () => {
                    let file = files[0];
                    fileTool.attribute(file);
                }
            }));
            menu.append(new nw.MenuItem({
                label: '   刷新           ',
                click: () => {
                    vm.loadFile();
                }
            }));
            menu.popup(ev.clientX, ev.clientY);
        }
        storageMenuInit();
    }
}());
