poconsole.controller('backupRestoreCtrl', function($scope, $route, $http, $routeParams, Page, $log, BackupRestore, Device) {
	Page.setTitle('Backup & Restore');
    Page.setPage('backuprestore');

	$scope.data_types = [ 'REAL', 'INT', 'BOOL'];

    $scope.loadBRTagList = function(page_num){
    	$scope.loading = true;
        var getBRTagList = BackupRestore.getBRTagPage(page_num);
        getBRTagList.then(function(data) {
        	$scope.loading = false;
			$scope.num_pages = data.total_pages;
            $scope.page_num = data.page;
            $scope.total = data.num_results;
			$scope.per_page = 10;
        	$scope.brTags = data.objects.map(function(t){
				if (t.tag_type == "REAL"){
					t.value = parseFloat(t.value);
				} else {
					t.value = parseInt(t.value);
				}
				return t;
			});
        });
    };
    $scope.loadBRTagList(1); //Run this function on controller startup to load list

    var getAllDevices = Device.getAllDevices();
    getAllDevices.then(function(d){
        $scope.devices = d.devices;
    });

    $scope.submitAddBRTag = function(){
        var createStatus = BackupRestore.createBRTag($scope.newBRTag);
        createStatus.then(function(data){
            $scope.loadBRTagList(1);
        });
    };

    $scope.openDeleteBRTag = function(id){
        var getBRTag = BackupRestore.getBRTag(id);
        getBRTag.then(function(data){
            $scope.error = false;
			if (data.tag_type == "REAL"){
				data.value = parseFloat(data.value);
			} else {
				data.value = parseInt(data.value);
			}
            $scope.dBRTag = data;
        });
    };

    $scope.deleteBRTag = function(id){
        var deleteBRTag = BackupRestore.deleteBRTag(id);
        deleteBRTag.then(function(data){
            $scope.error = false;
            $scope.loadBRTagList(1);
        });
    };

    $scope.openEditBRTag = function(id){
        var getBRTag = BackupRestore.getBRTag(id);
        getBRTag.then(function(data){
			console.log(data);
            $scope.error = false;
			if (data.tag_type == "REAL"){
				data.value = parseFloat(data.value);
			} else {
				data.value = parseInt(data.value);
			}
            $scope.editBRTag = data;
        });
    };

    $scope.submitEditBRTag = function(){
        var updateBRTag = BackupRestore.updateBRTag($scope.editBRTag);
        updateBRTag.then(function(data){
            $scope.loadBRTagList(1);
        });
    };

	$scope.backupBRTag = function(id){
		var backupBRTagVal = BackupRestore.backupBRTagValueSingle(id);
		backupBRTagVal.then(function(data){
			$scope.loadBRTagList(1);
		});
	};

	$scope.backupBRTagsAll = function(){
    	$scope.loading = true;
		var backupBRTagVal = BackupRestore.backupBRTagValuesAll();
		backupBRTagVal.then(function(data){
			$scope.loadBRTagList(1);
		});
	};

	$scope.restoreBRTag = function(id){
		var restoreBRTagVal = BackupRestore.restoreBRTagValueSingle(id);
		restoreBRTagVal.then(function(data){
			$scope.loadBRTagList();
		});
	};

	$scope.restoreBRTagsAll = function(){
    	$scope.loading = true;
		var restoreBRTagVal = BackupRestore.restoreBRTagValuesAll();
		restoreBRTagVal.then(function(data){
			$scope.loadBRTagList(1);
		});
	};

	$scope.device_label = function(data_type_obj){
		return data_type_obj.address + " - " + data_type_obj.device_type.device_type;
	};
});
