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


mod.controller('PouleViewerCtrl', ['$scope', '$routeParams', 'ViewerService', '$interval',
    function($scope, $routeParams, ViewerService, $interval) {

        //store all the teams
        $scope.teamsInPoule = [];

        $scope.currentTier = $routeParams.tier;
        $scope.currentPoule = $routeParams.poule;

        $scope.refreshTeamsForPoule = function() {
            var idchampionship = $routeParams.idchampionship;
            var tier = $routeParams.tier;
            var poule = $routeParams.poule;
            ViewerService.getTeamsForPoule(idchampionship, tier, poule).
            success(function(data, status, headers, config) {
                console.log('getTeamsForPoule successful...');
                console.log(data);
                if (data['ok'] == true) {
                    $scope.teamsInPoule = data.teams;
                } else {
                    console.log('call returned but wasnt ok: ' + data.error);
                }
            }).
            error(function(data, status, headers, config) {
                console.log('getTeamsForPoule failed...');
            });
        };

        $scope.refreshTeamsForPoule();
        var timer = $interval($scope.refreshTeamsForPoule, 1000);

        // Cancel timer on destroying controller
        $scope.$on('$destroy', function() {
            $interval.cancel(timer);
        });
    }
]);