(function () {
    "use strict";

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

    function factory($http, $q) {

        var _getReservas = 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('/reserva/GetReservas', 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('/reserva/PrepareNew')
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

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

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

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

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

        var _getReserva = function (codigo) {
            var deferred = $q.defer();
            var args = {
                'codigo': codigo,
            };
            $http.post('/reserva/GetReserva', 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('/reserva/SaveEvent', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

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

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

        return {
            getReservas: _getReservas,
            prepareNew: _prepareNew,
            addItem: _addItem,
            getAllTiposReserva: _getAllTiposReserva,
            save: _save,
            getReserva: _getReserva,
            saveEvent: _saveEvent,
            encerrarReserva: _encerrarReserva,
            converterReserva: _converterReserva,
        };
    }

})();