(function () {
    angular.module('vip-projects')
        .factory('SkillsService', function ($http) {
            return {
                retrieveSkills: function () {
                    return $http.get('/api/skills').then(function (response) {
                        return response.data;
                    }, function (error) {
                        return [];
                    });
                },
                /**
                 *
                 * @returns contains the new list of skills including the ones just saved
                 */
                saveSkills: function (skills) {
                    return $http.post('/api/skills', { skills: skills }).then(function (response) {
                        return response.data;
                    }, function (error) {
                        return skills;//if failed to save, keep the old list
                    });
                },
                //User Story FA18 | VIP-181, mvale080 (added routes for admin control)
                /* 
                   Below are three routes, each for an admin edit power. adminAddSkill bypasses the fuzzy matching check made by the saveSkills route.
                   updateSkill changes the value for a specific skillID, changing the skill string and/or the approval status
                   deleteSkill removes the skill from the database
                */
                adminAddSkill: function(skill){
                    return $http.post('/api/adminskills', skill).then(function(data){
                        return data.data;
                    }, function (error) {
                        return skill;
                    });
                },

                updateSkill: function (skillData, id) {
                    return $http.put('/api/skills/' + id, skillData).then(function (data) {
                        return data.data;
                    });
                },

                deleteSkill: function (id) {
                    return $http.delete('/api/skills/' + id).then(function (data) {
                        return data.data;
                    });
                }
            }
        });
}());
