'use strict';

/* Controllers */

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



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

	$scope.navbarVisible = true;

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

	$scope.getActiveCss = function (path) {
		if(path=='/championships' && $location.path().indexOf('/championship')==0) {
			return {
				active:true
			};
		}


		// console.log('getActiveCss called with path: '+path);
		// console.log($location.path());

		// console.log('get activecss: '+path+' '+$location.path());
		var ok = $location.path().indexOf(path) == 0;
		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();

}]);
