poconsole.factory('tags',function($q, $http, $log, dateConversion){
	var getTag = function(id) {
        var deferred = $q.defer();
        var w ={"where":{"id":id}};
        $http.get('/tag', {params:w}).success(function(data) {
        	deferred.resolve({
    			tag:data
    		});
        });
        return deferred.promise;
    };

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

    var getDataTypes = function() {
        var deferred = $q.defer();
        $http.get('/data_type').success(function(data) {
        	deferred.resolve({
    			data_types:data
    		});
        });
        return deferred.promise;
    };

    var getTagHistoryBetween = function(id, start, end){
    	var deferred = $q.defer();
        var w = {'where':{'tagID': id, 'createdAt': {">":dateConversion.mysqlDate(start), "<": dateConversion.mysqlDate(end)}}};
        $http.get("/tag_val", {params:w}).success(function(data) {
        	deferred.resolve({
    			vals:data
    		});
        });
        return deferred.promise;
    };

    var getCurrentValues = function(){
    	var deferred = $q.defer();
        $http.get('/tag_val/latest').success(function(data) {
    		deferred.resolve({
    			vals:data
    		});
        });
        return deferred.promise;
    };

    var createTag = function(tag){
        $http.post('/tag/create', {
            tag: tag.tag,
            name: tag.name,
            tag_class:5,
            deviceID: tag.deviceID,
            units: tag.units,
            minExpected: tag.minExpected,
            maxExpected: tag.maxExpected,
            guarantee_sec: tag.guarantee_sec,
            change_threshold: tag.change_threshold,
            description: tag.description,
            data_type: tag.data_type
        }).success(function(data){
            return data;
        });
    };

    var updateTag = function(tag){
        $log.info("updateTag called with "+ JSON.stringify(tag));
        $http.post('/tag/update/'+ tag.id, {
            tag: tag.tag,
            name: tag.name,
            tag_class:5,
            deviceID: tag.deviceID,
            units: tag.units,
            minExpected: tag.minExpected,
            maxExpected: tag.maxExpected,
            guarantee_sec: tag.guarantee_sec,
            change_threshold: tag.change_threshold,
            description: tag.description,
            data_type: tag.data_type
        }).success(function(data){
            return data;
        }).error(function(err){
            $log.warn("updateTag Error: " + err);
        });
    };

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

    var clearSingleTagData = function(id){
        var deferred = $q.defer();
        var url = '/tag_val/clear/' + id;
        $http.get(url).success(function(data) {
            deferred.resolve({
                status: data.status
            });
        });
        return deferred.promise;
    };

    var clearAllTagData = function(){
        var deferred = $q.defer();
        var url = '/tag_val/clear/all';
        $http.get(url).success(function(data) {
            deferred.resolve({
                status: data.status
            });
        });
        return deferred.promise;
    };

    return {
    	getTag: getTag,
    	getTagList: getTagList,
        getDataTypes: getDataTypes,
        getTagHistoryBetween: getTagHistoryBetween,
    	getCurrentValues: getCurrentValues,
        createTag: createTag,
        updateTag: updateTag,
        deleteTag: deleteTag,
        clearSingleTagData: clearSingleTagData,
        clearAllTagData: clearAllTagData
    };
});
