(function() {
    'use strict';

    angular
        .module('app.core')
        .factory('apiFactory', apiFactory);

    apiFactory.$inject = ['$http'];

    /* @ngInject */
    function apiFactory($http) {

        // var urlApi = '/api/';
        var urlApi = 'http://localhost:3011/v1/';
        // var urlApi = '/api/';
        var urlLogin = '/authentication/';


        var apiFactory = {
             getToDos: getToDos,
             getUser: getUser,
             getPerson: getPerson,
             getPersonActivities: getPersonActivities,
             getApi: getApi,
             getIdentityTypes: getIdentityTypes,
             updatePerson: updatePerson,
             authenticate: authenticate,
             getPersonUsers: getPersonUsers,
             getPersonOrgUnits: getPersonOrgUnits
        };

        return apiFactory;

        ////////////////////////////

        function authenticate(data, resolve, reject) {
            return $http.post(urlLogin + 'authenticate', data)
              .success(function (response) {
                resolve(response);
              })
              .error(function (err) {
                reject(err);
              })
        }

        function getUser(callback, usId){
            return $http.get(urlApi + 'users/' + usId)
            .success(function(data){
              callback(data);
            })
            .error(function(err){
              console.log(err);
            });
        }

        function getPersonUsers(personId, resolve, reject) {
          return $http.get(urlApi + 'persons/' + personId + '/users')
            .success(function (data) {
              resolve(data);
            })
            .error(function (err) {
              reject(err);
            })
        }

        function getApi(callback, param){
            return $http.get(urlApi + param)
            .success(function(data){
              callback(data);
            })
            .error(function(err){
              console.log(err);
            });
        }

        function getPersonActivities(id, resolve, reject){
            return $http.get(urlApi + 'persons/' + id + '/activities')
            .success(function(data){
              resolve(data);
            })
            .error(function(err){
              reject(err);
            });
        }

        function getPersonOrgUnits(id, resolve, reject) {
          return $http.get(urlApi + 'persons/' + id + '/orgUnits')
          .success(function(data){
            resolve(data);
          })
          .error(function(err){
            reject(err);
          });
        }

        function updatePerson(person, resolve, reject){
         $http({
            method:"PUT",
            url: urlApi + 'persons/' + person._id,
            data: person
          })
          .success(function(data){
            resolve(data);
          })
          .error(function(err){
            reject(err);
          });
        }

        // function getPerson(callback, perId){
        //     return $http.get(urlApi + 'persons/' + perId)
        //     .success(function(data){
        //       callback(data);
        //     })
        //     .error(function(err){
        //       console.log(err);
        //     });
        // }

        function getPerson(id, resolve, reject) {
          return $http.get(urlApi + 'persons/' + id)
          .success(function(data){
            resolve(data);
          })
          .error(function(err){
            reject(err);
          });
        }

        function getIdentityTypes(resolve, reject) {
          return $http.get(urlApi + 'identitytypes')
          .success(function(data){
            resolve(data);
          })
          .error(function(err){
            reject(err);
          });
        }

        function getToDos() {
          //return todos;
        }
    }
})();
