'use strict';

var picard = angular.module('picard', ['ngRoute', 'ngCookies', 'picard.directives', 'picard.filters', 'picard.services', 'ui.bootstrap', 'chieffancypants.loadingBar'])
    .config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
        $routeProvider.
            when('/', {templateUrl: '/partials/home.html', controller: HomeCtrl}).
            when('/Login', {templateUrl: '/partials/login.html', controller: LoginCtrl}).
            when('/Logout', {templateUrl: '/partials/home.html', controller: LogoutCtrl}).

            when('/Taxes', {templateUrl: '/partials/tax-list.html', controller: TaxListCtrl, resolve: TaxListCtrl.resolve}).
            when('/Tax', {templateUrl: '/partials/tax-detail.html', controller: TaxCtrl, resolve: TaxCtrl.resolve}).
            when('/Tax/:id', {templateUrl: '/partials/tax-detail.html', controller: TaxCtrl, resolve: TaxCtrl.resolve}).

            when('/ProductGroups', {templateUrl: '/partials/product-group-list.html', controller: ProductGroupListCtrl, resolve: ProductGroupListCtrl.resolve}).
            when('/ProductGroup', {templateUrl: '/partials/product-group-detail.html', controller: ProductGroupCtrl, resolve: ProductGroupCtrl.resolve}).
            when('/ProductGroup/:id', {templateUrl: '/partials/product-group-detail.html', controller: ProductGroupCtrl, resolve: ProductGroupCtrl.resolve}).

            when('/Products', {templateUrl: '/partials/product-list.html', controller: ProductListCtrl, resolve: ProductListCtrl.resolve}).
            when('/Product', {templateUrl: '/partials/product-detail.html', controller: ProductCtrl, resolve: ProductCtrl.resolve}).
            when('/Product/:id', {templateUrl: '/partials/product-detail.html', controller: ProductCtrl, resolve: ProductCtrl.resolve}).

            when('/FoodTables', {templateUrl: '/partials/food-table-list.html', controller: FoodTableListCtrl, resolve: FoodTableListCtrl.resolve}).
            when('/FoodTable', {templateUrl: '/partials/food-table-detail.html', controller: FoodTableCtrl, resolve: FoodTableCtrl.resolve}).
            when('/FoodTable/:id', {templateUrl: '/partials/food-table-detail.html', controller: FoodTableCtrl, resolve: FoodTableCtrl.resolve}).

            when('/Modifiers', {templateUrl: '/partials/modifier-list.html', controller: ModifierListCtrl, resolve: ModifierListCtrl.resolve}).
            when('/Modifier', {templateUrl: '/partials/modifier-detail.html', controller: ModifierCtrl, resolve: ModifierCtrl.resolve}).
            when('/Modifier/:id', {templateUrl: '/partials/modifier-detail.html', controller: ModifierCtrl, resolve: ModifierCtrl.resolve}).

            when('/Tables', {templateUrl: '/partials/table-list.html', controller: TableListCtrl, resolve: TableListCtrl.resolve}).
            when('/Voucher', {templateUrl: '/partials/voucher.html', controller: VoucherCtrl, resolve: VoucherCtrl.resolve}).
            when('/Voucher/:id', {templateUrl: '/partials/voucher.html', controller: VoucherCtrl, resolve: VoucherCtrl.resolve}).

            otherwise({templateUrl: '/partials/404.html'});
        $locationProvider.html5Mode(true).hashPrefix('!');
    }])
    .config(['$httpProvider', function ($httpProvider) {
        $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
    }])
    .config(['$httpProvider', function ($httpProvider) {
        $httpProvider.interceptors.push('responseInterceptor');
    }])
    .factory('responseInterceptor', ['$q', '$rootScope', function ($q, $rootScope) {
        return {
            response: function (response) {
                return response;
            },
            responseError: function (response) {
                var headers = response.headers(),
                    isResourceResponse = headers['content-type'] === "application/json; charset=utf-8",
                    status = response.status;
                if (status === 401 && !isResourceResponse) {
                    $rootScope.$broadcast('loginRequired');
                }
                // otherwise
                return $q.reject(response);
            }
        };
    }]);


