poconsole.factory('Tag',function($q, $http, $log, dateConversion){
    var service = {};

    var makeDateinObject = function(ob){
        ob.datetime= new Date(ob.datetime);
        return ob;
    };

	service.getTag = function(id) {
        var deferred = $q.defer();
        $http.get('/api/tags/' + id).success(function(data) {
            console.log(data);
        	deferred.resolve({
    			tag:data
    		});
        });
        return deferred.promise;
    };

    service.getMultipleTags = function(ids){
        var deferred = $q.defer();
        $http.get('/api/multipletags/' + ids).success(function(data) {
        	deferred.resolve({
    			tag:data
    		});
        });
        return deferred.promise;
    };

    service.getTagList = function() {
        var deferred = $q.defer();
        $http.get('/api/tags').success(function(data) {
        	deferred.resolve({
    			tags:data.objects
    		});
        });
        return deferred.promise;
    };

    service.getTagHistoryBetween = function(id, start, end){
    	var deferred = $q.defer();
        var url = "/api/valuesbetween/" + id + "/" + start + "/" + end;
        console.log({url:url});
        $http.get(url).success(function(data) {
        	deferred.resolve({
    			vals:data
    		});
        });
        return deferred.promise;
    };

    service.getCurrentValues = function(){
    	var deferred = $q.defer();
        $http.get('/api/latest').success(function(data) {
            var vals = data.map(function(v){
                v.created_on = Date.create(v.created_on + "Z", { fromUTC: true });
                return v;
            });
    		deferred.resolve(vals);
        });
        return deferred.promise;
    };

    service.createTag = function(tag){
        $http.post('/api/tags', {
            tag: tag.tag,
            name: tag.name,
            tag_category: tag.tag_category,
            device_id: parseInt(tag.device_id),
            description: tag.description,
            data_type: tag.data_type,
            change_threshold: tag.change_threshold,
            guarantee_sec: tag.guarantee_sec,
            units: tag.units,
            min_expected: tag.min_expected,
            max_expected: tag.max_expected
        }).success(function(data){
            return data;
        });
    };

    service.updateTag = function(tag){
        $log.info("updateTag called with "+ JSON.stringify(tag));
        var put_obj = {
            tag: tag.tag,
            name: tag.name,
            tag_category: tag.tag_category,
            device_id: parseInt(tag.device_id),
            description: tag.description,
            data_type: tag.data_type,
            change_threshold: tag.change_threshold,
            guarantee_sec: tag.guarantee_sec,
            units: tag.units,
            min_expected: tag.min_expected,
            max_expected: tag.max_expected
        };
        console.log({id: tag._id, put_obj: put_obj});
        $http.put('/api/tags/'+ tag._id, data=put_obj).success(function(data){
            return data;
        }).error(function(err){
            $log.warn("updateTag Error: " + err);
        });
    };

    service.deleteTag = function(id){
        var deferred = $q.defer();
        var url = '/api/tags/' + id;
        $http.delete(url).success(function(data) {
            deferred.resolve({
                data: data
            });
        });
        return deferred.promise;
    };

    service.clearSingleTagData = function(id){
        var deferred = $q.defer();
        var url = '/api/tag_vals';
        var filters = [{"name": "id", "op": "==", "val": "%" + id.toString() + "%"}];
        var req_data = {"q": JSON.stringify({"filters": filters})};
        console.log(req_data);
        $http.delete(url, data=req_data).success(function(data) {
            deferred.resolve({
                status: data.status
            });
        });
        return deferred.promise;
    };

    service.clearAllTagData = function(){
        var deferred = $q.defer();
        var url = '/api/tag_vals';
        $http.delete(url).success(function(data) {
            deferred.resolve({
                status: data.status
            });
        });
        return deferred.promise;
    };

    service.getTagsAtTime = function(dt){
        var deferred = $q.defer();
        $http.get('/api/tagsattime/' + dt).success(function(data) {
            var vals = data.map(function(v){
                v.datetime = Date.create(v.datetime + "Z", { fromUTC: true });
                return v;
            });
    		deferred.resolve(vals);
        });
        return deferred.promise;
    };

    service.getDailyTotals = function(){
        var deferred = $q.defer();
        $http.get('/api/today_totals').success(function(data) {
            deferred.resolve(data);
        });
        return deferred.promise;
    };

    return service;
});
