'use strict';

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

            var URL = {
                ALL: '/cms/api/librarylaktasi/all',
                GET: '/qusion/api/librarylaktasi/get',
                GET_RADNICI: '/cms/api/librarylaktasi/GetRadnici',
                BY_TYPE: '/cms/api/librarylaktasi/LibrariesByType',
                BY_LIBRARY_TYPE: '/cms/api/librarylaktasi/GetByLibraryType',
                BY_LIBRARY_TYPE_AKTIVAN: '/cms/api/librarylaktasi/GetByLibraryTypePageAktivan',
                BY_LIBRARY_TYPE_PAGE: '/cms/api/librarylaktasi/GetByLibraryTypePage',
                LIBRARIES_TYPE_ODJEL_PAGE: '/cms/api/librarylaktasi/SelektujPoTipuIOdjelu',
                LIBRARIES_TYPE_ODJEL_NP: '/cms/api/librarylaktasi/SelektujPoTipuIOdjeluNp',
                QUERYTYPE: '/cms/api/librarylaktasi/QueryType',
                GETTYPE: '/cms/api/librarylaktasi/gettype',
                GET_BY_ALIAS: '/cms/api/librarylaktasi/getbyalias',
            };

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

                return deferred.promise;
            };

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

                return deferred.promise;
            };


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

                        deferred.resolve(LibraryFactory.create(data));
                    });

                return deferred.promise;
            };

            var GetByLibraryType = function (librarytype) {
                var deferred = $q.defer();
                $http.get(URL.BY_LIBRARY_TYPE, {
                    params: { librarytype: librarytype}
                })
                    .success(function (data) {
                        var libraries = [];
                        for (var i = 0; i < data.length; i++) {
                            libraries.push(LibraryFactory.create(data[i]));
                        }
                        deferred.resolve(libraries);
                    });

                return deferred.promise;
            };

            var getByLibraryTypePage = function (librarytype, pagel) {
                var deferred = $q.defer();
                $http.get(URL.BY_LIBRARY_TYPE_PAGE, {
                    params: { librarytype: librarytype, page: pagel }
                })
                   .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 getByLibraryTypePageAktivan = function (librarytype,aktivan) {
                var deferred = $q.defer();
                $http.get(URL.BY_LIBRARY_TYPE_AKTIVAN, {
                    params: { librarytype: librarytype,aktivan: aktivan }
                })
                    .success(function (data) {
                        var libraries = [];
                        for (var i = 0; i < data.length; i++) {
                            libraries.push(LibraryFactory.create(data[i]));
                        }
                        deferred.resolve(libraries);
                    });

                return deferred.promise;
            };

            var getByLibraryTypeIOdjel = function (librarytype, odjel, page) {
                var deferred = $q.defer();
                $http.get(URL.LIBRARIES_TYPE_ODJEL_PAGE, {
                    params: { librarytype: librarytype,odjel:odjel,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 getByLibraryTypeIOdjelNp = function (librarytype, odjel) {
                var deferred = $q.defer();
                $http.get(URL.LIBRARIES_TYPE_ODJEL_NP, {
                    params: { librarytype: librarytype, odjel: odjel}
                })
                   .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 get = function (id) {
                var deferred = $q.defer();
                $http.get(URL.GET, {
                    params: { id: id }
                }).success(function (data) {
                    var library = LibraryFactory.create(data);
                    deferred.resolve(library);
                });

                return deferred.promise;
            };

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

                return deferred.promise;
            };

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

                return deferred.promise;
            };

            var gettype = function (id) {
                var deferred = $q.defer();
                $http.get(URL.GETTYPE, {
                    params: { id: id }
                }).success(function (data) {
                    var librarytype = LibraryFactory.create(data);
                    deferred.resolve(librarytype);
                });

                return deferred.promise;
            };


           
            return {
                query: query,
                getByAlias:getByAlias,
                get: get,
                getByType: getByType,
                GetByLibraryType: GetByLibraryType,
                getByLibraryTypePage: getByLibraryTypePage,
                getByLibraryTypePageAktivan:getByLibraryTypePageAktivan,
                getByLibraryTypeIOdjel: getByLibraryTypeIOdjel,
                getByLibraryTypeIOdjelNp: getByLibraryTypeIOdjelNp,
                getRadnici: getRadnici,
                querytype: querytype,
                gettype: gettype
            };
        }
    ]);