define(['app', 'jquery', 'underscore', 'providers/extended-route', 'ngRoute', 'authentication'], function(app, $, _) { 'use strict';

    var locationPath = window.location.pathname.toLowerCase().split('?')[0];

    app.config(['extendedRouteProvider', '$locationProvider', function($routeProvider, $locationProvider) {

        $locationProvider.html5Mode(true);
        $locationProvider.hashPrefix('!');

        // /decisions/*
        if(/^\/decisions($|\/.*)/.test(locationPath))
            registerRoutes_Decisions($routeProvider);

        // /insession/*
        if(/^\/insession($|\/.*)/.test(locationPath))
            registerRoutes_insession($routeProvider);

        $routeProvider.when('/404', { templateUrl: '/app/views/404.html', resolveUser: true }).otherwise({redirectTo: '/404'});
    }
  ]);

  //============================================================
  //
  //
  //============================================================
  function registerRoutes_Decisions(routeProvider) {

      $("base").attr('href', '/decisions/'); // allow full page reload outside of  /decisions/*

      routeProvider.when('/x', { templateUrl: 'views/decisions/index.html', resolveController: true });
  }

  //============================================================
  //
  //
  //============================================================
  function registerRoutes_insession(routeProvider) {

      $("base").attr('href', '/insession/'); // allow full page reload outside of  /insession/*

      routeProvider
      .when('/',           { templateUrl : 'views/meetings/documents/in-session/index.html',      resolveController : true})
      .when('/management', { templateUrl : 'views/meetings/documents/in-session/management.html', resolveController : true, resolve : { user : securize(["Administrator","EditorialService"]) } } )
      .when('/:meeting',   { templateUrl : 'views/meetings/documents/in-session/meeting-id.html', resolveController : true, progress : { stop : false } } );
    }

    //============================================================
    //
    //
    //============================================================
    function securize(requiredRoles) {
        return ['$q', '$rootScope', 'authentication', '$location', '$window', function($q, $rootScope, authentication, $location, $window) {

            return $q.when(authentication.getUser()).then(function (user) {

                var hasRole = !!_.intersection(user.roles, requiredRoles).length;

                if (!user.isAuthenticated) {
                    $window.location.href = 'https://accounts.cbd.int/signin?returnurl='+encodeURIComponent($location.absUrl());
                    throw user; // stop route change!
                }
                else if (!hasRole)
                    $location.url('/403?returnurl='+encodeURIComponent($location.url()));

                return user;
            });
        }];
    }
});

@@ -412,16 +412,27 @@
      // 
+/management
 /decisio
@@ -451,16 +451,28 @@
  if(/%5E%5C/
+management%5C/
 decision
@@ -1056,16 +1056,27 @@
 ref', '/
+management/
 decision
@@ -1160,17 +1160,16 @@
 .when('/
-x
 ', %7B tem
@@ -1236,16 +1236,134 @@
 rue %7D);%0A
+      routeProvider.when('/:meeting/:number', %7B templateUrl: 'views/decisions/edit.html', resolveController: true %7D);%0A
   %7D%0A%0A  /

Move decision stuff under /management/