(function () {
    "use strict";

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

    function factory($http, $q) {

        var _getConsignacoes = function (filtro) {
            var deferred = $q.defer();
            var args = {
                'chaves': filtro.chaves,
                'codigo': filtro.codigo,
                'cliente': filtro.cliente,
                'emissaoDe': filtro.emissao.de,
                'emissaoAte': filtro.emissao.ate,
                'valorTotalDe': filtro.valorTotal.de,
                'valorTotalAte': filtro.valorTotal.ate,
                'status': filtro.status,
            }
            $http.post('/consignacao/GetConsignacoes', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

        var _prepareNew = function () {
            var deferred = $q.defer();
            $http.post('/consignacao/PrepareNew')
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

        var _addItem = function (consignacao, codigoProduto) {
            var deferred = $q.defer();
            var args = {
                'consignacao': consignacao,
                'codigoProduto': codigoProduto,
            };
            $http.post('/consignacao/AddItem', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

        var _trocarTabelaPreco = function (consignacao, codigoProduto) {
            var deferred = $q.defer();
            var args = {
                'consignacao': consignacao,
                'codigoProduto': codigoProduto,
            };

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

        var _getAllTiposConsignacao = function () {
            var deferred = $q.defer();
            $http.post('/consignacao/GetAllTiposConsignacao')
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

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

        var _getConsignacao = function (codigo) {
            var deferred = $q.defer();
            var args = {
                'codigo': codigo,
            };
            $http.post('/consignacao/GetConsignacao', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

        var _saveEvent = function (codigo, textoEvento) {
            var deferred = $q.defer();
            var args = {
                'codigo': codigo,
                'textoEvento': textoEvento
            };
            $http.post('/consignacao/SaveEvent', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };
        

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

        var _converterConsignacao = function (consignacao, listaprevenda) {
            var deferred = $q.defer();
            var args = {
                'consignacao': consignacao,
                'listaprevenda': listaprevenda,
            };
            $http.post('/consignacao/ConverterConsignacao', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

        var _converterConsignacaoGerarRetorno = function (consignacao, listaprevenda, naturezaoperacao, serie, volumes, transportadora, peso, frete) {
            var deferred = $q.defer();
            var args = {
                'consignacao': consignacao,
                'listaprevenda': listaprevenda,
                'naturezaoperacao': naturezaoperacao,
                'serie': serie,
                'volumes': volumes,
                'transportadora': transportadora,
                'peso': peso,
                'frete': frete,
            };
            $http.post('/consignacao/ConverterConsignacaoGerarRetorno', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

        var _getNaturezasOperacao = function () {
            var deferred = $q.defer();
            $http.post('/consignacao/GetNaturezasOperacao')
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

        var _getSeriesNF = function () {
            var deferred = $q.defer();
            $http.post('/consignacao/GetSeriesNF')
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

        var _getNaturezasOperacaoRetorno = function () {
            var deferred = $q.defer();
            $http.post('/consignacao/GetNaturezasOperacaoRetorno')
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

        var _gerarNotaFiscalSaida = function (consignacao, naturezaoperacao, serie, volumes, transportadora, peso, tipoFrete) {
            var deferred = $q.defer();
            var args = {
                'consignacao': consignacao,
                'naturezaoperacao': naturezaoperacao,
                'serie': serie,
                'volumes': volumes,
                'transportadora': transportadora,
                'peso': peso,
                'tipoFrete': tipoFrete
            };
            $http.post('/consignacao/GerarNotaFiscalSaida', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };
     
        var _gerarNotaFiscalRetorno = function (consignacao, naturezaoperacao, serie, volumes, transportadora, peso, frete) {
            var deferred = $q.defer();
            var args = {
                'consignacao': consignacao,
                'naturezaoperacao': naturezaoperacao,
                'serie': serie,
                'volumes': volumes,
                'transportadora': transportadora,
                'peso': peso,
                'frete': frete,
            };
            $http.post('/consignacao/GerarNotaFiscalRetorno', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

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

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

        return {
            getConsignacoes: _getConsignacoes,
            prepareNew: _prepareNew,
            addItem: _addItem,
            trocarTabelaPreco: _trocarTabelaPreco,
            getAllTiposConsignacao: _getAllTiposConsignacao,
            save: _save,
            getConsignacao: _getConsignacao,
            saveEvent: _saveEvent,
            encerrarConsignacao: _encerrarConsignacao,
            converterConsignacao: _converterConsignacao,
            converterConsignacaoGerarRetorno: _converterConsignacaoGerarRetorno,
            getNaturezasOperacao: _getNaturezasOperacao,
            getNaturezasOperacaoRetorno: _getNaturezasOperacaoRetorno,
            getSeriesNF: _getSeriesNF,
            gerarNotaFiscalSaida: _gerarNotaFiscalSaida,
            gerarNotaFiscalRetorno: _gerarNotaFiscalRetorno,
            imprimirComprovanteRemessa: _imprimirComprovanteRemessa,
            imprimirComprovanteRetorno: _imprimirComprovanteRetorno, 
        };
    }

})();