'use strict';

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

            var URL = {
                QUERY: '/qusion/api/event/query',
                GET: '/qusion/api/event/get',
                GETEDIT: '/qusion/api/event/getedit',
                CREATE: '/qusion/api/event/create',
                CREATE_USER: '/qusion/api/event/createRadnik',
                CREATE_TEAM: '/qusion/api/event/createTeam',
                UPDATE: '/qusion/api/event/edit',
                UPDATE_USER: '/qusion/api/event/editRadnik',
                DELETE: '/qusion/api/event/delete',
                DELETE_USER: '/qusion/api/event/deleteRadnik'
            };

            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;
            };

            var getEdit = function(id) {
                var deferred = $q.defer();
                $http.get(URL.GETEDIT, {
                    params: { id: id }
                }).success(function(data) {
                    var event = EventFactory.create(data);
                    deferred.resolve(event);
                });

                return deferred.promise;
            };

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

                return deferred.promise;

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

                return deferred.promise;

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

                return deferred.promise;

            };

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

                return deferred.promise;
            };
            
            var updateRadnik = function (radnik) {
                var deferred = $q.defer();
                $http.post(URL.UPDATE_USER, radnik
                ).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 removeRadnik = function (params) {
                var deferred = $q.defer();
                $http.get(URL.DELETE_USER, {
                    params: params
                })
                    .success(function (data) {

                        deferred.resolve(data);
                    });

                return deferred.promise;
            };

            return {
                query: query,
                createRadnik: createRadnik,
                get: get,
                create: create,
                update: update,
                updateRadnik: updateRadnik,
                remove: remove,
                removeRadnik: removeRadnik,
                getEdit: getEdit,
                createTeam: createTeam
            };
        }
    ]);