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

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

/** @ngInjetct */
function UserinfoCtrl($scope, $ionicLoading, $ionicActionSheet, $toast,
  LinkerService, PersonImgCache, fileService, CoreAppConfig, SettingService) {

  getLoginUser();

  $scope.$on('$ionicView.beforeEnter', function() {
    getLoginUser();
  });

  $scope.$on('$ionicView.enter', function() {
    getLoginUser();
  });

  /**
   * 获取当前登录人信息。
   */
  function getLoginUser() {
    var user = LinkerService.currentUser,
      userId = LinkerService.currentUserId;
    $scope.loginUser = user || {};
  }

  /**
   * 选择用户图片。
   */
  $scope.changePhoto = function() {
    $ionicActionSheet.show({
      title: '选取照片',
      buttons: [{
        text: '<i class="icon ion-camera sino-camera-icon"></i>拍照'
      }, {
        text: '<i class="icon ion-image sino-img-icon"></i>从相册中选取'
      }, {
        text:'<i class="icon ion-trash-a sino-head-portrait-delete-icon"></i>移除头像'
      }],
      cancelText: '取消',
      cancel: function() {
        return true;
      },
      buttonClicked: function(index) {
        if (index === 0) {
          $scope.takePicture();
        } else if (index === 1) {
          $scope.choosePicture();
        } else if (index === 2) {
          $scope.removePictrue();
        }
        return true;
      }
    });
  };

  /**
   * 删除用户头像
   */
  $scope.removePictrue = function() {
    SettingService.removeUserPicture($scope.loginUser.userId).then(function(status) {
      if(status) {
        PersonImgCache.syncPersonImgUrl($scope.loginUser.userId);
      }
    });
  };

  /**
   * 上传图片成功的回调。
   */
  var uploadImageSuccess = function() {
    PersonImgCache.syncPersonImgUrl($scope.loginUser.userId);
    $ionicLoading.hide();
  };
  /**
   * 上传图片失败的回调。
   */
  var uploadFileFail = function() {
    $toast.showLongCenter('上传用户图片失败，请检查网络连接。');
  };
  /**
   * 获取图片成功的回调。
   * @param  {String} fileURL
   */
  $scope.onGetPictureSuccess = function(fileURL) {
    var options = {
      mimeType: 'image/jpeg',
      fileName: fileURL && fileURL.substr(fileURL.lastIndexOf('/') + 1) + '.jpg'
    };
    $toast.showLongCenter('正在上传头像..');
    fileService.upload(fileURL, CoreAppConfig.serverUrl + '/base-user/user/' + $scope.loginUser.userId + '/photo', options).then(uploadImageSuccess, uploadFileFail);
  };
  /**
   * 获取图片失败的回调。
   */
  $scope.onGetPictureFail = function() {

  };

  /**
   * 拍照。
   */
  $scope.takePicture = function() {
    navigator.camera.getPicture($scope.onGetPictureSuccess, $scope.onGetPictureFail, {
      quality: 100,
      cameraDirection: Camera.Direction.FRONT,
      destinationType: Camera.DestinationType.FILE_URI,
      saveToPhotoAlbum: true
    });
  };
  /**
   * 选取现有照片。
   */
  $scope.choosePicture = function() {
    navigator.camera.getPicture($scope.onGetPictureSuccess, $scope.onGetPictureFail, {
      quality: 100,
      allowEdit: true,
      destinationType: Camera.DestinationType.FILE_URI,
      sourceType: Camera.PictureSourceType.SAVEDPHOTOALBUM
    });
  };
}

