'use strict';

angular.module('users.services', [
    'system'
])
    .factory('UserService', [
        '$http',
        '$q',
        'System',
        'UserFactory',
        function($http, $q, System, UserFactory) {

            var URL = {
                ALL: '/qusion/api/user/query',
                GET: '/qusion/api/user/get',
                GETEDIT: '/qusion/api/user/getedit',
                CREATE: '/qusion/api/user/create',
                UPDATE: '/qusion/api/user/edit',
                SETTINGS: '/qusion/api/user/settings',
                GET_ROLES: '/qusion/api/user/GetRoles',
                DELETE: '/qusion/api/user/delete',
                STATUS: '/qusion/api/user/status',
                CHANGE_PASS: '/qusion/api/user/changePassword',
                GET_CLASSIFICATIONS: '/qusion/api/user/GetClassifications'
            };

            var query = function (params) {
                var deferred = $q.defer();
                $http.get(URL.ALL, {
                    params: params
                })
                    .success(function (data) {
                        var users = [];
                        for (var i = 0; i < data.Items.length; i++) {
                            users.push(UserFactory.create(data.Items[i]));
                        }
                        deferred.resolve({ users: users, total: data.Count });
                    });

                return deferred.promise;
            };

            var get = function(id) {
                var deferred = $q.defer();
                $http.get(URL.GET, {
                    params: { id: id }
                })
                    .success(function(data) {
                        var user = UserFactory.create(data);
                        deferred.resolve(user);
                    });

                return deferred.promise;
            };
            
            var getUserClassifications = function () {
                var deferred = $q.defer();
                $http.get(URL.GET_CLASSIFICATIONS, {})
                    .success(function (data) {
                        deferred.resolve(data);
                    });

                return deferred.promise;
            };
            
            var setLockStatus = function (id) {
                var deferred = $q.defer();
                $http.get(URL.STATUS, {
                    params: { id: id }
                })
                    .success(function (data) {
                        deferred.resolve(data);
                    });

                return deferred.promise;
            };

            var changePassword = function(password) {
                var deferred = $q.defer();
                $http.post(URL.CHANGE_PASS, password)
                    .success(function(data) {
                        deferred.resolve(data);
                    });

                return deferred.promise;
            };

            var getEdit = function(id) {
                var deferred = $q.defer();
                $http.get(URL.GETEDIT, {
                    params: { id: id }
                })
                    .success(function(data) {
                        var user = UserFactory.create(data);
                        deferred.resolve(user);
                    });

                return deferred.promise;
            };

            var create = function(user) {
                var deferred = $q.defer();
                $http.post(URL.CREATE, user)
                    .success(function(data) {
                        user = UserFactory.create(data);
                        deferred.resolve(user);
                    });

                return deferred.promise;

            };

            var update = function(user) {
                var deferred = $q.defer();
                $http.post(URL.UPDATE, user)
                    .success(function(data) {
                        deferred.resolve(data);
                    });

                return deferred.promise;
            };

            var getRoles = function() {
                var deferred = $q.defer();
                $http.get(URL.GET_ROLES)
                    .success(function (data) {
                        deferred.resolve(data);
                    });

                return deferred.promise;
            };

            var remove = function (params) {
                var deferred = $q.defer();
                $http.get(URL.DELETE, {
                    params: params
                })
                    .success(function (data) {

                        deferred.resolve(data);
                    });

                return deferred.promise;
            };
            
            var getSettings = function () {
                var deferred = $q.defer();
                $http.get(URL.SETTINGS, {}).success(function (data) {
                    deferred.resolve(data);
                });

                return deferred.promise;
            };

            var saveSettings = function (settings) {
                var deferred = $q.defer();
                $http.post(URL.SETTINGS, settings).success(function (data) {
                    deferred.resolve(data);
                });

                return deferred.promise;
            };

            return {
                query: query,
                get: get,
                create: create,
                update: update,
                remove: remove,
                getEdit: getEdit,
                getSettings: getSettings,
                saveSettings: saveSettings,
                getRoles: getRoles,
                setLockStatus: setLockStatus,
                changePassword: changePassword,
                getUserClassifications: getUserClassifications
            };
        }
    ]);