(function () {
    "use strict";

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

    function factory($http, $q) {

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

        var _getStatusImposto = function () {

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

        var _create = function (notaFiscal) {
            var deferred = $q.defer();
            var args = { 'nf': notaFiscal };
            $http.post('/notafiscalcomplementar/create', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        }

        var _getByOrigem = function (notaFiscal) {
            var deferred = $q.defer();
            var args = { 'nf': notaFiscal };
            $http.post('/notafiscalcomplementar/getbyorigem', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

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

        var _save = function (item) {
            ;
            var deferred = $q.defer();
            var args = { infoAdicional: item.InformacaoAdicional, id: item.Id };
            $http.post('/notafiscalcomplementar/save', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

        var _saveImposto = function (imposto, idProduto) {
            var deferred = $q.defer();
            var args = { 'imposto': imposto, 'idProduto': idProduto };
            $http.post('/notafiscalcomplementar/saveimposto', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

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


        return {
            get: _get,
            create: _create,
            getByOrigem: _getByOrigem,
            saveItens: _saveItens,
            save: _save,
            saveImposto: _saveImposto,
            generateNFe: _generateNFe,
            getStatusImposto: _getStatusImposto
        };
    }

})();