'use strict';

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

            var URL = {
                ALL: '/cms/api/imagelaktasi/Query',
                QUERYALL: '/cms/api/imagelaktasi/QueryAll',
                GALLERY: '/cms/api/imagelaktasi/QueryGallery',
                ORGANIZE: '/cms/api/imagelaktai/organize',
                LIBRARIES: '/cms/api/librarylaktasi/GetByType',
                GLIBRARIES: '/cms/api/librarylaktasi/GetByType',
                SETTINGS: '/cms/api/imagelaktasi/settings'
            };

            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 queryall = function (params) {
                var deferred = $q.defer();
                $http.get(URL.QUERYALL, {
                    params: params
                })
                    .success(function (data) {
                        var images = [];
                        for (var i = 0; i < data.length; i++) {
                            images.push(ImageFactory.create(data[i]));
                        }
                        deferred.resolve(images);
                    });

                return deferred.promise;
            };

            var querygallery = function (params) {
                var deferred = $q.defer();
                $http.get(URL.GALLERY, {
                    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.GLIBRARIES, {
                    params: { 'type': 'eImage' }
                })
                    .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 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 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,
                queryall: queryall,
                querygallery: querygallery,
                remove: remove,
                removeLibrary: removeLibrary,
                organize: organize,
                getSettings: getSettings,
                saveSettings: saveSettings,
                removeSettings: removeSettings,
                libraries: libraries,
                elibraries: elibraries,
            };
        }
    ]);