angular.module('bananaraceApp.controllers').controller('PouleCtrl', ['$scope', '$routeParams', 'PouleService', 'TeamService', 'MainService', function($scope, $routeParams, PouleService, TeamService, MainService) {

	$scope.currentChampionship = $routeParams.idchampionship;
	$scope.currentTier = $routeParams.tier;
	$scope.currentPoule = $routeParams.poule;
	$scope.activeIdRace = null;

	$scope.addTeamToPoule = function(team) {
		var idchampionship = $routeParams.idchampionship;
		var tier = $routeParams.tier;
		var poule = $routeParams.poule;
		PouleService.addTeam(team, idchampionship, tier, poule).
			success(function(data, status, headers, config) {
				console.log('added team to poule...');
				if(data['ok']==true) {
						$scope.refreshTeamsForPoule();
						$scope.refreshTeamsAvailableForPoule();
				}
			}).
			error(function(data, status, headers, config) {
				console.log('addPoule failed...');
			}
		);
	};

	$scope.removeTeamFromPoule = function(team) {
		var idchampionship = $routeParams.idchampionship;
		var tier = $routeParams.tier;
		var poule = $routeParams.poule;
		PouleService.removeTeam(team, idchampionship, tier, poule).
			success(function(data, status, headers, config) {
				console.log('added team to poule...');
				if(data['ok']==true) {
						$scope.refreshTeamsForPoule();
						$scope.refreshTeamsAvailableForPoule();
				}
			}).
			error(function(data, status, headers, config) {
				console.log('addPoule failed...');
			}
		);
	};

	$scope.refreshTeamsForPoule = function() {
		var idchampionship = $routeParams.idchampionship;
		var tier = $routeParams.tier;
		var poule = $routeParams.poule;
		console.log('TeamCtrl refreshing team in poule');

		TeamService.getTeamsInPoule(idchampionship, tier, poule).
		success(function(data, status, headers, config) {
			console.log('getTeamsInPoule successful...');
			console.log(data);
			if(data['ok']==true) {
				$scope.teamsInPoule = data.teams;
				//set first to be selected
				$scope.selectedTeam = data.teams[0];
			} else {
				console.log('call returned but wasnt ok: '+data['error']);
			}
		}).
		error(function(data, status, headers, config) {
			console.log('getTeamsInPoule failed...');
		});
	};

	$scope.refreshTeamsAvailableForPoule = function() {
		var idchampionship = $routeParams.idchampionship;
		var tier = $routeParams.tier;
		var poule = $routeParams.poule;
		console.log('TeamCtrl refreshing team in poule');

		TeamService.getTeamsAvailableForPoule(idchampionship, tier, poule).
		success(function(data, status, headers, config) {
			console.log('refreshTeamsAvailableForPoule successful...');
			console.log(data);
			if(data['ok']==true) {
				$scope.teamsAvailableForPoule = data.teams;
			} else {
				console.log('call returned but wasnt ok: '+data['error']);
			}
		}).
		error(function(data, status, headers, config) {
			console.log('getTeamsInPoule failed...');
		});
	};

	$scope.setSelectedTeam = function(team) {
		$scope.selectedTeam = team;
		console.log('team is selected: '+team);
	};

	$scope.addRace = function() {
		var idchampionship = $routeParams.idchampionship;
		var tier = $routeParams.tier;
		var poule = $routeParams.poule;
		var selectedTeam = $scope.selectedTeam;

		PouleService.addRace(idchampionship, tier, poule, selectedTeam.idteam).
			success(function(data, status, headers, config) {
				console.log('addRace successful...');
				console.log(data);
				if(data['ok']==true) {
						$scope.refreshRaces();
				}
			}).
			error(function(data, status, headers, config) {
				console.log('addRace failed...');
			}
		);;
	};

	$scope.deleteRace = function(race) {
		var idchampionship = $routeParams.idchampionship;
		var tier = $routeParams.tier;
		var poule = $routeParams.poule;
		console.log('PouleCtrl deleting race');

		PouleService.deleteRace(idchampionship, tier, poule, race.idrace).
		success(function(data, status, headers, config) {
			console.log('deleteRace call successful...');
			console.log(data);
			if(data['ok']==true) {
				$scope.refreshRaces();
			} else {
				console.log('deleteRace call returned but wasnt ok: '+data['error']);
			}
		}).
		error(function(data, status, headers, config) {
			console.log('deleteRace failed...');
		});
	};

	$scope.refreshRaces = function() {
		var idchampionship = $routeParams.idchampionship;
		var tier = $routeParams.tier;
		var poule = $routeParams.poule;
		console.log('PouleCtrl refreshing races');

		PouleService.getRacesInPoule(idchampionship, tier, poule).
		success(function(data, status, headers, config) {
			console.log('getRacesInPoule successful...');
			console.log(data);
			if(data['ok']==true) {
				$scope.racesInPoule = data.races;
			} else {
				console.log('getRacesInPoule call returned but wasnt ok: '+data['error']);
			}
		}).
		error(function(data, status, headers, config) {
			console.log('getTeamsInPoule failed...');
		});
	};

	$scope.toggleActiveRace = function(race) {
		var idchampionship = $routeParams.idchampionship;
		var tier = $routeParams.tier;
		var poule = $routeParams.poule;
		MainService.toggleActiveRace(race.idrace, tier, poule, idchampionship);
	};

	$scope.getActiveRace = function() {
		return MainService.getActiveRace();
	};

	$scope.addRaceForAllteams = function() {
		var idchampionship = $routeParams.idchampionship;
		var tier = $routeParams.tier;
		var poule = $routeParams.poule;
		for(var i = 0; i<$scope.teamsInPoule.length; i++) {
				PouleService.addRace(idchampionship, tier, poule, $scope.teamsInPoule[i].idteam).
				success(function(data, status, headers, config) {
					$scope.refreshRaces();;
				});
			// this.addRace = function(idchampionship, tier, poule, idteam) {
		}
	};

	$scope.$on('activeIdRaceChanged', function(event, message) {
		//the raceid changed!
		$scope.activeIdRace = message;
	});

	$scope.refreshTeamsForPoule();
	$scope.refreshTeamsAvailableForPoule();
	$scope.refreshRaces();
}]);
