(function () {
    'use strict';

    angular
        .module('webApp')
        .service('TransactionService', transactionService);

    transactionService.$inject = ['$http', 'TransactionListFactory', 'WebAppConstants'];

    function transactionService($http, TransactionListFactory, WebAppConstants){
        /* jshint validthis: true */
        this.fetchTransactions = function(successCallback, errorCallback){
            function success(response){

                TransactionListFactory.instance.copyValues(response.data.content);

                if(_.isFunction(successCallback)){
                    successCallback(response);
                }
            }

            function error(response){
                if (_.isFunction(errorCallback)){
                    errorCallback(response);
                }
            }

            $http.get(WebAppConstants.DOMAIN + '/api/v1/transactions/').then(success, error);
        };

        function createTransaction(requestData, successCallback, errorCallback){
            function success(data, status, headers, config){
                if(_.isFunction(successCallback)){
                    successCallback(data, status, headers, config);
                }
            }

            function error(data, status, headers, config){
                if (_.isFunction(errorCallback)){
                    errorCallback(data, status, headers, config);
                }
            }

            $http.post(WebAppConstants.DOMAIN + '/api/v1/account', requestData).then(success, error);
        }
    }
})();
