'use strict';

var NewslettersService = angular.module('newsletters.services', [
    'system'
])
    .factory('NewslettersService',  [
        '$http',
        '$q',
        'System',
        'NewslettersFactory',
        function ($http, $q, System, NewslettersFactory) {

            var URL = {
                DELETE: '/qusion/api/newsletter/Delete',
                CREATE: '/qusion/api/newsletter/Create',
                QUERY: '/qusion/api/newsletter/Query',
                GET_FOR_EDIT: '/qusion/api/newsletter/Edit',
                UPDATE: '/qusion/api/newsletter/Update',
                STATUS: '/qusion/api/newsletter/Status',
               
            };


  

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

                return deferred.promise;
            };

      


            var get = function (id) {
                var deferred = $q.defer();
                $http.get(URL.GET_FOR_EDIT, {
                    params: { id: id }
                }).success(function (data) {
                    var newsletter = data;
                    deferred.resolve(newsletter);
                });
                return deferred.promise;
            };


            var update = function (newsletter) {
                var deferred = $q.defer();
                $http.post(URL.UPDATE, newsletter)
                    .success(function (data) {
                        deferred.resolve(data);
                    });

                return deferred.promise;
            };


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

                return deferred.promise;
            };

        

            return {
                query: query,
                get: get,
                create: create,
                update: update,
                remove: remove,
                setLockStatus: setLockStatus,
            };
        }
    ]);
