angular.module('bananaraceApp.controllers').controller('OverviewCtrl', ['$scope', 'OverviewService', '$routeParams', function ($scope, OverviewService, $routeParams) {
	//set route params in scope variables
	$scope.currentChampionship = $routeParams.idchampionship;

  console.log('OVERVIEW.JS LOADED');

	//data for the data gods
	$scope.overviewdata = {};

	$scope.refreshData = function() {
		OverviewService.getData($scope.currentChampionship).
		success(function(data, status, headers, config) {
			console.log('getData successful...');
			console.log(data);
			if(data['ok']==true) {

				//init the tier container
				var tiers = [];
				//post process
				for(var i = 0; i<data.rows.length; i++) {
					var tiernr = data.rows[i].tier;
					var poulenr = data.rows[i].poule;
					var idteam = data.rows[i].idteam;
					var teamname = data.rows[i].teamname;
          var bestpouletotaltime = data.rows[i].bestpouletotaltime;

          console.log('trying something...');

					//look for tier object
					var tierfound = false;
					var currenttier = null;
					for(var j = 0; j < tiers.length; j++) {
							if (tiers[j].tier == tiernr) {
									tierfound = true;
									currenttier = tiers[j];
									break;
							}
					}

					//init if necessary
					if(!tierfound) {
						currenttier = {
							tier:tiernr,
							poules:[]
						};
						tiers.push(currenttier);
					}

					//look for the poule object
					var poulefound = false;
					var currentpoule = null;
					for(var j = 0; j < currenttier.poules.length; j++) {
							if (currenttier.poules[j].poule == poulenr) {
									poulefound = true;
									currentpoule = currenttier.poules[j];
									break;
							}
					}

					//init if necessary
					if(!poulefound) {
						currentpoule = {
							poule:poulenr,
							teams:[]
						};
						currenttier.poules.push(currentpoule);
					}

					//look for the team object
					var teamfound = false;
					var currentteam = null;
					for(var j = 0; j < currentpoule.teams.length; j++) {
							if (currentpoule.teams[j].idteam == idteam) {
									teamfound = true;
									currentteam = currentpoule.teams[j];
									break;
							}
					}

					//init if necessary
					if(!teamfound) {
						currentteam = {
							idteam:idteam,
							name:teamname,
							races:[]
						};
            if(currentteam.idteam==null) {
              continue;
            }
						currentpoule.teams.push(currentteam);
					}

					//add the race (lowest level of the data) OR SHOULD BE THE LOWEST DUN DUN DUUUN
					var race = {
						idrace:data.rows[i].idrace,
						totaltime:data.rows[i].totaltime,
						totalpenaltyseconds:data.rows[i].totalpenaltyseconds,
            bestpouletotaltime:bestpouletotaltime,
            everythingmeasured:data.rows[i].everythingmeasured
					};

          console.log('race totaltime');
          console.log(race.totaltime);

          //only push if an id was set
          if(race.idrace!=null) {
					  currentteam.races.push(race);
          }

					console.log('looked for '+tiernr+' found: '+tierfound)
				}

				//set the tiers container
				$scope.overviewdata.tiers = tiers;

				console.log('tiers data');
				console.log($scope.overviewdata);

			} else {
				console.log('shit hit the fan with getData')
			}
		}).
		error(function(data, status, headers, config) {
			console.log('getData failed...');
		});
	};

  $scope.sumOfSeconds = function(racetime, penaltytime) {
    if(penaltytime==null) {
      return racetime;
    } else {
      return parseFloat(racetime)+parseFloat(penaltytime);
    }
  };

	//do this as init of the controller
	$scope.refreshData();
}]);
