package com.camerakit;

import android.content.Context;
import android.media.MediaScannerConnection;
import android.os.Environment;

import java.io.File;
import java.io.FileOutputStream;

public class Photo extends CameraProduct {

    private byte[] mJpeg;
    private Throwable mError;

    private CameraPending<PhotoJpeg> mPendingJpeg;

    public Photo(Context context) {
        super(context);
    }

    void set(byte[] jpeg) {
        mJpeg = jpeg;
        if (mPendingJpeg != null) {
            mPendingJpeg.set(new PhotoJpeg(mContext, mJpeg));
        }
    }

    void set(Throwable error) {
        mError = error;
    }

    public CameraPending<PhotoJpeg> toJpeg() {
        mPendingJpeg = new CameraPending<>();

        if (mJpeg != null) {
            mPendingJpeg.set(new PhotoJpeg(mContext, mJpeg));
        }

        return mPendingJpeg;
    }

    public CameraPending<PhotoFile> toGalleryFile() {
        String label = mContext.getApplicationInfo().loadLabel(mContext.getPackageManager()).toString();
        return toGalleryFile(label);
    }

    public CameraPending<PhotoFile> toGalleryFile(String folderName) {
        return toGalleryFile(folderName, System.currentTimeMillis() + ".jpg");
    }

    public CameraPending<PhotoFile> toGalleryFile(String folderName, String fileName) {
        CameraPending<PhotoFile> output = new CameraPending<>();

        toJpeg().whenReady(jpeg -> {
            output.run(pending -> {
                File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES + File.separator + folderName);

                if (!directory.isDirectory()) {
                    directory.mkdirs();
                }

                File imageFile = new File(directory, fileName + ".png");
                FileOutputStream out = new FileOutputStream(imageFile);
                out.write(jpeg.getBytes());
                out.flush();
                out.close();

                MediaScannerConnection.scanFile(mContext, new String[]{imageFile.getAbsolutePath()}, null, (path, uri) -> {
                    pending.set(new PhotoFile(mContext, imageFile));
                });
            });
        });

        return output;
    }


}

@@ -671,21 +671,26 @@
 %3E toJpeg
+Bytes
 () %7B%0A
-
         
@@ -858,32 +858,1072 @@
 ingJpeg;%0A    %7D%0A%0A
+    public CameraPending%3CPhotoFile%3E toFile() %7B%0A        String label = %22camerakit%22;%0A        return toGalleryFile(label);%0A    %7D%0A%0A    public CameraPending%3CPhotoFile%3E toFile(String folderName) %7B%0A        return toGalleryFile(folderName, System.currentTimeMillis() + %22.jpg%22);%0A    %7D%0A%0A    public CameraPending%3CPhotoFile%3E toFile(String folderName, String fileName) %7B%0A        CameraPending%3CPhotoFile%3E output = new CameraPending%3C%3E();%0A%0A        toJpegBytes().whenReady(jpeg -%3E %7B%0A           output.run(pending -%3E %7B%0A               File directory = new File(mContext.getFilesDir(), folderName);%0A%0A               if (!directory.isDirectory()) %7B%0A                   directory.mkdirs();%0A               %7D%0A%0A               File imageFile = new File(directory, fileName);%0A               FileOutputStream out = new FileOutputStream(imageFile);%0A               out.write(jpeg.getBytes());%0A               out.flush();%0A               out.close();%0A%0A               pending.set(new PhotoFile(mContext, imageFile));%0A           %7D);%0A        %7D);%0A%0A        return output;%0A    %7D%0A%0A
     public Camer
@@ -2434,16 +2434,21 @@
   toJpeg
+Bytes
 ().whenR
@@ -2740,32 +2740,32 @@
              %7D%0A%0A
+
                 
@@ -2813,17 +2813,8 @@
 Name
- + %22.png%22
 );%0A 

Add Photo methods to save to local app storage file