'use strict';

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

            var URL = {
                ALL: '/qusion/api/library/all',
                CREATE: '/qusion/api/Library/Create',
                UPDATE: '/qusion/api/Library/Edit',
                DELETE: '/qusion/api/Image/RemoveLibrary',
                GET_RADNICI: '/qusion/api/Library/GetRadnici',
                BY_TYPE: '/qusion/api/Library/LibrariesByType',
                BY_LIBRARY_TYPE: '/qusion/api/Library/GetByLibraryType',
                LIBRARIES_TYPE_ODJEL: '/qusion/api/Library/SelektujPoTipuIOdjelu',
                QUERYTYPE: '/qusion/api/Library/QueryType',
                GETTYPE: '/qusion/api/Library/gettype',
                GET: '/qusion/api/Library/get',
                CREATETYPE: '/qusion/api/Library/createtype',
                EDITTYPE: '/qusion/api/Library/EditType',
                DELETETYPE: '/qusion/api/Library/DeleteType'

            };

            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 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 getByLibraryTypeIOdjel = function (librarytype,odjel) {
                var deferred = $q.defer();
                $http.get(URL.LIBRARIES_TYPE_ODJEL, {
                    params: { librarytype: librarytype,odjel:odjel}
                })
                    .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 create = function (library) {
                var deferred = $q.defer();
                $http.post(URL.CREATE, library)
                    .success(function (data) {
                        deferred.resolve(data);
                    });
                return deferred.promise;
            };

            var update = function (library) {
                var deferred = $q.defer();
                $http.post(URL.UPDATE, library)
                    .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 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 (libraryId) {
                var deferred = $q.defer();
                $http.get(URL.GET_RADNICI, {
                    params: { libraryId: libraryId }
                })
                    .success(function (data) {
                        var radnici = [];
                        for (var i = 0; i < data.length; i++) {
                            radnici.push(OdjeljenjeFactory.create(data[i]));
                        }
                        deferred.resolve(libraries);
                    });

                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;
            };

            var createtype = function (librarytype) {
                var deferred = $q.defer();
                $http.post(URL.CREATETYPE, librarytype)
                    .success(function (data) {
                        librarytype = LibraryFactory.create(data);
                        deferred.resolve(data);
                    });
                return deferred.promise;
            };

            var edittype = function (librarytype) {
                var deferred = $q.defer();
                $http.post(URL.EDITTYPE, librarytype)
                    .success(function (data) {

                        deferred.resolve(data);
                    });

                return deferred.promise;
            };

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

                        deferred.resolve(data);
                    });

                return deferred.promise;
            };

            return {
                query: query,
                get: get,
                create: create,
                update: update,
                getByType: getByType,
                GetByLibraryType: GetByLibraryType,
                getByLibraryTypeIOdjel: getByLibraryTypeIOdjel,
                remove: remove,
                getRadnici: getRadnici,
                querytype: querytype,
                gettype: gettype,
                createtype: createtype,
                edittype: edittype,
                removetype: removetype
            };
        }
    ]);