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

    service.getBRTagPage = function(page_num){
        var deferred = $q.defer();
        var url = '/api/backup_restore?page=' + parseInt(page_num).toString();
        console.log(url);
        $http.get(url).success(function(data) {
            deferred.resolve(data);
        });
        return deferred.promise;
    };

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

    service.createBRTag = function(brTag){
        var deferred = $q.defer();
        $http.post('/api/backup_restore', {
            tag: brTag.tag,
            device_id: brTag.device_id
        }).success(function(data){
            deferred.resolve(data);
        });
        return deferred.promise;
    };

    service.updateBRTag = function(brTag){
        console.log(brTag);
        var deferred = $q.defer();
        var url = "/api/backup_restore/" + brTag._id;
        var put_obj = {
            tag: brTag.tag,
            device_id: brTag.device_id
        };

        if (brTag.data_type){
            put_obj.data_type = brTag.data_type;
        }

        if (brTag.value){
            put_obj.value = brTag.value;
        }

        $http.put(url, data=put_obj).success(function(data){
           deferred.resolve(data);
        });
        return deferred.promise;
    };

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

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

    service.backupBRTagValueSingle = function(id){
        var deferred = $q.defer();
        var url = '/api/backup/' + id.toString();
        $http.get(url).success(function(data) {
            deferred.resolve(data);
        });
        return deferred.promise;
    };

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

    service.restoreBRTagValueSingle = function(id){
        var deferred = $q.defer();
        var url = '/api/restore/' + id.toString();
        $http.get(url).success(function(data) {
            deferred.resolve(data);
        });
        return deferred.promise;
    };




    return service;
});
