class CapturePhoto {
    constructor(){
        this.options = {
            // Some common settings are 20, 50, and 100
            quality: 100,
            destinationType: Camera.DestinationType.FILE_URI,
            sourceType: Camera.PictureSourceType.CAMERA,
            encodingType: Camera.EncodingType.JPEG,
            mediaType: Camera.MediaType.PICTURE,
            allowEdit: false,
            targetHeight:500,
            targetWidth: 500,
            correctOrientation: true
        };

        document.addEventListener('resume', function (evt) {
            window.plugin.backgroundMode.disable();
        });
    }

    static fixStatusBarToIos(){
        if (device.platform === "iOS") {
            StatusBar.hide();
            StatusBar.show();
        }
    }

    get(type = Camera.PictureSourceType.CAMERA){
        this.options.sourceType = type;

        if(type !== Camera.PictureSourceType.CAMERA) {
            return new Promise((resolve, reject) => {
                window.plugin.backgroundMode.enable();
                navigator.camera.getPicture((result) => {
                    CapturePhoto.fixStatusBarToIos();
                    FileUpload.getFile(result).then(file => {
                        resolve(file)
                    });
                },(error)=>{
                    CapturePhoto.fixStatusBarToIos();
                    reject(error);
                }, this.options);
            });
        }

        return new Promise((resolve) => {
            window.plugin.backgroundMode.enable();
            navigator.device.capture.captureImage((result) => {
                CapturePhoto.fixStatusBarToIos();
                FileUpload.getFile(result[0].fullPath).then(file => {
                    resolve(file)
                });
            },(error) => {
                CapturePhoto.fixStatusBarToIos();
                console.debug("Unable to obtain picture: " + error, "app");
            },);
        });
    }
}
