(function () {
    "use strict";

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

    function factory($http, $q) {
        var _getAll = function (filtro) {
            var deferred = $q.defer();
            var args = {
                'chaves': filtro.chaves,
                'cpf': filtro.cpf,
                'status': filtro.status,
                'emissaoDe': filtro.emissao.de,
                'emissaoAte': filtro.emissao.ate,
                'totalDe': filtro.total.de,
                'totalAte': filtro.total.ate,
                'skip': filtro.skip,
                'take': filtro.take,
                'terminal': filtro.codigoTerminal,
                'acrescimo': filtro.Acrescimo
            };

            $http.post('/ticket/getall', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });

            return deferred.promise;
        };

        var _count = function (filtro) {
            var deferred = $q.defer();
            var args = {
                'chaves': filtro.chaves,
                'cpf': filtro.cpf,
                'status': filtro.status,
                'emissaoDe': filtro.emissao.de,
                'emissaoAte': filtro.emissao.ate,
                'totalDe': filtro.total.de,
                'totalAte': filtro.total.ate,
                'terminal': filtro.codigoTerminal
            };
            $http.post('/ticket/count', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });

            return deferred.promise;
        };

        var _get = function (ticket, emissao) {
            var deferred = $q.defer();
            var args = { 'id': ticket, 'emissao': emissao };
            $http.post('/ticket/get', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); })

            return deferred.promise;
        };
        var _getChartItensbyGroup = function (vendas) {
            var deferred = $q.defer();
            var args = {
                vendas: vendas
            };
            $http.post('/ticket/getchartitensbygroup', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); })

            return deferred.promise;
        };


        var _InserirTEFCancel = function (id, idTef) {
            var deferred = $q.defer();
            var args = {
                'id': id,
                'idTef': idTef,
            };
            $http.post('/ticket/InsertTEFCancel', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); })

            return deferred.promise;
        };

        var _GetStatusTEFCancel = function (idSiTef) {
            var deferred = $q.defer();
            var args = { 'id': idSiTef };
            $http.post('/ticket/GetStatusTEFCancel', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); })

            return deferred.promise;
        };

        var _ConfirmaCancelamentoTEF = function (idSiTef, pedido, emissao) {
            var deferred = $q.defer();
            var args = { 'id': idSiTef, 'pedido': pedido, 'emissao': emissao };
            $http.post('/ticket/ConfirmaCancelamentoTEF', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); })

            return deferred.promise;
        };

        var _saveVendors = function (venda) {
            
            var deferred = $q.defer();
            var args = { 'venda': venda };
            $http.post('/ticket/saveVendors', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); })

            return deferred.promise;
        };

        var _inserirCancelamento = function (ticket, emissao, motivo) {
            var deferred = $q.defer();
            var args = {
                'ticket': ticket,
                'emissao': emissao,
                'motivo': motivo
            };

            $http.post('/ticket/CancelarTicket', args)
            .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

        return {
            getAll: _getAll,
            count: _count,
            get: _get,
            InserirTEFCancel: _InserirTEFCancel,
            GetStatusTEFCancel: _GetStatusTEFCancel,
            ConfirmaCancelamentoTEF: _ConfirmaCancelamentoTEF,
            saveVendors: _saveVendors,
            inserirCancelamento: _inserirCancelamento,
            getChartItensbyGroup: _getChartItensbyGroup
        };
    }

})();