(function () {
    "use strict";

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

    function factory($http, $q) {
        var service = {};

        service.getNaturezas = function () {
            var deferred = $q.defer();

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

            return deferred.promise;
        };

        service.validarEnderecoClienteVarejo = function (endereco) {
            var deferred = $q.defer();
            var args = { endereco: endereco };

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

            return deferred.promise;
        }

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

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

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

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

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



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

        service.getImpostoValores = function (id) {
            var deferred = $q.defer();
            var args = {
                consumivelID: id
            };
            $http.post('/consumivel/GetImpostoValores', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

        service.addListaReferencias = function (consumivel, auxiliarChave) {
            var deferred = $q.defer();
            var args = {
                consumivel: consumivel,
                auxiliar: auxiliarChave
            };
            $http.post('/consumivel/AddListaReferencias', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

        service.getListaReferencias = function (id) {
            var deferred = $q.defer();
            var args = {
                consumivelID: id
            };
            $http.post('/consumivel/GetListaReferencias', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

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

        return service;
    }

})();