'use strict';

/** 
 * @controller UserMainController
 * @property scope 
 * @property location
 * @property route
 * @property System
 * @property UserService
 */

angular.module('users.controllers.main', [
    'system'
])
    .controller('UserMainController', [
        '$scope',
        '$location',
        '$route',
        'System',
        'UserService',
        'ClassificationService',
        function ($scope, $location, $route, System, UserService, ClassificationService) {
            $scope.query =  $route.current.params || {};
            $scope.selection = {};
            
            $scope.sidebarView = 'basic';

            $(".tooltip").tooltip();

            /**
            * @method refresh
            * @description refreshes page and loads all initial data
            */
            $scope.refresh = function () {
                UserService.query($scope.query).then(function (data) {
                    $scope.users = data.users;
                    $scope.pager = System.Pager.create({ page: $scope.query.page, total: data.total });
                });
                
                ClassificationService.getByAlias("team")
                    .then(function (data) {
                        $scope.classification = data;
                    });
            };

            /**
            * @method isSearch
            * @description returns value indicates if search is running
            */
            $scope.isSearch = function () {
                return Object.keys($scope.query).length !== 0;
            };
            /**
           * @method search
           * @description searches users 
           */
            $scope.search = function (q, reset) {
                reset ? $scope.query = q : angular.extend($scope.query, q);
                $location.search($scope.query);
                $scope.refresh();
            };

            /**
            * @method reset
            * @description resets search to its initial value
            */
            $scope.reset = function () {
                $location.search(($scope.query = {}));
            };

            /**
            * @action back returns to location /users
            */
            $scope.back = function () {
                $location.url('/users');
            };

            /**
            * @action goToSettings
            * @description returns to location /documents/settings
            */
            $scope.goToSettings = function () {
                $location.url('/users/settings');
            };
            
            $scope.switchSidebar = function (view) {
                $scope.sidebarView = view;
            };

            /**
            * @action edit redirects user to edit form
            * @param user
            */
            $scope.edit = function (user) {
                $location.url('/users/' + user.ID + '/edit');
            };

            /**
            * @action new redirects user to create form
            */
            $scope.new = function () {
                $location.url('/users/create');
            };

            /**
            * @action select selects single user and redirects user to user details
            * @param user user instance to view details for
            */
            $scope.select = function (user) {
                $location.url('/users/' + user.ID);
            };

            /**
            * @method remove
            * @param id
            * @description Removes user by its id
            */
            $scope.remove = function (user, index) {
                var answer = confirm($scope.$resource("utility.areYouShure"));
                if (answer) {
                    UserService.remove({ id: user.ID }).then(function (response) {
                        if (response.Error == false) {
                            $scope.users.splice(index, 1);
                            System.Message.success($scope.$resource("utility.deleted"));
                        }
                    });
                }
            };
            
            /**
            * @method setLockStatus
            * @param id
            * @description Set user parametat Locket to true or false. Inidcates whether the user can access the system.
            */
            $scope.setLockStatus = function(user) {
                UserService.setLockStatus(user.ID).then(function (data) {
                    user.Locked = data.Locked;
                    
                    if (data.Locked == false) {
                        System.Message.success($scope.$resource("users.unlocked"));
                        
                    } else {
                        System.Message.success($scope.$resource("users.locked"));
                    }
                });
            };

            /**
            * @method link returns to location user.ExternalLink
            * @param user user instance to generate link for
            */
            $scope.link = function (user) {
                $location.url(user.ExternalLink);
            };
            
            $scope.$watch("pager.page", function () {
                if (!$scope.pager || !$scope.pager.page) return;
                $scope.query.page = $scope.pager.page;
                $scope.search($scope.query, false);
            });

            $scope.refresh();

        }
    ]);