// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.controllers' is found in controllers.js
angular.module('starter', ['ionic', 'starter.controllers'],
  function($httpProvider) {
    // Use x-www-form-urlencoded Content-Type
    $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
    // $httpProvider.defaults.headers.post['Access-Control-Allow-Origin'] = 'http://localhost:8100';
    var param = function(obj) {
      var query = '',
        name, value, fullSubName, subName, subValue, innerObj, i;

      for (name in obj) {
        value = obj[name];

        if (value instanceof Array) {
          for (i = 0; i < value.length; ++i) {
            subValue = value[i];
            fullSubName = name + '[' + i + ']';
            innerObj = {};
            innerObj[fullSubName] = subValue;
            query += param(innerObj) + '&';
          }
        } else if (value instanceof Object) {
          for (subName in value) {
            subValue = value[subName];
            fullSubName = name + '[' + subName + ']';
            innerObj = {};
            innerObj[fullSubName] = subValue;
            query += param(innerObj) + '&';
          }
        } else if (value !== undefined && value !== null)
          query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
      }

      return query.length ? query.substr(0, query.length - 1) : query;
    };

    $httpProvider.defaults.transformRequest = [function(data) {
      return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
    }];
  })

.run(function($ionicPlatform, $rootScope) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if (window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(false);
      cordova.plugins.Keyboard.disableScroll(true);

    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.hide();
    }

    if(window.localStorage.getItem('license')){
      $rootScope.check();
    }
  });

  $ionicPlatform.on("pause", function (event) {
    $rootScope.stopRefresh();
  });

  $ionicPlatform.on("resume", function (event) {
    $rootScope.startRefresh();
  });

  $ionicPlatform.registerBackButtonAction(function (event) {
    event.preventDefault();
  }, 100);
})

.config(function($stateProvider, $urlRouterProvider, $ionicConfigProvider) {
  $ionicConfigProvider.views.swipeBackEnabled(false);
  $ionicConfigProvider.tabs.position('top');

  $stateProvider

  .state('login', {
    url: '/login',
    templateUrl: 'templates/login.html',
    controller: 'LoginCtrl'
  })

  .state('masalar', {
    url: '/masalar',
    templateUrl: 'templates/masalar.html',
    controller: 'MasalarCtrl'
  })

  .state('masa-detay', {
    url: '/masa-detay/:hall/:table',
    templateUrl: 'templates/masa-detay.html',
    controller: 'MasaDetayCtrl'
  })

  .state('siparis', {
    url: '/siparis/:hall/:table',
    templateUrl: 'templates/siparis.html',
    controller: 'SiparisCtrl'
  });
  // if none of the above states are matched, use this as the fallback
  if(window.localStorage.getItem('waiter'))
    $urlRouterProvider.otherwise('/masalar');
  else
    $urlRouterProvider.otherwise('/login');
});
