'use strict';

/**
 */

var ImageService = angular.module('images.services', [
    'system'
])
    .factory('ImageService', [
        '$http',
        '$q',
        'System',
        'ImageFactory',
        function ($http, $q, System, ImageFactory) {

            var URL = {
                ALL: '/qusion/api/image/Query',
                eALL: '/qusion/api/image/eQuery',
                STATUSLISTA: '/qusion/api/library/StatusLista',
                DELETE: '/qusion/api/image/remove',
                ORGANIZE: '/qusion/api/image/organize',
                EDIT: '/qusion/image/edit',
                LIBRARIES: '/qusion/api/library/GetByType',
                DELETE_LIB: '/qusion/api/image/removelibrary',
                UPDATE_LIB: '/qusion/api/library/edit',
                SETTINGS: '/qusion/api/image/settings',
                DELETE_SETTINGS: '/qusion/api/image/removesettings',
                GET: '/qusion/api/image/Get'
            };

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

                return deferred.promise;
            };

            var equery = function (params) {
                var deferred = $q.defer();
                $http.get(URL.eALL, {
                    params: params
                })
                    .success(function (data) {
                        var images = [];
                        for (var i = 0; i < data.Images.length; i++) {
                            images.push(ImageFactory.create(data.Images[i]));
                        }
                        deferred.resolve(data);
                    });

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

            var elibraries = function () {
                var deferred = $q.defer();
                $http.get(URL.LIBRARIES, {
                    params: { 'type': 'eImage' }
                })
                    .success(function (data) {
                        deferred.resolve(data);
                    });
                return deferred.promise;
            };

            var setLockStatusLista = function (id) {
                var deferred = $q.defer();
                $http.get(URL.STATUSLISTA, {
                    params: { id: id }
                })
                    .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 editImage = function (image) {
                var deferred = $q.defer();
                $http.post(URL.EDIT, image)
                    .success(function (data) {
                        deferred.resolve(data);
                    });
                return deferred.promise;
            };

            
            var organize = function (members) {
                var deferred = $q.defer();
                $http.post(URL.ORGANIZE, members)
                    .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 updateLibrary = function (library) {
                var deferred = $q.defer();
                $http.post(URL.UPDATE_LIB, library)
                    .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;
            };

            var get = function (id) {
                var deferred = $q.defer();
                $http.get(URL.GET, {
                    params: { id: id }
                }).success(function (data) {
                    var image = ImageFactory.create(data);
                    deferred.resolve(image);
                });

                return deferred.promise;
            };

            return {
                query: query,
                equery: equery,
                remove: remove,
                removeLibrary: removeLibrary,
                organize: organize,
                editImage: editImage,
                getSettings: getSettings,
                saveSettings: saveSettings,
                updateLibrary: updateLibrary,
                removeSettings: removeSettings,
                libraries: libraries,
                elibraries: elibraries,
                setLockStatusLista: setLockStatusLista,
                get:get,
              
            };
        }
    ]);