(function (app) {
    app.factory('camera', ["$q", function ($q) {
        return {
            chooseImage: function (cameraOptions) {
                var deferred = $q.defer();
                var defaultCameraOptions = {
                    quality: 50,
                    sourceType: 1,
                    allowEdit: true,
                    encodingType: 0,//0为jpg,1为png
                    targetHeight: 112,
                    targetWidth: 112
                };
                var options = angular.extend(defaultCameraOptions, cameraOptions);
                var sheetOptions = {
                    title: '选择图片方式',
                    buttonLabels: ['拍摄照片', '从图库选择'],
                    addCancelButtonWithLabel: '取消',
                    androidEnableCancelButton: true
                };
                window.plugins.actionsheet.show(sheetOptions, function (index) {
                    switch (index) {
                        case 1: //拍摄图片
                            options.sourceType = 1;
                            break;
                        case 2://从图库选择
                            options.sourceType = 2;
                            break;
                        default:
                            deferred.resolve();
                            break;
                    }
                    navigator.camera.getPicture(function (imageUri) {
                        if (imageUri) {
                            deferred.resolve(imageUri);
                        } else {
                            deferred.reject('图片未找到');
                        }
                    }, function (error) {
                        deferred.reject(error);
                    }, options);
                });
                return deferred.promise;
            }
        };
    }]);
})(angular.module('app.services'));