'use strict';

angular.module('bestInBlackApp', [
  'ngCookies',
  'ngResource',
  'ngSanitize',
  'ngTouch',
  'btford.socket-io',
  'ui.router',
  'ui.bootstrap',
  //BIB
  'ngMaterial',
  'angular-loading-bar',
  'bib-carousel',
  //TAGS
  'tagger',
  //UPLOAD
  'angularFileUpload',
  //TRANSITION
  'hj.gsapifyRouter',
  //VIDEOGULAR
  'com.2fdevs.videogular',
  'com.2fdevs.videogular.plugins.controls',
  'com.2fdevs.videogular.plugins.overlayplay',
  'com.2fdevs.videogular.plugins.buffering',
  'com.2fdevs.videogular.plugins.poster',
  'info.vietnamcode.nampnq.videogular.plugins.youtube',
  'com.benjipott.videogular.plugins.chromecast',
  //VIDEOGULAR
  'com.2fdevs.videogular',
  'com.2fdevs.videogular.plugins.controls',
  'com.2fdevs.videogular.plugins.overlayplay',
  'com.2fdevs.videogular.plugins.buffering',
  'com.2fdevs.videogular.plugins.poster',
  'info.vietnamcode.nampnq.videogular.plugins.youtube',
  'com.digibos.videogular.plugins.hidden',
  'com.2fdevs.videogular.plugins.dash',
  //LAZY LOAD
  'afkl.lazyImage',
  //INFINITE SCROLLING
  'infinite-scroll'
])
  .config(function ($mdThemingProvider) {
    $mdThemingProvider.definePalette('bibPalette', {
      '50': 'ffebee',
      '100': 'ffcdd2',
      '200': 'ef9a9a',
      '300': 'e57373',
      '400': 'ef5350',
      '500': 'f44336',
      '600': 'e53935',
      '700': 'd32f2f',
      '800': 'c62828',
      '900': 'b71c1c',
      'A100': 'ff8a80',
      'A200': 'ff5252',
      'A400': 'ff1744',
      'A700': 'd50000',
      'contrastDefaultColor': 'light',    // whether, by default, text (contrast)
                                          // on this palette should be dark or light
      'contrastDarkColors': ['50', '100', //hues which contrast should be 'dark' by default
        '200', '300', '400', 'A100'],
      'contrastLightColors': undefined    // could also specify this if default was 'dark'
    });

    $mdThemingProvider.theme('bib')
      .primaryPalette('bibPalette')
      .accentPalette('orange');
  })
  .config(['$stateProvider', '$urlRouterProvider', '$locationProvider', '$httpProvider', 'gsapifyRouterProvider', function ($stateProvider, $urlRouterProvider, $locationProvider, $httpProvider, gsapifyRouterProvider) {
    $urlRouterProvider
      .otherwise('/');

    $locationProvider.html5Mode(true);
    $httpProvider.interceptors.push('authInterceptor');

    //TRANSITION MANAGER
    // Set default transitions to use if unspecified
    gsapifyRouterProvider.defaults = {

      // name of transition to use or 'none'
      enter: 'fade',

      leave: 'none'

    };

    // Add a new transition
    gsapifyRouterProvider.transition('slideToFromRight', {

      // transition duration
      duration: 0.5,

      // transition delay
      delay: 0,

      // start/end point for transition (eg. off screen)
      css: {
        x: '100%'
      }

    });

    // Add a new transition
    gsapifyRouterProvider.transition('slideToFromLeft', {

      // transition duration
      duration: 0.5,

      // transition delay
      delay: 0,

      // start/end point for transition (eg. off screen)
      css: {
        x: '-100%'
      }

    });
  }])

  .factory('authInterceptor', ['$rootScope', '$q', '$cookieStore', '$location', function ($rootScope, $q, $cookieStore, $location) {
    return {
      // Add authorization token to headers
      request: function (config) {
        config.headers = config.headers || {};
        if ($cookieStore.get('token')) {
          config.headers.Authorization = 'Bearer ' + $cookieStore.get('token');
        }
        return config;
      },

      // Intercept 401s and redirect you to login
      responseError: function (response) {
        if (response.status === 401) {
          $location.path('/login');
          // remove any stale tokens
          $cookieStore.remove('token');
          return $q.reject(response);
        }
        else {
          return $q.reject(response);
        }
      }
    };
  }])

  .run(['$rootScope', '$location', '$state', '$anchorScroll', 'Auth', function ($rootScope, $location, $state, $anchorScroll, Auth) {
    // Redirect to login if route requires auth and you're not logged in
    $rootScope.$on('$stateChangeSuccess', function (event, toState, toParams, fromState) {
      $state.previous = fromState;
    });
    $rootScope.$on('$stateChangeStart', function (event, next) {
      //$anchorScroll();
      Auth.checkAuth(next);
    });
  }]);
