package com.example.habitrack;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Base64;

import java.io.ByteArrayOutputStream;

/**
 * Created by sshussai on 11/17/17.
 */

/**
 * Handles the acceptance and compression of Images
 */
public class ImageHandler {

    public static class Compressor extends AsyncTask<Bitmap, Void, String> {
        private final int COMPRESSION_QUALITY = 100;
        private String encodedImage;
        ByteArrayOutputStream byteArrayBitmapStream = new ByteArrayOutputStream();

        // Code taken from: http://mobile.cs.fsu.edu/converting-images-to-json-objects/
        @Override
        protected String doInBackground(Bitmap... bitmaps) {
            for(Bitmap bitmap : bitmaps){
                bitmap.compress(Bitmap.CompressFormat.PNG, COMPRESSION_QUALITY,
                        byteArrayBitmapStream);
                byte[] b = byteArrayBitmapStream.toByteArray();
                encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
            }
            return encodedImage;
        }
    }

    /**
     * Decompresses the image
     */
    public static class Decompressor extends AsyncTask<String, Void, Bitmap>{

        private Bitmap decodedByte;

        // Code taken from: http://mobile.cs.fsu.edu/converting-images-to-json-objects/
        @Override
        protected Bitmap doInBackground(String... strings) {
            for (String stringPicture : strings) {
                byte[] decodedString = Base64.decode(stringPicture, Base64.DEFAULT);

                decodedByte = BitmapFactory.decodeByteArray(decodedString,
                        0, decodedString.length);
            }
            return decodedByte;
        }
    }
}

@@ -327,24 +327,26 @@
 dler %7B%0A%0A    
+//
 public stati
@@ -406,16 +406,95 @@
 ring%3E %7B%0A
+    public static class Compressor extends AsyncTask%3CHabitEvent, Void, Void%3E %7B%0A
         
@@ -663,96 +663,8 @@
 );%0A%0A
-        // Code taken from: http://mobile.cs.fsu.edu/converting-images-to-json-objects/%0A
     
@@ -695,22 +695,20 @@
 otected 
-String
+Void
  doInBac
@@ -719,24 +719,32 @@
 und(
-Bitmap
+HabitEvent
 ... 
+ha
 bit
-map
+Event
 s) %7B
@@ -764,30 +764,34 @@
 for(
-Bitmap bitmap
+HabitEvent he
  : 
+ha
 bit
-map
+Event
 s)%7B%0A
@@ -810,14 +810,108 @@
     
-b
+B
 itmap
+ photo = he.getDecodedPhoto();%0A                he.setDecodedPhoto(null);%0A                photo
 .com
@@ -967,32 +967,8 @@
 ITY,
-%0A                       
  byt
@@ -1125,16 +1125,66 @@
 FAULT);%0A
+                he.setEncodedPhoto(encodedImage);%0A
         
@@ -1208,28 +1208,20 @@
  return 
-encodedImage
+null
 ;%0A      

change img compression function to work with he instead of a picture