'use strict';


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

            var URL = {
                ALL: '/cms/api/documentlaktasi/Query',
                GET: '/cms/api/documentlaktasi/Get',
                LIBRARIESP: '/cms/api/librarylaktasi/GetByTypeDocuments',
                LIBRARIESTP: '/cms/api/librarylaktasi/GetByLibraryTypePage',
                LIBRARIES: '/cms/api/librarylaktasi/GetByType',
                LIBRARIESTYPE: '/cms/api/librarylaktasi/GetByLibraryType',
                DLIBRARIESTYPE: '/cms/api/documentlaktasi/GetDocumentsByLibraryType',
                SETTINGS: '/qusion/api/documentlaktasi/settings'

            };
            
            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 get = function (id) {
                var deferred = $q.defer();
                $http.get(URL.GET, {
                    params: { id: id }
                }).success(function (data) {
                    var document = DocumentFactory.create(data);
                    deferred.resolve(document);
                });

                return deferred.promise;
            };

            var librariesp = function (page) {
                var deferred = $q.defer();
                $http.get(URL.LIBRARIESP, {
                    params: { 'type': 'Document', page: page}
                })
                   .success(function (data) {
                       var libraries = [];
                       for (var i = 0; i < data.Items.length; i++) {
                           libraries.push(LibraryFactory.create(data.Items[i]));
                       }
                       deferred.resolve({ libraries: libraries, total: data.Count });
                   });

                return deferred.promise;
            };

            var librariestp = function (type,page) {
                var deferred = $q.defer();
                $http.get(URL.LIBRARIESTP, {
                    params: { type: type, page: page }
                })
                   .success(function (data) {
                       var libraries = [];
                       for (var i = 0; i < data.Items.length; i++) {
                           libraries.push(LibraryFactory.create(data.Items[i]));
                       }
                       deferred.resolve({ libraries: libraries, total: data.Count });
                   });

                return deferred.promise;
            };
            
            var libraries = function () {
                var deferred = $q.defer();
                $http.get(URL.LIBRARIESTYPE, {
                    params: { 'type': 'Document' }
                })
                    .success(function (data) {

                        deferred.resolve(data);
                    });

                return deferred.promise;
            };
            
           var getdocumentByType = function (type,page) {
                var deferred = $q.defer();
                $http.get(URL.DLIBRARIESTYPE, {
                    params: { type: type, page:page }
                })
                   .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 getSettings = function () {
                var deferred = $q.defer();
                $http.get(URL.SETTINGS, {}).success(function (data) {
                    deferred.resolve(data);
                });

                return deferred.promise;
            };

            var librariestype = function () {
                var deferred = $q.defer();
                $http.get(URL.LIBRARIESTYPE, {
                    params: { 'type': 'Document' }
                })
                    .success(function (data) {

                        deferred.resolve(data);
                    });

                return deferred.promise;
            };

            return {
                query: query,          
                libraries: libraries,
                get: get,
                librariesp: librariesp,
                librariestype: librariestype,
                getdocumentByType:getdocumentByType,
                getSettings: getSettings
            };
        }
    ]);