(function () {
    angular.module('app', ['ionic', 'app.controllers', 'app.services', 'app.filters', 'app.directives', 'ionic-citypicker'])
   .config(["$ionicConfigProvider", function ($ionicConfigProvider) {
       //ionic全局配置
       $ionicConfigProvider.views.maxCache(10);
       $ionicConfigProvider.platform.android.tabs.position('bottom');
       $ionicConfigProvider.platform.android.navBar.alignTitle("center");
       $ionicConfigProvider.backButton.text('后退').icon('ion-chevron-left');
       $ionicConfigProvider.form.checkbox("circle");
       $ionicConfigProvider.templates.maxPrefetch(0);
       $ionicConfigProvider.scrolling.jsScrolling(true);
   }]).factory('myHttpInterceptor', ["$q", 'data', function ($q, data) {  //$http拦截器定义
       var initInjector = angular.injector(['ng']);
       var $http = initInjector.get('$http');
       return {
           'request': function (config) {
               return config;
           },
           'requestError': function (rejection) {
               return $q.reject(rejection);
           },
           'response': function (response) {
                   return response;
           },
           'responseError': function (rejection) {
               if (rejection.status == 401) {
                   data.remove('user');
                   data.remove('token');
                   $http.defaults.headers.common["Authorization"] = undefined;
                   window.location.href = '#/account/login';
                   return $q.reject('');
               }
               return $q.reject(rejection);
           }
       }
   }])
 .config(["$httpProvider", "$windowProvider", function ($httpProvider, $windowProvider) {
     var win = $windowProvider.$get();
     var token=win.localStorage['token'];
     if (token) {
         $httpProvider.defaults.headers.common['Authorization'] = 'Bearer ' + token;
     }
     $httpProvider.interceptors.push('myHttpInterceptor');
     
 }])
.run(["$ionicPlatform", "$location", "msg", "$rootScope", "$timeout", "$ionicHistory", "userService", "$state", "$http", "map",
    function ($ionicPlatform, $location, msg, $rootScope, $timeout, $ionicHistory, userService, $state, $http, map) {
    $ionicPlatform.registerBackButtonAction(function (e) {
        if ($location.path().indexOf("wl/") != -1) {
            if ($rootScope.backButtonPressedOnceToExit) {
                ionic.Platform.exitApp();
                $rootScope.$destroy()
            } else {
                $rootScope.backButtonPressedOnceToExit = true;
                msg.text("再按一次退出系统",true);
                $timeout(function () {
                    $rootScope.backButtonPressedOnceToExit = false
                }, 3000)
            }
        } else if ($ionicHistory.backView()) {
            $ionicHistory.goBack()
        }
        $ionicPlatform.preventDefault();
        $ionicPlatform.stopPropagation();
        return false
    }, 101);
    $ionicPlatform.ready(function () {
        if (window.cordova && cordova.platformId === 'ios' && window.cordova.plugins && window.cordova.plugins.Keyboard) {
            cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
            cordova.plugins.Keyboard.disableScroll(true);
        }
        if (window.StatusBar) {
            StatusBar.styleDefault();
        }
   
        if (!$rootScope.location) {
            map.getLocation().then(function (result) {
                $rootScope.location = result;
            });
        }
        if (!userService.isLogin()) {
            $state.go('login');
        }
    });
}]);
 angular.module("app.controllers", []);
 angular.module("app.services", []);
 angular.module("app.filters", []);
 angular.module('app.directives', []);
})();
