'use strict';

var ContentService = angular.module('contents.services', [
    'system'
])
    .factory('ContentService', [
        '$http',
        '$q',
        'System',
        'ContentFactory',
        function ($http, $q, System, ContentFactory) {

            var URL = {
                QUERY: '/qusion/api/genericContent/query',
                GET: '/qusion/api/genericContent/get',
                GET_EDIT: '/qusion/api/genericContent/getEdit',
                GET_EDIT_PAGE: '/qusion/api/genericContent/getEditPage',
                CREATE: '/qusion/api/genericContent/create',
                EDIT: '/qusion/api/genericContent/edit',
                DELETE: '/qusion/api/genericContent/delete'
            };
            
            var query = function (params) {
                var deferred = $q.defer();
                $http.get(URL.QUERY, {
                    params: params
                })
                    .success(function(data) {
                        var contents = [];
                        for (var i = 0; i < data.Items.length; i++) {
                            contents.push(ContentFactory.create(data.Items[i]));
                        }
                        deferred.resolve({ contents: contents, 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 content = ContentFactory.create(data);
                    deferred.resolve(content);
                });

                return deferred.promise;
            };
            
            var getEdit = function (id) {
                var deferred = $q.defer();
                $http.get(URL.GET_EDIT, {
                    params: { id: id }
                }).success(function (data) {
                    var content = ContentFactory.create(data);
                    deferred.resolve(content);
                });

                return deferred.promise;
            };

            var getEditPage = function (pageid) {
                var deferred = $q.defer();
                $http.get(URL.GET_EDIT_PAGE, {
                    params: { pageid: pageid }
                }).success(function (data) {
                    var content = ContentFactory.create(data);
                    deferred.resolve(content);
                });

                return deferred.promise;
            };

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

                return deferred.promise;

            };

            var update = function (content) {
                var deferred = $q.defer();
                $http.post(URL.EDIT, content
                ).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;
            };
            
            return {
                query: query,
                get: get,
                create: create,
                remove: remove,
                getEdit: getEdit,
                getEditPage:getEditPage,
                update: update
            };
        }
    ]);