'use strict';

window.ovPlayerDirectory = '/publish/lib/openveo-player/';
(function(angular) {

  var app = angular.module('ov.publish', ['ov.i18n', 'ov.entity', 'ov.player', 'vds.multirange', 'angular-time-polyfill']);

  /**
   * Defines a filter to print status in cells.
   *
   * This filter has one optional Number parameter which must be specified
   * if the input is equal to 8 (error status), to precise the error code.
   */
  function StatusFilter($filter) {
    return function(input, errorCode) {
      var label = $filter('translate')('VIDEOS.STATE_' + input);
      var type = 'label-danger';

      // Video is published
      if (input == 12)
        type = 'label-success';

      // Video is sent
      else if (input == 11)
        type = 'label-warning';

      // All other video states
      else if (input !== 0)
        type = 'label-info';

      // Video is on error
      if (input === 0)
        label = label + '(' + errorCode + ')';

      return '<span class=\'label ' + type + '\'>' + label + '</span>';
    };
  }

  /**
   * Filter to print Category in cells.
   */
  function CategoryFilter(publishService) {
    var rubrics = publishService.getCategoriesByKey();
    return function(input) {
      if (input && rubrics[input])
        return rubrics[input].name;
      else
        return '';
    };
  }

  app.run(['$rootScope', function($rootScope) {
    $rootScope.$on('$locationChangeStart', function(event, next, current) {
      // url slug : shortening the url to stuff that follows after "#"
      current = current.slice(current.lastIndexOf('/publish/') + 9, current.length);
      next = next.slice(next.lastIndexOf('/publish/') + 9, next.length);
      if (current == 'videos' && next.lastIndexOf('video/') >= 0) {
        $rootScope.newAnimation = 'RL';
      } else if (current.lastIndexOf('video/') >= 0 && next == 'videos') {
        $rootScope.newAnimation = 'LR';
      } else {
        $rootScope.newAnimation = '';
      }
    });
  }]);

  /*
   * Configures the ov.publish application by adding new routes.
   */
  app.config(['$routeProvider', function($routeProvider) {

    // Add route /publish/videos with authentication.
    // Also retrieve the list of videos
    $routeProvider.when('/publish/videos', {
      templateUrl: '/publish/be/views/videos.html',
      controller: 'VideoController',
      title: 'VIDEOS.PAGE_TITLE',
      access: 'access-videos-page',
      resolve: {
        categories: ['publishService', function(publishService) {
          return publishService.loadCategories();
        }],
        properties: ['publishService', function(publishService) {
          return publishService.loadProperties();
        }],
        platforms: ['publishService', function(publishService) {
          return publishService.loadPlatforms();
        }]
      }
    });

    // Add route /publish/videos/videosId with authentication.
    // Also retrieve the list of videos
    $routeProvider.when('/publish/video/:videoId', {
      templateUrl: '/publish/be/views/chapter.html',
      controller: 'ChapterController',
      title: 'CHAPTER.PAGE_TITLE',
      access: 'chapter-video',
      resolve: {
        video: ['publishService', '$route', function(publishService, $route) {
          var videoId = $route.current.params.videoId;
          return publishService.loadVideo(videoId);
        }]
      }
    });

    // Add route /publish/watcher with authentication.
    // Also retrieve the watcher status
    $routeProvider.when('/publish/watcher', {
      templateUrl: '/publish/be/views/watcher.html',
      controller: 'WatcherController',
      title: 'WATCHER.PAGE_TITLE',
      access: 'access-watcher-page',
      resolve: {
        watcherStatus: ['publishService', function(publishService) {
          return publishService.getWatcherStatus();
        }]
      }
    });

    // Add route /publish/properties with authentication.
    // Also retrieve the list of properties
    $routeProvider.when('/publish/properties', {
      templateUrl: '/publish/be/views/properties.html',
      controller: 'PropertiesController',
      title: 'PROPERTIES.PAGE_TITLE',
      access: 'access-properties-page'
    });

    // Add route /publish/categories with authentication.
    // Also retrieve the list of categories
    $routeProvider.when('/publish/categories', {
      templateUrl: '/publish/be/views/categories.html',
      controller: 'CategoriesController',
      title: 'CATEGORIES.PAGE_TITLE',
      access: 'access-categories-page',
      resolve: {
        categories: ['publishService', function(publishService) {
          return publishService.loadCategories();
        }]
      }
    });

  }]);

  app.filter('status', StatusFilter);
  app.filter('category', CategoryFilter);

  // Filter to display content in the table (cf. dataTable.html)
  StatusFilter.$inject = ['$filter'];
  CategoryFilter.$inject = ['publishService'];

})(angular);

@@ -136,16 +136,22 @@
 lish', %5B
+%0D%0A    
 'ov.i18n
@@ -152,16 +152,21 @@
 v.i18n',
+%0D%0A   
  'ov.ent
@@ -170,16 +170,21 @@
 entity',
+%0D%0A   
  'ov.pla
@@ -188,16 +188,21 @@
 player',
+%0D%0A   
  'vds.mu
@@ -211,16 +211,21 @@
 irange',
+%0D%0A   
  'angula
@@ -240,16 +240,20 @@
 olyfill'
+%0D%0A  
 %5D);%0D%0A%0D%0A 
@@ -3314,16 +3314,22 @@
 video: %5B
+'$q', 
 'publish
@@ -3349,32 +3349,36 @@
 oute', function(
+$q, 
 publishService, 
@@ -3384,24 +3384,62 @@
  $route) %7B%0D%0A
+          var deferred = $q.defer();%0D%0A
           va
@@ -3482,16 +3482,18 @@
 deoId;%0D%0A
+%0D%0A
         
@@ -3486,39 +3486,32 @@
 d;%0D%0A%0D%0A          
-return 
 publishService.l
@@ -3527,16 +3527,300 @@
 videoId)
+.then(function(result) %7B%0D%0A            if (result.data.entity)%0D%0A              deferred.resolve.apply(deferred, arguments);%0D%0A            else%0D%0A              deferred.reject();%0D%0A          %7D, function() %7B%0D%0A            deferred.reject();%0D%0A          %7D);%0D%0A%0D%0A          return deferred.promise
 ;%0D%0A     

Add verification on /publish/video/:videoId to verify that the video is found