var burpControllers = angular.module('burpControllers', ['ngResource']);

burpControllers.controller('BurpListCtrl', [ '$scope', '$resource', '$http', function($scope, $resource, $http) {

	$scope.initList = function() {
		var burps = $resource('/burps/search/findFirst8ByOrderByTimestampDesc').get();
	
		burps.$promise.then(function(data) {
			$scope.burps = burps._embedded.burps;
	
			for (var i = 0; i < $scope.burps.length; i++) {
				$scope.burps[i].user = $resource($scope.burps[i]._links.user.href).get();
				$scope.burps[i].tags = $resource($scope.burps[i]._links.tags.href).get();
			}
		});
	}
	
	$scope.doBurp = function() {
		
		// simple HTTP POST
		$http.post('/burps', { "text" : $scope.text }).success(function() {
			console.log('REFRESH');
			$scope.initList();
        });
		
		// Resource (might be moved into a service)
//		var Burp = $resource('/burps');
//
//		var newBurp = new Burp({
//			"text" : $scope.text
//		});
//		
//		newBurp.$save();
	};

	
} ]);

burpControllers.controller('TagCtrl', [ '$scope', '$routeParams', '$resource', function($scope, $routeParams, $resource) {

	var burpsList = $resource($routeParams.taggedBurps).get();
	burpsList.$promise.then(function(data) {
		$scope.burps = burpsList._embedded.burps;

		for (var i = 0; i < $scope.burps.length; i++) {
			$scope.burps[i].user = $resource($scope.burps[i]._links.user.href).get();
			$scope.burps[i].tags = $resource($scope.burps[i]._links.tags.href).get();
		}
	});

} ]);
