angular.module('bananaraceApp.controllers').controller('ChampionshipsCtrl', ['$scope', 'ChampionshipService', function($scope, ChampionshipService) {
	var championships = [];

	$scope.refreshChampionships = function() {
		console.log('doing refreshChampionships');
		ChampionshipService.getChampionships().
		success(function(data, status, headers, config) {
			console.log('refreshChampionships successful...');
			console.log(data);
			if(data['ok']==true) {
				$scope.championships = data.championships;
			} else {
			}
		}).
		error(function(data, status, headers, config) {
			console.log('refreshChampionships failed...');
		});
	};

	$scope.deleteChampionship = function(championship) {
		ChampionshipService.deleteChampionship(championship).
		success(function(data, status, headers, config) {
			console.log('refreshChampionships successful...');
			if(data['ok']==true) {
				$scope.refreshChampionships();
			} else {
			}
		}).
		error(function(data, status, headers, config) {
			console.log('refreshChampionships failed...');
		});
	};

	$scope.addChampionship = function(newchampionship) {
		console.log("adding: "+newchampionship);
		ChampionshipService.addChampionship(newchampionship).
			success(function(data, status, headers, config) {
				console.log('addChampionships successful...');
				console.log(data);
				if(data['ok']==true) {
						$scope.refreshChampionships();
						newchampionship.name = "";
				}
			}).
			error(function(data, status, headers, config) {
				console.log('addChampionships failed...');
			}
		);
	};

	//initial refresh championships when building the controller
	$scope.refreshChampionships();
}]);
