poconsole.controller('tagsCtrl', function($scope, $route, $http, $routeParams, Page, $log, tags, devices) {
	Page.setTitle('Tags');
    Page.setPage('tags');
    $scope.loadTagList = function(){
    	$scope.loading = true;
        var getTagList = tags.getTagList();
        getTagList.then(function(data) {
        	$scope.loading = false;
        	$scope.tags = data.tags;
        });
    };
    $scope.loadTagList();

    var getDataTypes = tags.getDataTypes();
    getDataTypes.then(function(d){
        $scope.data_types = d.data_types;
    });

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

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

    $scope.openDeleteTag = function(id){
        var getTag = tags.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 = tags.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 = tags.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 = tags.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 = tags.getTag(id);
        getTag.then(function(data){
            $scope.error = false;
            $scope.editTag = data.tag[0];
            $log.info("Started editing tag with parameters: "+ JSON.stringify($scope.editTag));
        });
    };

    $scope.submitEditTag = function(){
        var editStatus = tags.updateTag($scope.editTag);
        $log.info(editStatus);
        $scope.editStatus = editStatus.status;
        $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.dType;
	};

});
