// 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'
angular.module('app', ['ionic',
  'ngCordova',
  'ionic-material',
  'ngResource',
  'app.controllers',
  'app.services',
  'app.directives',
  'ionic.wizard',
  'pascalprecht.translate',
  "Scope.safeApply",
  "ionic-lock-screen"])
  .config(function($ionicConfigProvider,$translateProvider) {
    if(ionic.Platform.isAndroid())
      $ionicConfigProvider.scrolling.jsScrolling(false);

      $translateProvider.translations('en', configData.lang_en);
      $translateProvider.translations('vn', configData.lang_vn);
      $translateProvider.preferredLanguage('en');

  })
.run(function($ionicPlatform,dataPersistenceService,$cordovaStatusbar ,initService,ModalViews) {
  $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(true);
      cordova.plugins.Keyboard.disableScroll(false);
    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }

    IonicDeploy.extract("input_app" ,function () {
      console.log("Extracted successfully !");
    },function () {
      console.log("Extracted faliure !");
    })

    $cordovaStatusbar.hide();
    initService.init();
    });
  })
  .config(function ($stateProvider, $urlRouterProvider) {
    $stateProvider
      .state('app', {
        url: '/app',
        abstract: true,
        templateUrl: 'templates/menu.html',
        controller: 'AppCtrl as appctrl'
      })
      .state('login', {
        url: "/login",
        templateUrl: 'templates/login.html',
        controller: 'LoginCtrl as loginctrl'
      })
      .state('app.home', {
        url: "/home",
        views: {
          'menuContent': {
            templateUrl: 'templates/home.html',
            controller: 'HomeCtrl as homectrl'
          },
          'fabContent': {
            template: ' <button class="button button-energized button-fab ink button-fab-bottom-left button-flt" ng-mobile-click="homectrl.undo()"><i class="icon ion-reply"></i> </button>',
            controller: 'HomeCtrl as homectrl'
          }
        }
      })
      .state('app.defects', {
        url: "/defects",
        views: {
          'menuContent': {
            templateUrl: 'templates/defects.html',
            controller: 'DefectsCtrl as operationsctrl'
          },
          'fabContent': {
            template: ''
          }
        },
        params: { step:-1 }
      }).state('app.opissues', {
        url: "/opissues",
        views: {
          'menuContent': {
            templateUrl: 'templates/ops_issues.html',
            controller: 'OpsIssuesCtrl as opsissuesctrl'
          },
          'fabContent': {
            template: ''
          }
        },
        params: { issuedata:{} }
      }).state('app.issues', {
        url: "/issues",
        views: {
          'menuContent': {
            templateUrl: 'templates/issues.html',
            controller: 'IssuesCtrl as issuesctrl'
          },
          'fabContent': {
            template: ' <button class="button button-positive button-fab ink button-fab-bottom-right button-flt" ng-mobile-click="issuesctrl.save()">Done</button>',
            controller: 'IssuesCtrl as issuesctrl'
          }
        }
      }).state('app.rework', {
        url: "/rework",
        views: {
          'menuContent': {
            templateUrl: 'templates/rework.html',
            controller: 'ReworkCtrl as reworkctrl'
          },
          'fabContent': {
            template: ''
          }
        }
      }).state('app.admin', {
        url: "/admin",
        views: {
          'menuContent': {
            templateUrl: 'templates/admin.html',
            controller: "AdminCtrl as adminctrl"
          },
          'fabContent': {
            template: ''
          }
        }
      })
      .state('app.system', {
        url: "/system",
        views: {
          'menuContent': {
            templateUrl: 'templates/system.html',
            controller: "SystemCtrl as systemctrl"
          }
        }
      }).state('app.measles', {
        url: "/measles",
        cache:false,
        views: {
          'menuContent': {
            templateUrl: 'templates/measles.html',
            controller: "MeaslesCtrl as measlesctrl"
          }
        },
        params: { defect:{} }
      }).state('app.reject', {
        url: "/reject",
        views: {
          'menuContent': {
            templateUrl: 'templates/defects.html',
            controller: 'RejectsCtrl as operationsctrl'
          },
          'fabContent': {
            template: ''
          }
        }
      }).state('app.oprejects', {
        url: "/oprejects",
        views: {
          'menuContent': {
            templateUrl: 'templates/ops_rejects.html',
            controller: 'OpsRejectsCtrl as opsrejectsctrl'
          },
          'fabContent': {
            template: ''
          }
        }
      }).state('app.advance_behind', {
        url: "/advance_behind_styles",
        views: {
          'menuContent': {
            templateUrl: 'templates/add_plan_advance_behind_master_data.html',
            controller: 'AddPlanAdvanceBehindMasterDataCtrl as addplanadvancebehindmasterdatactrl'
          },
          'fabContent': {
            template: ''
          }
        }
      }).state('app.reject_sizewise', {
      url: "/reject_size_wise",
      views: {
        'menuContent': {
          templateUrl: 'templates/reject_size_wise_capturing.html',
          controller: 'RejectsSizeWiseCtrl as rejectssizewisectrl'
        },
        'fabContent': {
          template: ''
        }
      }
    });
    // if none of the above states are matched, use this as the fallback
    $urlRouterProvider.otherwise('/login');
  });
angular.module('Scope.safeApply', []).run(function($rootScope) {

  $rootScope.$safeApply = function() {
    var $scope, fn, force = false;
    if(arguments.length == 1) {
      var arg = arguments[0];
      if(typeof arg == 'function') {
        fn = arg;
      }
      else {
        $scope = arg;
      }
    }
    else {
      $scope = arguments[0];
      fn = arguments[1];
      if(arguments.length == 3) {
        force = !!arguments[2];
      }
    }
    $scope = $scope || this;
    fn = fn || function() { };
    if(force || !$scope.$$phase) {
      $scope.$apply ? $scope.$apply(fn) : $scope.apply(fn);
    }
    else {
      fn();
    }
  };

});
