(function () {
    "use strict";

    angular.module('pos').factory('shipayService', factory);
    factory.$inject = ['$http', '$q'];

    function factory($http, $q) {

        
        var _executeOrder = (  pedido,   valorapagar,   wallet,   cpf)=>  {


            var data = Object.assign({}, { "codPedido": pedido, "valorapagar": valorapagar, "wallet": wallet, "cpf": cpf });

            var deferred = $q.defer();
            $http.post('/shipay/executeOrder', data)
                .success(  (data)=>  { deferred.resolve(data); })
                .error( (data, status, headers, config)=> { deferred.reject(data); });
            return deferred.promise;
        };


        var _sendEmail = (email, linkPagamento) => {


            var data = Object.assign({}, { "email": email, "linkPagamento": linkPagamento});

            var deferred = $q.defer();
            $http.post('/shipay/SendEmail', data)
                .success((data) => { deferred.resolve(data); })
                .error((data, status, headers, config) => { deferred.reject(data); });
            return deferred.promise;
        };

        var _getStatus = function (shipayId,order_id, codPedido, valor, wallet) {
            var deferred = $q.defer(); 
            var args = Object.assign({}, { 'shipayId': shipayId,'order_id': order_id, 'codPedido': codPedido, 'valor': valor, 'wallet': wallet });
            $http.post('/shipay/GetStatusShiPay', args)
                .success((data) => { deferred.resolve(data); }) 
                .error((data, status, headers, config) => { deferred.reject(data); });
            return deferred.promise;
        };


        var _cancelshipay = function (shipayId, order_id, codPedido) {
            var deferred = $q.defer();
            var args = Object.assign({}, { 'shipayId': shipayId, 'order_id': order_id, 'codPedido': codPedido  });
            $http.post('/shipay/CancelShiPay', args)
                .success((data) => { deferred.resolve(data); })
                .error((data, status, headers, config) => { deferred.reject(data); });
            return deferred.promise;
        };

        var _cancelIgnoredOrder = function (order_id, codPedido) {
            var deferred = $q.defer();
            var args = Object.assign({}, { 'order_id': order_id, 'codPedido': codPedido});
            $http.post('/shipay/CancelIgnoredOrder', args)
                .success((data) => { deferred.resolve(data); })
                .error((data, status, headers, config) => { deferred.reject(data); });
            return deferred.promise;
        };


        var _resolvePayPendente = function (pedido) {
            var deferred = $q.defer();
            var args = Object.assign({}, { 'pedido': pedido });
            $http.post('/shipay/ResolvePayPendente', args)
                .success((data) => { deferred.resolve(data); })
                .error((data, status, headers, config) => { deferred.reject(data); });
            return deferred.promise;
        };

 
        return {
            resolvePayPendente :_resolvePayPendente,
            executeOrder: _executeOrder,
            getStatus: _getStatus,
            cancelshipay: _cancelshipay,
            sendEmail: _sendEmail,
            cancelIgnoredOrder : _cancelIgnoredOrder
        };
    } 

})();