infrastructureModule.
controller('MenuManageController', ['$scope', '$rootScope', '$log', '$http', '$location', '$translate',
    '$timeout', '$attrs', 'ngDraggable', 'menuService', 'serviceTypeService',
    function ($scope, $rootScope, $log, $http, $location, $translate, $timeout, $attrs, ngDraggable, menuService, serviceTypeService) {
        //'use strict';
        $log.debug('MenuManageController.ctor()...');
  
        $scope.bag = [{
            label: 'Glasses',
            value: 'glasses',
            children: [{
                label: 'Top Hat',
                value: 'top_hat',
                children: [{
                    label: 'Top Hat 2',
                    value: 'top_hat 2'
                }, {
                    label: 'Top Hat 3',
                    value: 'top_hat 3'
                }]
            }, {
                label: 'Curly Mustache',
                value: 'mustachio'
            }]
        }];

        $scope.awesomeCallback = function (node, tree) {
            // Do something with node or tree
            $log.info('callback');
        };

        $scope.otherAwesomeCallback = function (node, isSelected, tree) {
            // Do soemthing with node or tree based on isSelected
            $log.info('other call back',node);
        }
         
        //scope model
        $scope.isAdd = false;
        $scope.selectedMenu = null;
        $scope.editMenuModel = null;
        $scope.menuData = [];
        $scope.checkedMenusSet = {};
        $scope.set = [];
        $scope.defaultServiceType = {};
        $scope.currentServiceTypeId = null; 
         
        var getMenusByServiceTypeId = function (serviceTypeId) {
            menuService.getMenus(serviceTypeId).success(function(menuData) {
                $scope.menuData = menuData;
            });
        };

        //根据serviceTypeId重新获取菜单
        $scope.updateMenus = function (selectedServiceType) {
            $scope.currentServiceTypeId = selectedServiceType.id;
            $scope.selectedMenu = null;

            menuService.getMenus($scope.currentServiceTypeId).success(function (menuData) {
                if (menuData.length === 0) {
                    $scope.showTree = false;
                    $scope.menuData = [];
                    return;
                }
                $scope.menuData = menuData;
                $scope.editMenuModel = null;
                $scope.showTree = true;
            });
        };

        serviceTypeService.getServiceTypeList().success(function (serviceTypeList) {
            $scope.serviceTypeList = serviceTypeList;
            $scope.selectedServiceType = _.find($scope.serviceTypeList, function (num) { return num.id === '1'; });
            $scope.currentServiceTypeId = $scope.selectedServiceType.id;
            getMenusByServiceTypeId($scope.currentServiceTypeId);
            $scope.showTree = true;
        });
         
        //scope function
        $scope.addMenu = function () {
            $scope.isAdd = true;
            $scope.editMenuModel = {};
        };

        $scope.toggle = function (scope) {
            scope.toggle();
        };

        $scope.toggleSub = function (menu) {
            if (menu.isChecked) {
                $scope.checkedMenusSet[menu.id] = menu;
            }
            else {
                delete $scope.checkedMenusSet[menu.id];
                //if the menu is child menu, and parent menu is checked, need to uncheck parent menu

                if (!menu.parentID) {
                    //return;
                }

                if ($.inArray(menu.parentID + "", $scope.set) !== - 1) {
                    delete $scope.checkedMenusSet[menu.parentID];
                    for (var i = 0, len = $scope.menuData.length; i < len; i++) {
                        var parentMenu = $scope.menuData[i];
                        if (parentMenu.id === menu.parentID) {
                            parentMenu.isChecked = false;
                            break;
                        }
                    }
                }
            }
            $scope.set = _.keys($scope.checkedMenusSet);
            if (!menu.subMenus) {
                return;
            }
            menu.subMenus.forEach(function (d) {
                d.isChecked = menu.isChecked;
                if (menu.isChecked) {
                    $scope.checkedMenusSet[d.id] = d;
                }
                else {
                    delete $scope.checkedMenusSet[d.id];
                }
            });

            $scope.set = _.keys($scope.checkedMenusSet);
        };

        $scope.save = function () {
            if (!$scope.editMenuModel) {
                return;
            }
            if (!$scope.isAdd) {
                $scope.editMenuModel.serviceTypeID = $scope.currentServiceTypeId;
                menuService.updateMenu($scope.editMenuModel).success(function () {
                    exchangeModel($scope.editMenuModel, $scope.selectedMenu);
                    $scope.editMenuModel = null;
                    $scope.selectedMenu = null;
                    $log.debug('Update menu successfully');
                });
            }
            else {
                if ($scope.selectedMenu != null) {
                    $scope.editMenuModel.parentID = $scope.selectedMenu.id;
                }
                $scope.editMenuModel.serviceTypeID = $scope.currentServiceTypeId; 
                menuService.addMenu($scope.editMenuModel).success(function (id) {
                    $scope.editMenuModel.id = id;
                    if ($scope.selectedMenu != null) {
                        $scope.selectedMenu.subMenus = $scope.selectedMenu.subMenus || [];
                        //when parent menu check box is checked, the new child should be checked as well.
                        if ($scope.selectedMenu.isChecked) {
                            $scope.editMenuModel.isChecked = true;
                            $scope.checkedMenusSet[$scope.editMenuModel.ID]= $scope.editMenuModel;
                            $scope.set = _.keys($scope.checkedMenusSet);
                        }
                        $scope.selectedMenu.subMenus.push($scope.editMenuModel);
                        $scope.showTree = true;
                    }
                    else {
                        $scope.editMenuModel.parentID = null;
                        $scope.editMenuModel.subMenus = [];
                        $scope.menuData.push($scope.editMenuModel);
                        $scope.showTree = true;
                    }
                    $scope.selectedMenu = null;
                    $scope.editMenuModel = null;
                    $log.debug('Add new menu successfully');
                });
            }
        };

        $scope.cancel = function () {
            $scope.selectedMenu = null;
            $scope.editMenuModel = null;
        };

        //$scope.selectMenu = function (node, isSelected, tree) {
        //    menu = node.value;
        //    $scope.isAdd = false;
        //    $scope.selectedMenu = menu;
        //    $scope.editMenuModel = {};
        //    exchangeModel(menu, $scope.editMenuModel);
        //};

        $scope.selectMenu = function (menu) { 
            $scope.isAdd = false;
            $scope.selectedMenu = menu;
            $scope.editMenuModel = { };
            exchangeModel(menu, $scope.editMenuModel);
        };

        function exchangeModel(from, to) {
            to.name = from.name; 
            to.orderIndex = from.orderIndex;
            to.parentID = from.parentID;
            to.navigationUrl = from.navigationUrl;
            to.isActive = from.isActive;
            to.isVisible = from.isVisible;
            to.moduleID = from.moduleID;
            to.iconClassName = from.iconClassName;
            to.id = from.id;
            to.createTime = from.createTime;
            to.updateTime = from.updateTime;
        };

        $scope.deleteMenus = function () {
            menuService.deleteMenus($scope.set)
                .success(function () {
                    $scope.set = [];
                    menuService.getMenus($scope.currentServiceTypeId).success(function (menuData) {
                        $scope.menuData = menuData;
                    });
                });
            //.fail(function () {

            //});
        }
    }
]);