(function () {
    'use strict';

    angular
        .module('app')
        .factory('Server', Server);

    Server.$inject = ['$http', '$q', '$timeout'];

    function Server($http, $q, $timeout) { 
        function get(action, data, successCallback, errorCallback) {
            var baseUrl = window.location.origin + urlGlobal + '/Terminais';
            $(".loading-div").fadeIn();
            var req = {
                method: 'GET',
                url: baseUrl + '/' + action,
                headers: {
                    'Content-Type': 'application/json; charset=utf-8',
                },
                data: data ? data : null
            };
            $http(req).then(function (data) {
                //var d = $q.defer();
                //d.resolve(data);
                //$timeout(function () {
                //successCallback(d.promise.$$state.value);
                successCallback(data);
                //}, 1000);
            }, function (error) {
                //if (error)
                    //$timeout(function () {
                        errorCallback(error);
                    //}, 1000);
            });
        }
        function post(action, data, successCallback, errorCallback) {
            var baseUrl = window.location.origin + urlGlobal + '/Terminais';
            $(".loading-div").fadeIn();
            var req = {
                method: 'POST',
                url: baseUrl + '/' + action,
                headers: {
                    'Content-Type': 'application/json; charset=utf-8'
                },
                data: data
            };
            $http(req).then(function (data) {
                successCallback(data);
            }, function (error) {
                if (error)
                    errorCallback(error);
            });
        }
        function getArraybuffer(action, data, successCallback, errorCallback) {
            var baseUrl = window.location.origin + urlGlobal + '/Terminais';
            var req = {
                method: 'GET',
                url: baseUrl + '/' + action,
                responseType: 'arraybuffer',
                headers: {
                    'Content-Type': 'application/json; charset=utf-8',
                },
                data: data ? data : null
            };
            $http(req).then(function (data) {
                successCallback(data);
            }, function (error) {
                errorCallback(error);
            });
        }

        return {
            get: get,
            post: post,
            getArraybuffer: getArraybuffer
        };
    }
})();