'use strict';

/* Controllers */

var mod = angular.module('bananaraceApp.controllers');


mod.controller('RaceCtrl', ['$scope', '$routeParams', 'RaceService', function($scope, $routeParams, RaceService) {
	//fetch all drives for the current race
	var drives = [];

	//set route params in scope variables
	$scope.currentChampionship = $routeParams.idchampionship;
	$scope.currentTier = $routeParams.tier;
	$scope.currentPoule = $routeParams.poule;
	$scope.currentRace = $routeParams.idrace;

	//possible lapamounts
	$scope.lapamounts = [1,2,3,4,5];
	//initial selection
	$scope.selectedLapamount = 2;

	//start of the lap, if necessary
	$scope.newMeasurementStarttime = new Date();

	//set the starting values for the relative start applet
	$scope.relativeRaceStartAddMinutes = 0;
	$scope.relativeRaceStartAddSeconds = 0;
	$scope.relativeRaceTime = new Date();
	$scope.rightnow = new Date();
	$scope.relativeRaceStart = $scope.rightnow;

	$scope.measurementValidityChanged = function(measurement) {
		console.log('measurement changed: '+measurement.valid);
		RaceService.updateMeasurement(measurement).
		success(function(data, status, headers, config) {
			console.log('updateMeasurement successful...');
			if(data['ok']==true) {
			} else {
			}
		}).
		error(function(data, status, headers, config) {
			console.log('updateMeasurement failed...');
		});;
	};

	$scope.relativeTimestampChanged = function() {
		var start = $scope.relativeRaceStart;
		var plusMinutes = $scope.relativeRaceStartAddMinutes;
		var plusSeconds = $scope.relativeRaceStartAddSeconds;

		console.log('plus seconds: '+plusSeconds);

		//using moments to calculate offsets and relative shit
		var newMoment = moment(start);
		newMoment.add(plusMinutes, 'minutes').add(plusSeconds, 'seconds');

		//set it!
		$scope.relativeRaceTime = newMoment;

		console.log('new relative time: '+newMoment);

	};

	$scope.setSelectedLapamount = function(amount) {
		$scope.selectedLapamount = amount;
	};

	$scope.refreshDrives = function() {
		console.log('doing refreshRaces');
		RaceService.getDrives($scope.currentRace).
		success(function(data, status, headers, config) {
			console.log('refreshDrives successful...');
			console.log(data);
			if(data['ok']==true) {
				$scope.drives = data.drives;
			} else {
			}
		}).
		error(function(data, status, headers, config) {
			console.log('refreshDrives failed...');
		});
	};

	$scope.refreshMeasurements = function() {
		console.log('doing refreshMeasurements');
		RaceService.getMeasurements($scope.currentRace).
		success(function(data, status, headers, config) {
			console.log('refreshMeasurements successful...');
			console.log(data);
			if(data['ok']==true) {
				$scope.measurements = data.measurements;
			} else {
			}
		}).
		error(function(data, status, headers, config) {
			console.log('refreshMeasurements failed...');
		});
	};

	$scope.refreshTeam = function() {
		RaceService.getTeam($scope.currentRace).
		success(function(data, status, headers, config) {
			console.log('refreshTeam successful...');
			console.log(data);
			if(data['ok']==true) {
				$scope.team = data.team;
				if(data.team.drivers.length > 0) {
					$scope.selectedDriver = data.team.drivers[0];
				}
			} else {
			}
		}).
		error(function(data, status, headers, config) {
			console.log('refreshDrivers failed...');
		});
	};

	$scope.setSelectedDriver = function(driver) {
		$scope.selectedDriver = driver;
	};

	$scope.addDrive = function() {
		RaceService.addDrive($scope.currentRace, $scope.selectedDriver, $scope.selectedLapamount).
		success(function(data, status, headers, config) {
			console.log('addDrive successful...');
			console.log(data);
			if(data['ok']==true) {
				$scope.refreshDrives();
			} else {
			}
		}).
		error(function(data, status, headers, config) {
			console.log('addDrive failed...');
		});
	};

	$scope.deleteDrive = function(drivenr, idrace) {
		RaceService.deleteDrive(drivenr, idrace).
		success(function(data, status, headers, config) {
			console.log('deleteDrive successful...');
			console.log(data);
			if(data['ok']==true) {
				$scope.refreshDrives();
			} else {
			}
		}).
		error(function(data, status, headers, config) {
			console.log('deleteDrive failed...');
		});
	};

	$scope.addMeasurementNow = function() {
		RaceService.addMeasurementNow($scope.currentRace).
		success(function(data, status, headers, config) {
			console.log('addMeasurementNow successful...');
			console.log(data);
			if(data['ok']==true) {
				$scope.refreshMeasurements();
			} else {
			}
		}).
		error(function(data, status, headers, config) {
			console.log('addMeasurementNow failed...');
		});
	};

	$scope.addMeasurementRelative = function() {
		RaceService.addMeasurementRelative($scope.currentRace, $scope.relativeRaceTime).
		success(function(data, status, headers, config) {
			console.log('addMeasurementRelative successful...');
			console.log(data);
			if(data['ok']==true) {
				$scope.refreshMeasurements();
			} else {
			}
		}).
		error(function(data, status, headers, config) {
			console.log('addMeasurementRelative failed...');
		});
	};

	$scope.deleteMeasurement = function(idmeasurement) {
		RaceService.deleteMeasurement(idmeasurement).
		success(function(data, status, headers, config) {
			console.log('deleteMeasurement successful...');
			console.log(data);
			if(data['ok']==true) {
				$scope.refreshMeasurements();
			} else {
			}
		}).
		error(function(data, status, headers, config) {
			console.log('deleteMeasurement failed...');
		});
	};

	$scope.refreshMeasurements();
	$scope.refreshDrives();
	$scope.refreshTeam();
}]);




