app.factory("SensorData", ['$http', '$rootScope',
    function ($http, $rootScope){
        var config = $rootScope.config;
        var url = '/sensors/';
        var api = config.private_key;
        var obj = {};
        obj.get = function(q, g, t, e){
            if(typeof g != 'undefined'){
                if(g === "api"){
                    return $http({
                        method: 'GET',
                        params: {
                            api: api,
                            jsonloc: q.jsonloc,
                            args: q.args,
                            scriptName: q.source,
                            ts: t,
                            te: e
                        },
                        url: url+"data/"
                    }).then(function(results){
                        return results;
                    });
                }
                else{
                    var urlg = url+'data/';
                    return $http({
                    method: 'GET',
                        params: {
                            api: api,
                            scriptName: q.source,
                            args: q.args,
                            g: g,
                            ts: t,
                            te: e
                        },
                        url: urlg
                    })
                    .then(function(results){
                        return results.data;
                    });
                }
            }
            else{
                var urlg = url;
                return $http({
                    method: 'GET',
                    params: {
                        api: api,
                        script: q
                    },
                    url: urlg
                })
                .then(function(results){
                    return results.data;
                });
            }
            
        };

        obj.post = function(q, object){
            return $http({
                method: 'POST',
                url: url + q,
                data: object,
                transformRequest: function(obj) {
                    var str = [];
                    for(var p in obj)
                        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                    return str.join("&");}
            }).then(function(results){
                return results.data;
            });
        };

        obj.put = function(q, object){
            return $http({
                method: 'PUT',
                url: url + q,
                data: object,
                headers: headers,
                transformRequest: function(obj) {
                    var str = [];
                    for(var p in obj)
                        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                    return str.join("&");}
            }).then(function(results){
                return results.data;
            });
        };

        obj.delete = function(q){
            return $http({method: 'DELETE', url: url + q, headers: headers}).then(function(results){
                return results.data;
            });
        };
        return obj;
    }
]);