'use strict';

angular.module('app.controllers', []);
angular.module('app.directives', []);
angular.module('app.filters', []);
angular.module('app.services', []);


// Declares how the application should be bootstrapped. See: http://docs.angularjs.org/guide/module
angular.module('app', ['ui.router', 'app.filters', 'app.services', 'app.directives', 'app.controllers'])

    // Gets executed during the provider registrations and configuration phase. Only providers and constants can be
    // injected here. This is to prevent accidental instantiation of services before they have been fully configured.
    .config([
        '$stateProvider', '$locationProvider', function($stateProvider, $locationProvider) {

            // UI States, URL Routing & Mapping. For more info see: https://github.com/angular-ui/ui-router
            // ------------------------------------------------------------------------------------------------------------

            $stateProvider
                .state('home', {
                    url: '/',
                    templateUrl: 'app/index',
                    controller: 'IndexController'

                })
                .state('about', {
                    url: '/about',
                    templateUrl: 'app/about/about',
                    controller: 'AboutController'
                })
                .state('login', {
                    url: '/login',
                    layout: 'basic',
                    templateUrl: 'app/login/login',
                    controller: 'LoginController'
                })
                .state('otherwise', {
                    url: '*path',
                    templateUrl: 'app/404/404',
                    controller: '404Controller'
                });

            $locationProvider.html5Mode(true);

        }
    ])

    // Gets executed after the injector is created and are used to kickstart the application. Only instances and constants
    // can be injected here. This is to prevent further system configuration during application run time.
    .run([
        '$templateCache', '$rootScope', '$state', '$stateParams', function($templateCache, $rootScope, $state, $stateParams) {

            // <ui-view> contains a pre-rendered template for the current view
            // caching it will prevent a round-trip to a server at the first page load
            var view = angular.element('#ui-view');
            $templateCache.put(view.data('tmpl-url'), view.html());

            // Allows to retrieve UI Router state information from inside templates
            $rootScope.$state = $state;
            $rootScope.$stateParams = $stateParams;

            $rootScope.$on('$stateChangeSuccess', function(event, toState) {

                // Sets the layout name, which can be used to display different layouts (header, footer etc.)
                // based on which page the user is located
                $rootScope.layout = toState.layout;
            });
        }
    ]);
