'use strict';

var angular = require('angular');

require('../modules');

var module = angular.module('appInitFlow.services');

module.factory('appInitFlowService', function($rootScope, $q, $state, $window) {
  var isDevelop = $window.location.search &&
    $window.location.search.indexOf('develop') > 0;
  var appInitFlow = {}, mapFn = {}, messageGetters = [];

  appInitFlow.MESSAGE_INIT = 'messageInit';
  //appInitFlow.INITIALIZE_SET = 'initializeSet'; //  app初始化设置
  appInitFlow.INTRO_PAGE = 'introPage'; // 进入应用引导页
  appInitFlow.SET_URL = 'setUrl'; // 省份选择
  appInitFlow.APP_VERSION_UPDATE = 'appVersionUpdate';// 版本更新
  appInitFlow.LOAD_APP_CONFIG = 'loadAppConfig';//加载应用配置节点
  appInitFlow.LOCK= 'lock';//判定设备锁定节点
  appInitFlow.AUTHENTICATE = 'authenticate';//身份认证节点
  appInitFlow.JUDGE_BIND = 'judgeBind';//判断设备绑定节点
  appInitFlow.CHECK_DEVICE_SAFE = 'checkDeviceSafe';//检测设备安全性节点

  appInitFlow.initing = false;
  appInitFlow.inited = false;

  mapFn[appInitFlow.MESSAGE_INIT] = function() {
    return $q.all(messageGetters.map(function(getter) {
      return getter();
    }))
    .then(function() {
      appInitFlow.nextAction(appInitFlow.MESSAGE_INIT);
    }, function() {
      //获取配置信息失败
    });
  };

  /**
   * 产生默认的流程节点动作实现。
   *
   * @param  {String} actionName
   * @return {Function}
   */
  function generateActionFn(actionName) {
    return function() {
      return appInitFlow.nextAction(actionName);
    };
  }

  var notInBorwserAppActions = [];
  appInitFlow.setActionFn = function(actionName, fn) {
    var inApp = !!window.cordova;
    if (!inApp && notInBorwserAppActions.indexOf(actionName) !== -1) {
      mapFn[actionName] = generateActionFn(actionName);
    } else {
      mapFn[actionName] = fn;
    }
  };

  /**
   * 开始app初始化流程。
   *
   * @return {Boolean} 返回是否开始流程成功的状态。如果流程之前已开始，则返回false。
   */
  appInitFlow.start = function() {
    if (!appInitFlow.initing && !appInitFlow.inited) {
      appInitFlow.initing = true;
      mapFn[appInitFlow.MESSAGE_INIT]();
      //TODO show 初始化页面
      return true;
    }

    return false;
  };

  /**
   * 初始化需要的所有基本信息，以便流程中的节点使用
   * @param {Function} fn
   */
  appInitFlow.addMessageGetter = function(fn) {
    messageGetters.push(fn);
  };

  /**
   * 进入app。
   *
   */
  appInitFlow.goToApp = function() {
    $state.go('core-notice');
  };

  appInitFlow.nextAction = function(actionName, state) {
    var next = null;
    appInitFlow.initing = true;
    /** acitonName阶段已经成功 */
    $rootScope.$broadcast('actionSuccess', actionName);
    switch (actionName) {
      case appInitFlow.MESSAGE_INIT:
        next = appInitFlow.INTRO_PAGE;
        break;
      case appInitFlow.INTRO_PAGE:
        next = appInitFlow.SET_URL;
        break;
      case appInitFlow.SET_URL:
        next = appInitFlow.APP_VERSION_UPDATE;
        break;
      case appInitFlow.APP_VERSION_UPDATE:
        next = appInitFlow.LOAD_APP_CONFIG;
        break;
      case appInitFlow.LOAD_APP_CONFIG:
        next = appInitFlow.LOCK;
        break;
      case appInitFlow.LOCK:
        next = appInitFlow.AUTHENTICATE;
        break;
      case appInitFlow.AUTHENTICATE:
        if(isDevelop) {
          finish();
        } else {
          next = appInitFlow.JUDGE_BIND;
        }
        break;
      case appInitFlow.JUDGE_BIND:
        next = appInitFlow.CHECK_DEVICE_SAFE;
        break;
      case appInitFlow.CHECK_DEVICE_SAFE:
        finish();
        return;
    }

    function finish() {
      appInitFlow.initing = false;
      appInitFlow.inited = true;
      appInitFlow.goToApp();
    }

    if (next) {
      if (mapFn[next]) {
        mapFn[next](function(state) {
          appInitFlow.nextAction(next, state);
        }, state);
      } else {
        appInitFlow.nextAction(next);
      }
    }
  };

  return appInitFlow;

});
