package com.github.oliveiradev.lib;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;

import com.github.oliveiradev.lib.shared.Constants;
import com.github.oliveiradev.lib.shared.TypeRequest;

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

/**
 * Created by felipe on 03/05/16.
 */
public class OverlapActivity extends Activity {

    private Uri fileUri;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handleIntent(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        handleIntent(intent);
    }

    private void handleIntent(Intent intent) {
        TypeRequest typeRequest = (TypeRequest) intent.getExtras().get(Constants.REQUEST_TYPE_EXTRA);
        if (typeRequest == TypeRequest.GALLERY)
            gallery();
        else
            camera();
    }

    private void gallery() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        startActivityForResult(intent, Constants.REQUEST_CODE_ATTACH_IMAGE);
    }

    private void camera() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            File photoFile = null;
            try {
                photoFile = createImageTempFile();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            fileUri = Uri.fromFile(photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
            startActivityForResult(takePictureIntent, Constants.REQUEST_CODE_TAKE_PICURE);
        }
    }


    @SuppressLint("SimpleDateFormat")
    private File createImageTempFile() throws IOException {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES);
        return File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );
    }


    private Uri getUri(int requestCode, Intent data) {
        if (requestCode == Constants.REQUEST_CODE_ATTACH_IMAGE) return data.getData();
        else if (requestCode == Constants.REQUEST_CODE_TAKE_PICURE) return fileUri;
        else return null;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && (requestCode == Constants.REQUEST_CODE_ATTACH_IMAGE || requestCode == Constants.REQUEST_CODE_TAKE_PICURE))
            RxPhoto.onActivityResult(getUri(requestCode, data));
        finish();
    }
}

@@ -99,16 +99,94 @@
 tivity;%0A
+import android.content.ContentResolver;%0Aimport android.content.ContentValues;%0A
 import a
@@ -204,24 +204,24 @@
 ent.Intent;%0A
-
 import andro
@@ -324,16 +324,64 @@
 iaStore;
+%0Aimport android.support.v4.content.FileProvider;
 %0A%0Aimport
@@ -587,16 +587,41 @@
 il.Date;
+%0Aimport java.util.Locale;
 %0A%0A/**%0A *
@@ -1750,233 +1750,33 @@
     
-File photoFile = null;%0A            try %7B%0A                photoFile = createImageTempFile();%0A            %7D catch (Exception ex) %7B%0A                ex.printStackTrace();%0A            %7D%0A            fileUri = Uri.fromFile(photoFile
+fileUri = createImageUri(
 );%0A 
@@ -1953,113 +1953,162 @@
   %7D%0A
+%0A
     %7D%0A%0A
-%0A
     
-@SuppressLint(%22SimpleDateFormat%22)%0A    private File createImageTempFile() throws IOException %7B
+private Uri createImageUri() %7B%0A        ContentResolver contentResolver = getContentResolver();%0A        ContentValues cv = new ContentValues();
 %0A   
@@ -2169,16 +2169,37 @@
 _HHmmss%22
+, Locale.getDefault()
 ).format
@@ -2224,40 +2224,45 @@
     
-String imageFileName = %22JPEG_%22 +
+cv.put(MediaStore.Images.Media.TITLE,
  tim
@@ -2271,14 +2271,9 @@
 tamp
- + %22_%22
+)
 ;%0A  
@@ -2282,304 +2282,86 @@
     
-File storageDir = Environment.getExternalStoragePublicDirectory(%0A                Environment.DIRECTORY_PICTURES);%0A        return File.createTempFile(%0A                imageFileName,  /* prefix */%0A                %22.jpg%22,         /* suffix */%0A                storageDir      /* directory */%0A        
+return contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cv
 );%0A 

fix handling camera images when targetSdk is 24