'use strict';

/**
 */

angular.module('documents.services', [
    'system'
])
    .factory('DocumentService', [
        '$http',
        '$q',
        'System',
        'DocumentFactory',
        function($http, $q, System, DocumentFactory) {

            var URL = {
                ALL: '/qusion/api/document/Query',
                DELETE: '/qusion/api/document/remove',
                EDIT: '/qusion/document/edit',
                LIBRARIES: '/qusion/api/library/GetByType',
                DELETE_LIB: '/qusion/api/document/removelibrary',
                SETTINGS: '/qusion/api/document/settings',
                DELETE_SETTINGS: '/qusion/api/document/removesettings'
            };

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

                return deferred.promise;
            };

            var libraries = function() {
                var deferred = $q.defer();
                $http.get(URL.LIBRARIES, {
                    params: { 'type': 'Document' }
                })
                    .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 editDoc = function(document) {
                var deferred = $q.defer();
                $http.post(URL.EDIT, document)
                    .success(function(data) {
                        deferred.resolve(data);
                    });
                return deferred.promise;
            };

            var removeLibrary = function(params) {
                var deferred = $q.defer();
                $http.get(URL.DELETE_LIB, {
                    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;
            };
            
            var removeSettings = function (params) {
                var deferred = $q.defer();
                $http.get(URL.DELETE_SETTINGS, {
                    params: params
                })
                    .success(function (data) {
                        deferred.resolve(data);
                    });

                return deferred.promise;
            };

            return {
                query: query,
                remove: remove,
                removeLibrary: removeLibrary,
                editDoc: editDoc,
                getSettings: getSettings,
                saveSettings: saveSettings,
                removeSettings: removeSettings,
                libraries: libraries
            };
        }
    ]);