'use strict';

var BlogService = angular.module('blogs.services', [
    'system'
])
    .factory('BlogService', [
        '$http',
        '$q',
        'System',
        'BlogFactory',
        function ($http, $q, System, BlogFactory) {

            var URL = {
                QUERY: '/qusion/api/blog/query',
                QUERY_POSTS: '/qusion/api/blog/queryPosts',
                GET: '/qusion/api/blog/get',
                CREATE: '/qusion/api/blog/create',
                EDIT: '/qusion/api/blog/edit',
                DELETE: '/qusion/api/blog/delete',
                DELETE_POST: '/qusion/api/blog/deletePost',
                CREATE_POST: '/qusion/api/blog/CreatePost',
                GET_POST_FOR_EDIT: '/qusion/api/blog/postForEdit',
                UPDATE_POST: '/qusion/api/blog/updatePost'
            };
            
            var query = function (params) {
                var deferred = $q.defer();
                $http.get(URL.QUERY, {
                    params: params
                })
                    .success(function(data) {
                        var blogs = [];
                        for (var i = 0; i < data.Items.length; i++) {
                            blogs.push(BlogFactory.create(data.Items[i]));
                        }
                        deferred.resolve({ blogs: blogs, total: data.Count });
                    });

                return deferred.promise;
            };
            
            var queryPosts = function (params) {
                var deferred = $q.defer();
                $http.get(URL.QUERY_POSTS, {
                    params: params
                })
                    .success(function (data) {
                        var posts = [];
                        for (var i = 0; i < data.Items.length; i++) {
                            posts.push(BlogFactory.create(data.Items[i]));
                        }
                        deferred.resolve({ posts: posts, 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 blog = BlogFactory.create(data);
                    deferred.resolve(blog);
                });
                
                return deferred.promise;
            };
            
            var getForEdit = function (id) {
                var deferred = $q.defer();
                $http.get(URL.GET_POST_FOR_EDIT, {
                    params: { id: id }
                }).success(function (data) {
                   var post = data;
                    deferred.resolve(post);
                });
                return deferred.promise;
            };

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

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

            var edit = function(blog) {
                var deferred = $q.defer();
                $http.post(URL.EDIT, blog)
                    .success(function(data) {

                        deferred.resolve(data);
                    });

                return deferred.promise;
            };
            
            var createPost = function (post) {
                var deferred = $q.defer();
                $http.post(URL.CREATE_POST, post)
                    .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 removePost = function (params) {
                var deferred = $q.defer();
                $http.get(URL.DELETE_POST, {
                    params: params
                })
                    .success(function (data) {

                        deferred.resolve(data);
                    });

                return deferred.promise;
            };

            return {
                query: query,
                get: get,
                create: create,
                edit: edit,
                remove: remove,
                removePost: removePost,
                createPost: createPost,
                getForEdit: getForEdit,
                updatePost: updatePost,
                queryPosts: queryPosts
            };
        }
    ]);
