var angular = require('angular');
require('../modules');
'use strict';

angular
  .module('setting')
  .controller('VersionCtrl', VersionCtrl);

/** @ngInjetct */
function VersionCtrl($scope, sinoNetwork, appVersionUpdate, $ionicActionSheet, $timeout) {

  $scope.getAppVersion = function() {
    $scope.versionLoading = true;
    $timeout(function() {
      sinoNetwork.getAppVersion().then(function(appVersion) {
        $scope.appVersion = appVersion;
        $scope.updateFlag = $scope.appVersion.needUpdate ||
        $scope.appVersion.needInstall;
      }).then(function() {
        $scope.versionLoading = false;
        if (!$scope.updateFlag) {
          return;
        }
        $ionicActionSheet.show({
          title: '更新版本',
          buttons: [{
            text: '最新版本是' + $scope.appVersion.appVersion + ',点击更新'
          }],
          cancelText: '取消',
          cancel: function() {
            return true;
          },
          buttonClicked: function() {
            $scope.versionUpdate();
            return true;
          }
        });
      });
    }, 1000);
  };

  $scope.versionUpdate = function() {
    if ($scope.appVersion.needInstall) {
      appVersionUpdate.installApp($scope.appVersion.appVersion);
    } else {
      sinoNetwork.versionUpdate();
    }
  };
}
