import uirouter from 'angular-ui-router';
import appConfig from './app.config';
import envConfig from './env.config';
import login from './features/login';
import reviews from './features/reviews';
import catalog from './features/catalog';
import insights from './features/insights';
import tags from './features/tags';
import linked from './features/linked';
import keywordtag from './features/keywordtag';
import jobs from './features/jobs';
import fileImport from './features/file-import';
import productReviewTitleSku from './features/product-review-title-sku';
import badgeSetup from './components/badgeSetup';
import overallInsightsEditor from './components/overall-insights-editor';

// 1.5 components
import card from './components/card';
import navbar from './components/navbar';
import importer from './components/importer';
import manageReviewTitles from './components/manage-review-titles';
import manageSkus from './components/manage-skus';

const MODULE_NAME = 'app';

angular.module(MODULE_NAME, [
    uirouter,
    login, reviews, catalog, insights, tags, linked, keywordtag, jobs, fileImport, productReviewTitleSku,
    card, navbar, importer, manageReviewTitles, manageSkus,
    'EnterpriseDashboardApp.HostFactory',
    'EnterpriseDashboardApp.ProductResource',
    'EnterpriseDashboardApp.TextReview',
    'EnterpriseDashboardApp.DataCalculator',
    'EnterpriseDashboardApp.ErrorMailer',
    'ui.bootstrap',
    'ReviewsUtils',
    'EnterpriseDashboardApp.HtmlKeywordsUnderliner',
	'angular-notify',
    'dndLists',
    'angular-click-outside',
    'EnterpriseDashboardApp.RealtimeProductResource',
    'EnterpriseDashboardApp.RealtimeApi',
    'EnterpriseDashboardApp.WS',
    'EnterpriseDashboardApp.RestApi',
    'EnterpriseDashboardApp.RealtimeUserResourceForAdministrator',
    'EnterpriseDashboardApp.Keyword2tagService',
	'environment',
    'ui.select',
    'vs-repeat',
    badgeSetup,
    overallInsightsEditor

]);

angular.module(MODULE_NAME)
    .config(envConfig)
    .config(appConfig)
    .run(function(Authentication, $log, $templateCache) {


        $log.info = console.log.bind(console);
        var url = './components/assign-reviews/assign-reviews.html';
        $templateCache.put(url, require(url));
        
        //
        // Authentication.checkAuthorization();
    })
    .factory('authInterceptor', function($rootScope, $q, $window) {
        return {
            request: function(config) {
                var token;
                config.headers = config.headers || {};

                // token = $localStorage.token;
                token = $window.sessionStorage.token;
                // console.log('BEFORE REQUEST: token', token)
                if (token) {
                    config.headers.Authorization = 'Bearer ' + token;
                }
                return config;
            },
            responseError: function(response) {
                console.log('in resposeError', response.status, response);
                if (response.status === 401) {

                    // handle the case where the user is not authenticated
                    // $window.location.href = '/login';
                    // return $q.reject(response);
                }
                return $q.reject(response);
            }
        };
    })
    .config(function($httpProvider) {
        $httpProvider.interceptors.push('authInterceptor');
    });



export default MODULE_NAME;