'use strict';

var KartaService = angular.module('karta.services', [
    'system'
])
    .factory('KartaService',  [
        '$http',
        '$q',
        'System',
        'KartaFactory',
        function ($http, $q, System, KartaFactory) {

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

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

                return deferred.promise;
            };
           
            return {
                query: query,
                get: get
            };
        }
    ]);