mod.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();
}]);





















mod.controller('NavCtrl', ['$scope', '$location', 'MainService', function ($scope, $location, MainService) {

	$scope.loggedIn = function() {
		return LoginService.isLoggedIn();
	};

	$scope.getActiveCss = function (path) {
		// console.log('get activecss: '+path+' '+$location.path());
		var ok = $location.path().indexOf(path) != -1;
		return {
			active: ok
		};
	};

	$scope.logout = function() {
		LoginService.setLoggedIn(false);
	};

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


mod.controller('TimepickerDemoCtrl', ['$scope', function ($scope, $log) {

	console.log('TIMERPICKERDEMOCTRL DOES SOMETHING');

  $scope.mytime = new Date();

  $scope.hstep = 1;
  $scope.mstep = 15;

  $scope.options = {
    hstep: [1, 2, 3],
    mstep: [1, 5, 10, 15, 25, 30]
  };

  $scope.ismeridian = true;
  $scope.toggleMode = function() {
    $scope.ismeridian = ! $scope.ismeridian;
  };

  $scope.update = function() {
    var d = new Date();
    d.setHours( 14 );
    d.setMinutes( 0 );
    $scope.mytime = d;
  };

  $scope.changed = function () {
    $log.log('Time changed to: ' + $scope.mytime);
  };

  $scope.clear = function() {
    $scope.mytime = null;
  };
}]);



mod.controller('DriveCtrl', ['$scope', 'RaceService', 'DriveService', '$routeParams', function ($scope, RaceService, DriveService, $routeParams) {

	//set route params in scope variables
	$scope.currentChampionship = $routeParams.idchampionship;
	$scope.currentTier = $routeParams.tier;
	$scope.currentPoule = $routeParams.poule;
	$scope.currentRace = $routeParams.idrace;
	$scope.currentDrive = $routeParams.drivenr;

	$scope.refreshComments = function() {
		DriveService.getComments($scope.currentRace, $scope.currentDrive).
		success(function(data, status, headers, config) {
			console.log('refreshComments successful...');
			console.log(data);
			if(data['ok']==true) {
				$scope.comments = data.comments;
			} else {
				console.log('shit hit the fan with refreshComments');
			}
		}).
		error(function(data, status, headers, config) {
			console.log('refreshComments failed...');
		});
	};

	$scope.deleteComment = function(idcomment) {
		DriveService.deleteComment(idcomment).
		success(function(data, status, headers, config) {
			console.log('deleteComment successful...');
			console.log(data);
			if(data['ok']==true) {
				$scope.refreshComments();
			} else {
				console.log('shit hit the fan with deleteComment');
			}
		}).
		error(function(data, status, headers, config) {
			console.log('deleteComment failed...');
		});
	};

	$scope.addComment = function(newcomment, newpenaltyseconds) {
		console.log('comment: '+newcomment);
		console.log('penaltyseconds: '+newpenaltyseconds);
		// this.addComment = function(idrace, drivenr, comment, penaltyseconds) {
		DriveService.addComment($scope.currentRace, $scope.currentDrive, newcomment, newpenaltyseconds).
		success(function(data, status, headers, config) {
			console.log('addComment successful...');
			console.log(data);
			if(data['ok']==true) {
				$scope.refreshComments();
			} else {
				console.log('shit hit the fan with addComment')
			}
		}).
		error(function(data, status, headers, config) {
			console.log('addcomment failed...');
		});
	};

	$scope.refreshComments();

}]);
