'use strict';

// Declare app level module which depends on views, and components
angular.module('unicornucopia', [
  'ngMaterial',
  'ui.router',
  'ngCookies',
  
  'unicornucopia.authentication.services',
  'unicornucopia.authentication.controllers',

  'unicornucopia.application.services',
  'unicornucopia.application.controllers',

  'unicornucopia.accounts.directives',
  'unicornucopia.accounts.services',
  'unicornucopia.accounts.controllers',

  'unicornucopia.ideas.directives',
  'unicornucopia.ideas.services',
  'unicornucopia.ideas.controllers',

  'unicornucopia.comments.directives',
  'unicornucopia.comments.services',
  'unicornucopia.comments.controllers'
  
])

.value('VERSION', '1')
.constant('API_VERSION', '1')
.constant('PREFIX', 'APP')
// .constant('DOMAIN', 'http://localhost:8000')
.constant('DOMAIN', 'http://unicornucopia-dev.elasticbeanstalk.com')

.config(['$mdThemingProvider', function($mdThemingProvider) {
  $mdThemingProvider.theme('default')
    .primaryPalette('red');
}])

.config(function($mdIconProvider) {
    $mdIconProvider
      .icon('actions:unicorn', 'media/unicorn.svg', 21);
  })

.config(['$stateProvider', '$urlRouterProvider',
  function($stateProvider, $urlRouterProvider){

    $stateProvider
      .state('authentication', {
        url: '/authentication',  
        templateUrl: 'authentication/authentication.tmpl.html',
        controller: 'AuthenticationController'
      })

      .state('application',{
        url: '/application',
        templateUrl: 'application/application.tmpl.html',
        controller: 'ApplicationController'
      })

      .state('application.ideas',{
        url: '/ideas?blessingId',
        templateUrl: 'ideas/ideas.tmpl.html',
        controller: 'IdeasListController'
      })

      .state('application.ideas.create',{
        url: '/create',
        templateUrl: 'ideas/ideas.create.tmpl.html',
        controller: 'IdeaCreateController'
      })

      .state('application.ideas.detail',{
        url: '/detail/:id',
        templateUrl: 'ideas/ideas.detail.tmpl.html',
        controller: 'IdeaDetailController'
      })

      .state('application.account',{
        url: '/account/:id',
        templateUrl: 'accounts/account.tmpl.html',
        controller: 'AccountController'
      })

    $urlRouterProvider.otherwise('/application/ideas/create');
}])

.run(['$rootScope', '$state', '$urlRouter', 'Authentication', 
    function($rootScope, $state, $urlRouter, Authentication) {
      // check if the user is authenticated
      $rootScope.$on('$locationChangeSuccess', function(evt) {
        // Halt state change from even starting
        evt.preventDefault();
        // Verify the user has a session token
        var sessionToken = Authentication.getToken();
        // Continue with the update and state transition if logic allows
        if(sessionToken){
          $rootScope.authentication = {'status': true};
          $urlRouter.sync();
        }else{
          $rootScope.authentication = {'status': false};
          $state.go('authentication');
        }
    });

}]);

@@ -683,31 +683,8 @@
 %5D)%0A%0A
-.value('VERSION', '1')%0A
 .con
@@ -2689,16 +2689,31 @@
 n token%0A
+        try%7B%0A  
         
@@ -2754,24 +2754,26 @@
 getToken();%0A
+  
         // C
@@ -2841,16 +2841,18 @@
         
+  
 if(sessi
@@ -2863,32 +2863,34 @@
 ken)%7B%0A          
+ 
+ 
 $rootScope.authe
@@ -2925,24 +2925,26 @@
 ;%0A          
+  
 $urlRouter.s
@@ -2962,15 +2962,148 @@
     
+  
 %7Delse%7B%0A
+            $rootScope.authentication = %7B'status': false%7D;%0A            $state.go('authentication');%0A          %7D%0A        %7Dcatch(e)%7B%0A
     
@@ -3186,32 +3186,32 @@
 thentication');%0A
-
         %7D%0A    %7D)
@@ -3200,24 +3200,33 @@
 ;%0A        %7D%0A
+        %0A
     %7D);%0A%0A%7D%5D)

check for authentication