(function () {
    "use strict";

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

    function factory($http, $q) {

        var cores = [
            { 'Codigo': '#f7786b', 'Nome': 'site' },
            { 'Codigo': '#f44336', 'Nome': 'site-red' },
            { 'Codigo': '#e91e63', 'Nome': 'site-pink' },
            { 'Codigo': '#9c27b0', 'Nome': 'site-purple' },
            { 'Codigo': '#673ab7', 'Nome': 'site-deeppurple' },
            { 'Codigo': '#3f51b5', 'Nome': 'site-indigo' },
            { 'Codigo': '#2196f3', 'Nome': 'site-blue' },
            { 'Codigo': '#03a9f4', 'Nome': 'site-lightblue' },
            { 'Codigo': '#00bcd4', 'Nome': 'site-cyan' },
            { 'Codigo': '#009688', 'Nome': 'site-teal' },
            { 'Codigo': '#4caf50', 'Nome': 'site-green' },
            { 'Codigo': '#8bc34a', 'Nome': 'site-lightgreen' },
            { 'Codigo': '#cddc39', 'Nome': 'site-lime' },
            { 'Codigo': '#ffeb3b', 'Nome': 'site-yellow' },
            { 'Codigo': '#ffc107', 'Nome': 'site-amber' },
            { 'Codigo': '#ff9800', 'Nome': 'site-orange' },
            { 'Codigo': '#ff5722', 'Nome': 'site-deeporange' },
            { 'Codigo': '#795548', 'Nome': 'site-brown' },
            { 'Codigo': '#9e9e9e', 'Nome': 'site-grey' },
            { 'Codigo': '#607d8b', 'Nome': 'site-bluegrey' },
            { 'Codigo': '#212121', 'Nome': 'site-black' },
            { 'Codigo': '#782929', 'Nome': 'site-marsala' },
            { 'Codigo': '#88B04B', 'Nome': 'site-greenery' },
            { 'Codigo': '#f7786b', 'Nome': 'site-rose-quartz' },
            { 'Codigo': '#5F4B8B', 'Nome': 'site-ultraviolet' } 
        ];

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

        var _getParametroLoja = function (parametro) {
            var deferred = $q.defer();
            var args = { 'parametro': parametro }
            $http.post('/config/GetParametroLoja', 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 = { 'item': item };
            $http.post('/config/save', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

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

        var _saveTerminal = function (terminal) {
            var deferred = $q.defer();
            var args = { 'terminal': terminal };
            $http.post('/config/SaveTerminal', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        }; 
        var _saveOperacaoVendaOnSession = function (operacaoVenda) {
            var deferred = $q.defer();
            var args = { 'operacaoVenda': operacaoVenda };
            $http.post('/config/SaveOperacaoVendaOnSession', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

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

        var _getCorTema = function (nome) {
            var deferred = $q.defer();
            var response = {};
            for (var i = 0; i < cores.length; i++) {
                if (cores[i].Nome === nome) {
                    response = cores[i];
                    break;
                }
            }
            deferred.resolve(response);
            return deferred.promise;
        }
        var _getAllCorTema = function () {
            var deferred = $q.defer();
            var response = {
                Status: 0,
                Message: null,
                Data: cores
            };
            deferred.resolve(response);
            return deferred.promise;
        }

        var _GetAllFormasPGTO = function () {
            var deferred = $q.defer();
            var args = {}
            $http.post('/config/GetAllFormasPGto', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };
        var _UpdateStatusFormaPGTO = function (codigo, statusnfe) {
            var deferred = $q.defer();
            var args = {
                'statusnfe': statusnfe,
                'codigo': codigo,
            }
            $http.post('/config/UpdateStatusFormaPGTO', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

        var _getAllTabelasPreco = function () {
            var deferred = $q.defer();
            var args = {}
            $http.post('/config/GetAllTabelasPreco', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        }; 
        var _getAllExcecao = function () {
            var deferred = $q.defer();
            var args = {}
            $http.post('/config/GetAllExcecao', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };
        var _updateExcecao = function (item) {
            var deferred = $q.defer();
            var args = { 'item': item };
            $http.post('/config/UpdateExcecao', args)
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };
        var _getAllTiposTerminal = function (rebuild) {
            var deferred = $q.defer();
            $http.get('/config/getalltiposterminal')
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };

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

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

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

        var _GetValorCookie = function () {
            var deferred = $q.defer();
            $http.post('/config/GetValorCookie')
                .success(function (data) { deferred.resolve(data); })
                .error(function (data, status, headers, config) { deferred.reject(data); });
            return deferred.promise;
        };
        return {
            get: _get,
            save: _save,
            saveTerminal: _saveTerminal,
            saveOperacaoVendaOnSession: _saveOperacaoVendaOnSession,
            saveVendedorOnSession: _saveVendedorOnSession,
            getCorTema: _getCorTema,
            getAllCorTema: _getAllCorTema,
            GetAllFormasPGTO: _GetAllFormasPGTO,
            UpdateStatusFormaPGTO: _UpdateStatusFormaPGTO,
            getAllTiposTerminal: _getAllTiposTerminal,
            saveParametroLoja: _saveParametroLoja,
            getParametroLoja: _getParametroLoja,
            getAllTabelasPreco: _getAllTabelasPreco,
            SetarDescricao: _SetarDescricao,
            SetarTerminal: _SetarTerminal,
            RemoverTerminal: _RemoverTerminal,
            GetValorCookie: _GetValorCookie,
            UpdateExcecao: _updateExcecao,
            GetAllExcecao:  _getAllExcecao
    };
}

})();


