'use strict';

var ArticleService = angular.module('articles.services', [
    'system'
])
    .factory('ArticleService', [
        '$http',
        '$q',
        'System',
        'ArticleFactory',
        function($http, $q, System, ArticleFactory) {

            var URL = {
                ALL: '/qusion/api/article/query',
                GET: '/qusion/api/article/get',
                GETSHARE: '/qusion/api/article/getShare',
                GETEDIT: '/qusion/api/article/getedit',
                CREATE: '/qusion/api/article/create',
                UPDATE: '/qusion/api/article/edit',
                DELETE: '/qusion/api/article/delete',
                SETTINGS: '/qusion/api/article/settings'
            };

            var query = function(params) {
                var deferred = $q.defer();
                $http.get(URL.ALL, {
                    params: params
                })
                    .success(function(data) {
                        var articles = [];
                        for (var i = 0; i < data.Items.length; i++) {
                            articles.push(ArticleFactory.create(data.Items[i]));
                        }
                        deferred.resolve({ articles: articles, 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 article = ArticleFactory.create(data);
                    deferred.resolve(article);
                });

                return deferred.promise;
            };

            var getShare = function (id) {
                var deferred = $q.defer();
                $http.get(URL.GETSHARE, {
                    params: { id: id }
                }).success(function (data) {
                    var article = ArticleFactory.create(data);
                    deferred.resolve(article);
                });

                return deferred.promise;
            };

            var getEdit = function(id) {
                var deferred = $q.defer();
                $http.get(URL.GETEDIT, {
                    params: { id: id }
                }).success(function(data) {
                    var article = ArticleFactory.create(data);
                    deferred.resolve(article);
                });

                return deferred.promise;
            };

            var create = function(article) {
                var deferred = $q.defer();
                $http.post(URL.CREATE, article
                ).success(function(data) {
                    deferred.resolve(data);
                });

                return deferred.promise;

            };

            var update = function(article) {
                var deferred = $q.defer();
                $http.post(URL.UPDATE, article
                ).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 getSettings = function() {
                var deferred = $q.defer();
                $http.get(URL.SETTINGS, { }).success(function(data) {
                    deferred.resolve(data);
                });

                return deferred.promise;
            };

            var saveSettings = function(setings) {
                var deferred = $q.defer();
                $http.post(URL.SETTINGS, setings
                ).success(function() {
                    deferred.resolve(setings);
                });

                return deferred.promise;
            };

            return {
                query: query,
                get: get,
                getShare: getShare,
                create: create,
                update: update,
                remove: remove,
                getEdit: getEdit,
                getSettings: getSettings,
                saveSettings: saveSettings
            };
        }
    ]);