'use strict';

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

            var URL = {
                QUERY: '/cms/api/genericcontentlaktasi/Query',
                GET: '/cms/api/genericcontentlaktasi/Get'
            };
            
            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(pageId) {
                var deferred = $q.defer();
                $http.get(URL.GET, {
                    params: { pageId: pageId }
                }).success(function (data) {
                    var content = ContentFactory.create(data);
                    deferred.resolve(content);
                });
                return deferred.promise;
            };
            
            return {
                query: query,
                get: get
            };
        }
    ]);