'use strict';

var EventService =  angular.module('events.services', [
    'system'
])
    .factory('EventService', [
        '$http',
        '$q',
        'System',
        'EventFactory',
        function ($http, $q, System, EventFactory) {

            var URL = {
                QUERY: '/cms/api/eventlaktasi/query',
                GET: '/cms/api/eventlaktasi/get'
            };

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

                return deferred.promise;
            };         

            return {
                query: query,
                get: get
            };
        }
    ]);