package by.vshkl.pxlsrt.core.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class TempStorageUtils {

    public static Observable<String> saveTempBitmap(final FileOutputStream fos, final String fname, final byte[] data) {
        return Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(final ObservableEmitter<String> emitter) throws Exception {
                long start = System.currentTimeMillis();
                Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                System.out.println("Decode bitmap -> " + (System.currentTimeMillis() - start));
                start = System.currentTimeMillis();
                bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getWidth());
                System.out.println("Trim bitmap -> " + (System.currentTimeMillis() - start));
                storeFile(fos, bitmap)
                        .subscribeOn(Schedulers.io())
                        .subscribe(new Consumer<Boolean>() {
                            @Override
                            public void accept(Boolean aBoolean) throws Exception {
                                if (aBoolean) {
                                    emitter.onNext(fname);
                                }
                            }
                        });
            }
        });
    }

    public static Observable<Boolean> saveResultPicture(final FileOutputStream fos, final Bitmap bitmap) {
        return Observable.create(new ObservableOnSubscribe<Boolean>() {
            @Override
            public void subscribe(final ObservableEmitter<Boolean> emitter) throws Exception {
                storeFile(fos, bitmap)
                        .subscribeOn(Schedulers.io())
                        .onErrorReturn(new Function<Throwable, Boolean>() {
                            @Override
                            public Boolean apply(Throwable throwable) throws Exception {
                                emitter.onNext(false);
                                return null;
                            }
                        })
                        .subscribe(new Consumer<Boolean>() {
                            @Override
                            public void accept(Boolean aBoolean) throws Exception {
                                if (aBoolean) {
                                    emitter.onNext(true);
                                }
                            }
                        });
            }
        });
    }

    public static Observable<Bitmap> getTempBitmap(final FileInputStream fis) {
        return Observable.create(new ObservableOnSubscribe<Bitmap>() {
            @Override
            public void subscribe(ObservableEmitter<Bitmap> emitter) throws Exception {
                Bitmap bitmap = BitmapFactory.decodeStream(fis);
                try {
                    fis.close();
                    emitter.onNext(bitmap);
                } catch (IOException e) {
                    e.printStackTrace();
                    emitter.onError(e);
                }
            }
        });
    }

    static Observable<Boolean> storeFile(final FileOutputStream fos, final Bitmap bitmap) {
        return Observable.create(new ObservableOnSubscribe<Boolean>() {
            @Override
            public void subscribe(ObservableEmitter<Boolean> emitter) throws Exception {
                long start = System.currentTimeMillis();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                System.out.println("Compress bitmap -> " + (System.currentTimeMillis() - start));
                bitmap.recycle();
                try {
                    fos.flush();
                    fos.close();
                    emitter.onNext(true);
                } catch (IOException e) {
                    e.printStackTrace();
                    emitter.onError(e);
                }
            }
        });
    }
}

@@ -100,16 +100,40 @@
 Factory;
+%0Aimport android.net.Uri;
 %0A%0Aimport
@@ -803,65 +803,8 @@
 n %7B%0A
-                long start = System.currentTimeMillis();%0A
     
@@ -904,85 +904,562 @@
     
-System.out.println(%22Decode bitmap -%3E %22 + (System.currentTimeMillis() - start)
+bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getWidth());%0A                storeFile(fos, bitmap)%0A                        .subscribeOn(Schedulers.io())%0A                        .subscribe(new Consumer%3CBoolean%3E() %7B%0A                            @Override%0A                            public void accept(Boolean aBoolean) throws Exception %7B%0A                                if (aBoolean) %7B%0A                                    emitter.onNext(fname);%0A                                %7D%0A                            %7D%0A                        %7D
 );%0A 
@@ -1471,141 +1471,401 @@
     
+  %7D%0A
       
-start = System.currentTimeMillis();%0A                bitmap = Bitmap.create
+  %7D);%0A    %7D%0A%0A    public static Observable%3CString%3E saveTempBitmap(final FileOutputStream fos, final String fname, final Uri uri) %7B%0A        return Observable.create(new ObservableOnSubscribe%3CString%3E() %7B%0A            @Override%0A            public void subscribe(final ObservableEmitter%3CString%3E emitter) throws Exception %7B%0A                
 Bitmap
-(
+ 
 bitmap
-, 0, 0, bitmap.getWidth(), bitmap
+ = BitmapFactory.decodeFile(uri
 .get
-Wid
+Pa
 th()
@@ -1887,82 +1887,86 @@
     
-System.out.println(%22Trim bitmap -%3E %22 + (System.currentTimeMillis() - start
+bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getWidth(
 ));%0A
@@ -4530,215 +4530,60 @@
     
-long start = System.currentTimeMillis();%0A                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);%0A                System.out.println(%22Compress bitmap -%3E %22 + (System.currentTimeMillis() - start)
+bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos
 );%0A 

Save temp image with given uri