/* 初始化设置页面 */
'use strict';

var angular = require('angular');
require('../modules');
require('../../appInitFlow/services/appInitFlow');
require('../../devicemanager/services/DeviceManager');
require('../../login/services/LoginService');
require('../../gesturepassword/services/GesturePasswordService');
require('../../initializeset/services/IntroPageService');
require('../../initializeset/services/SetUrlService');

angular
  .module('appconfig.services.appflowinit', ['appInitFlow.services',
  'devicemanager.services', 'login.services', 'initializeset.services', 'appVersionUpdate'])
  .factory('DefaultAppFlowInitService', DefaultAppFlowInitService);

/** @ngInject */
function DefaultAppFlowInitService(appInitFlowService, DeviceManager, LoginService,
  GesturePasswordService, $q, appVersionUpdate, IntroPageService, SetUrlService) {
  return {
    registerDefaultActions: function() {
      //0.9.23版本升级时，需要处理的升级问题
      appInitFlowService.addMessageGetter(function() {
        var isIOS = ionic.Platform.isIOS(),
          sameVersion = localStorage.appVersion == '0.9.22' ||
            localStorage.appVersion == '0.9.23',
          updated = localStorage['updateChange-v0.9.23'];
        if (isIOS && (!updated) && sameVersion) {
          localStorage['notices.version'] = '-1';
          localStorage['updateChange-v0.9.23'] = 'true';
        }
      });

      // 介绍页面
      appInitFlowService.setActionFn(appInitFlowService.INTRO_PAGE, IntroPageService.guidePage);
      // 设置登录地址
      appInitFlowService.setActionFn(appInitFlowService.SET_URL, SetUrlService.setLoginUrl);
      // app自动更新
      appInitFlowService.setActionFn(appInitFlowService.APP_VERSION_UPDATE, appVersionUpdate.versionUpdate);
      // 加载应用配置信息
      appInitFlowService.setActionFn(appInitFlowService.LOAD_APP_CONFIG, DeviceManager.sync);
      // 设备锁定节点的处理
      appInitFlowService.setActionFn(appInitFlowService.LOCK, DeviceManager.judgeLocked);
      // 设置身份认证节点
      appInitFlowService.setActionFn(appInitFlowService.AUTHENTICATE, LoginService.onLoginAction);
      // 判断设备绑定节点
      appInitFlowService.setActionFn(appInitFlowService.JUDGE_BIND, DeviceManager.judgeDeviceBind);
      // 设置设备手势密码验证节点的处理
      appInitFlowService.setActionFn(appInitFlowService.CHECK_DEVICE_SAFE, GesturePasswordService.judgeGesturePassword);
    },

    start: function() {
      appInitFlowService.start();
    }
  };
}
