package com.siteshot.siteshot.utils;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.os.Environment;
import android.util.Log;

import com.parse.ParseFile;
import com.parse.ParseUser;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by aclissold on 10/6/14.
 */
public class PhotoUtils {

    private static final String TAG = PhotoUtils.class.getName();

    private String mCurrentPhotoPath;

    public String getCurrentPhotoPath() {
        return mCurrentPhotoPath;
    }

    /**
     *
     * @param bitmap the image to upload to Parse
     *
     * @return       the same bitmap, rotated if necessary
     */
    public Bitmap uploadProfilePhoto(Bitmap bitmap) {
        Bitmap rotatedBitmap = rotate(bitmap);

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
        byte[] data = stream.toByteArray();
        ParseFile file = new ParseFile("userIcon.jpg",data);
        ParseUser user = ParseUser.getCurrentUser();
        user.put("icon", file);
        user.saveInBackground();

        return rotatedBitmap;
    }

    private Bitmap rotate(Bitmap bitmap) {
        int iconOrientation = 1;

        try {
            ExifInterface exif = new ExifInterface(mCurrentPhotoPath);
            iconOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
        } catch (IOException e) {
            Log.d(TAG, e.getMessage());
        }

        Matrix matrix = new Matrix();

        switch (iconOrientation) {
            case ExifInterface.ORIENTATION_NORMAL:
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                matrix.setRotate(90);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                matrix.setRotate(180);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                matrix.setRotate(-90);
                break;
        }

        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }

    /**
     * Creates the photo file on disk, ready to be written to. After calling this method,
     * getCurrentPhotoPath will return the absolute path of the file.
     *
     * @return             a new photo file
     * @throws IOException
     */
    public File createPhotoFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        mCurrentPhotoPath = image.getAbsolutePath();
        return image;
    }
}

@@ -212,24 +212,54 @@
 .ParseFile;%0A
+import com.parse.ParseObject;%0A
 import com.p
@@ -274,16 +274,16 @@
 seUser;%0A
-
 %0Aimport 
@@ -673,32 +673,538 @@
 otoPath;%0A    %7D%0A%0A
+    public Bitmap uploadPhoto(Bitmap bitmap) %7B%0A        Bitmap rotatedBitmap = rotate(bitmap);%0A%0A        ByteArrayOutputStream stream = new ByteArrayOutputStream();%0A        rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);%0A        byte%5B%5D data = stream.toByteArray();%0A        ParseFile file = new ParseFile(%22photo.jpg%22, data);%0A        ParseObject object = new ParseObject(%22UserPhoto%22);%0A        object.put(%22photo%22, file);%0A        object.saveInBackground();%0A%0A        return rotatedBitmap;%0A    %7D%0A%0A
     /**%0A     *%0A 
@@ -1605,32 +1605,32 @@
 .toByteArray();%0A
-
         ParseFil
@@ -1667,16 +1667,17 @@
 on.jpg%22,
+ 
 data);%0A 

Add uploadPhoto method to PhotoUtils