var otsApp = angular.module('otsApp', ['ngRoute', 'events.module', 'FlotLineChart.module']);

/*
 * These constants (the paths and HTML filename below) are tied into the
 * ControllerStatus values (ControllerStatus.java).
 */
otsApp.config(function($routeProvider)
    {
    $routeProvider
            .when("/IDLE",
                {
                templateUrl: 'modules/idle/IDLE.html',
                controller: 'idleController'
                })
            .when("/LOADING_REPO",
                {
                templateUrl: 'modules/loading/LOADING_REPO.html',
                controller : 'loadingController'
                })
            .when("/STARTING_TEST",
                {
                templateUrl: 'modules/starting/STARTING_TEST.html',
                controller : 'startingController'
                })
            .when("/RUNNING_TEST",
                {
                templateUrl: 'modules/running/RUNNING_TEST.html',
                //controller : 'runningController'
                })
            .when("/UPLOADING_RESULTS",
                {
                templateUrl: 'modules/uploading/UPLOADING_RESULTS.html',
                controller : 'uploadingController'
                })
            //.otherwise({redirectTo:'/main.html'});  // does this work?
    });

otsApp.controller('mainController', function($rootScope, $scope, $location, eventSubscriptionService)
    {
    $rootScope.status = {};

    eventSubscriptionService.subscribe('ControllerStatus', function(message)
        {
        $rootScope.$apply(function () // this ensures the changes get applied
            {
            var status = JSON.parse(message.data);
            $location.path(status.state);  // route to the corresponding page for the state
            $rootScope.status = status;  // each page needs the controller status, so it goes in the root scope
            if (status.reportUrl !== null)
                window.location.href = status.reportUrl;  // Go to the test report
            });
        });
    });

