var angular = require('angular');
require('../modules');
require('./sinoNetwork');
require('../directives/$toast');
require('uiRouter');

 var module = angular.module('appVersionUpdate');

module.run(function($rootScope, sinoNetwork, appVersionUpdate, WebSocketConfig) {

  $rootScope.$on('resume', function() {
    appVersionUpdate.versionUpdate();
  });

  $rootScope.$on('server.online', function() {
    $rootScope.connected = true;
    appVersionUpdate.versionUpdate();
  });

  WebSocketConfig.registerHandler('versionUpdate', appVersionUpdate.update);

});

module.provider('downloadConfig', function() {
  this.appName = '';

  this.setAppName = function(appName) {
    this.appName = appName;
  };

  this.$get = [function() {
    return this;
  }];
});

module.factory('appVersionUpdate', function($rootScope, sinoNetwork, $toast, $http,
  $state, CommonService, downloadConfig, $window, $ionicPopup) {
  var updating = false; // 更新状态
  var installAppDialogOpening = false;

  return {
    versionUpdate: versionUpdate,
    update: versionUpdate,
    installApp: installApp
  };

  /**
   * 下载app
   */
  function installApp(appVersion) {
    if (ionic.Platform.isAndroid() && window.SinoNetwork && window.SinoNetwork.installApp) {
      window.SinoNetwork.installApp(false);
      return;
    }

    if (installAppDialogOpening) {
        return;
    }
    installAppDialogOpening = true;
    $ionicPopup.confirm({
      title: '移动办公安装',
      template: '有' + appVersion + '新版本需要更新升级，请点击确定按钮，完成安装',
      okText: '确定',
      cancelText: '取消'
    }).then(function(res) {
      installAppDialogOpening = false;
      if (res) {
        install();
      }
    }, function() {
      installAppDialogOpening = false;
    });
  }

  function install() {
    if (ionic.Platform.isAndroid() && window.SinoNetwork && window.SinoNetwork.installApp) {
      window.SinoNetwork.installApp(true);
      return;
    }

    var installUrl;
    var baseUrl = 'http://112.126.73.50/appdownload',
      osType = ($window.device.platform === 'Android') ? 'android' : 'ios',
      appName = downloadConfig.appName;
    installUrl = baseUrl + '?ostype=' + osType + '&dept=' + appName;

    if (ionic.Platform.isIOS() && window.SinoNetwork && window.SinoNetwork.toCoreMail) {
      cordova.exec(function() {}, function() {}, "SinoNetwork", "toCoreMail", [installUrl, '下载ios app', '返回']);
    } else {
      window.open(installUrl, '_system');
    }
  }

  function versionUpdate(nextAction) {
    if ($rootScope.connected) {
      sinoNetwork.getAppVersion()
      .then(function(appVersion) {
        if (!appVersion) {
            return;
        }
        window.localStorage.appVersion = appVersion.version;
        if (appVersion.needUpdate) {
            updating = true;
            return sinoNetwork.versionUpdate().then(function() {
            updating = false;
            });
        } else if (appVersion.needInstall) {
            installApp(appVersion.appVersion);
        }
      });
    }
    
    nextAction = nextAction ? nextAction : function() {};
    nextAction();
  }
});
