poconsole.controller('tagsCtrl', function($scope, $route, $http, $routeParams, Page, $log, Tag, Device) {
	Page.setTitle('Tags');
    Page.setPage('tags');

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

    $scope.loadTagList = function(){
    	$scope.loading = true;
        var getTagList = Tag.getTagList();
        getTagList.then(function(data) {
        	$scope.loading = false;
        	$scope.tags = data.tags;
        });
    };
    $scope.loadTagList();

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

    $scope.submitAddTag = function(){
        var createStatus = Tag.createTag($scope.newTag);
        $scope.createStatus = createStatus.status;
        $scope.loadTagList();
    };

    $scope.openDeleteTag = function(id){
        var getTag = Tag.getTag(id);
        getTag.then(function(data){
            $scope.error = false;
            $scope.dTag = data.tag;
            $log.info("Thinking about deleting tag with parameters: "+ JSON.stringify($scope.dTag));
        });
    };

    $scope.deleteTag = function(id){
        var deleteTag = Tag.deleteTag(id);
        deleteTag.then(function(data){
            $log.info("deleting tag "+ id + " status: " + data.status);
            $scope.error = false;
            $scope.loadTagList();
        });
    };

    $scope.openClearTagData = function(id){
        var getTag = Tag.getTag(id);
        getTag.then(function(data){
            $scope.error = false;
            $scope.dTagValues = data.tag;
            $log.info("Thinking about deleting tag data with parameters: "+ JSON.stringify($scope.dTagValues));
        });
    };

    $scope.deleteTagValues = function(id){
        var clearSingleTagData = Tag.clearSingleTagData(id);
        clearSingleTagData.then(function(data){
            $log.info("deleting tag "+ id + " status: " + data.status);
            $scope.error = false;
            $scope.loadTagList();
        });
    };

    $scope.openEditTag = function(id){
        var getTag = Tag.getTag(id);
        getTag.then(function(data){
            $scope.error = false;
            $scope.editTag = data.tag;
            $log.info("Started editing tag with parameters: "+ JSON.stringify($scope.editTag));
        });
    };

    $scope.submitEditTag = function(){
        var editStatus = Tag.updateTag($scope.editTag);
        $scope.loadTagList();
    };

    $scope.getPlotTags = function(){
        var tags_to_plot = [];
        for(var i =0; i<$scope.tags.length; i ++){
            if ($scope.tags[i].selectedForPlot){
                tags_to_plot.push($scope.tags[i]._id);
            }
        }
        $scope.tags_to_plot = tags_to_plot.join();
    };

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

});
