package com.xlythe.textmanager.text;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.provider.MediaStore;

import com.xlythe.textmanager.text.concurrency.Future;
import com.xlythe.textmanager.text.concurrency.FutureImpl;
import com.xlythe.textmanager.text.concurrency.Present;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public final class VideoAttachment extends Attachment {
    private transient byte[] mBytes;

    public VideoAttachment(Uri uri){
        super(Type.VIDEO, uri);
    }

    private VideoAttachment(Parcel in) {
        super(in);
    }

    public static final Parcelable.Creator<VideoAttachment> CREATOR = new Parcelable.Creator<VideoAttachment>() {
        public VideoAttachment createFromParcel(Parcel in) {
            return new VideoAttachment(in);
        }

        public VideoAttachment[] newArray(int size) {
            return new VideoAttachment[size];
        }
    };

    public synchronized Future<byte[]> getBytes(final Context context) {
        if (mBytes != null) {
            return new Present<>(mBytes);
        } else {
            return new FutureImpl<byte[]>() {
                @Override
                public byte[] get() {
                    byte[] videoBytes = null;
                    try {
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        FileInputStream fis = new FileInputStream(new File(getRealPath()));

                        byte[] buf = new byte[1024];
                        int n;
                        while (-1 != (n = fis.read(buf)))
                            baos.write(buf, 0, n);

                        videoBytes = baos.toByteArray();
                        setBytes(videoBytes);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return videoBytes;
                }

                private String getRealPath() {
                    Cursor cursor = null;
                    try {
                        String[] proj = { MediaStore.Images.Media.DATA };
                        cursor = context.getContentResolver().query(getUri(),  proj, null, null, null);
                        if (cursor == null) {
                            return getUri().getPath();
                        }
                        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                        cursor.moveToFirst();
                        return cursor.getString(column_index);
                    } finally {
                        if (cursor != null) {
                            cursor.close();
                        }
                    }
                }
            };
        }
    }

    private synchronized void setBytes(byte[] bytes) {
        mBytes = bytes;
    }
}

@@ -27,24 +27,145 @@
 ager.text;%0A%0A
+import static com.xlythe.textmanager.text.TextManager.DEBUG;%0Aimport static com.xlythe.textmanager.text.TextManager.TAG;%0A%0A
 import andro
@@ -331,16 +331,41 @@
 iaStore;
+%0Aimport android.util.Log;
 %0A%0Aimport
@@ -635,27 +635,93 @@
 java.io.
-IOException
+FileNotFoundException;%0Aimport java.io.IOException;%0Aimport java.io.InputStream
 ;%0A%0Apubli
@@ -1750,20 +1750,16 @@
         
-File
 InputStr
@@ -1768,24 +1768,19 @@
 m fis = 
-new File
+get
 InputStr
@@ -1787,31 +1787,8 @@
 eam(
-new File(getRealPath())
 );%0A%0A
@@ -2283,30 +2283,485 @@
 ate 
-String getRealPath() %7B
+InputStream getInputStream() throws FileNotFoundException %7B%0A                    if (DEBUG) %7B%0A                        Log.d(TAG, %22getInputStream(): %22 + getUri());%0A                    %7D%0A%0A                    // Special case for MMS%0A                    if (getUri().toString().startsWith(%22content://mms/part%22)) %7B%0A                        return context.getContentResolver().openInputStream(getUri());%0A                    %7D%0A%0A                    // Special case for files from gallery
 %0A   
@@ -3038,17 +3038,17 @@
 (cursor 
-=
+!
 = null) 
@@ -3057,85 +3057,8 @@
     
-                        return getUri().getPath();%0A                        %7D%0A
     
@@ -3176,24 +3176,28 @@
             
+    
 cursor.moveT
@@ -3230,23 +3230,56 @@
         
+    
 return 
+new FileInputStream(new File(
 cursor.g
@@ -3300,17 +3300,45 @@
 n_index)
-;
+));%0A                        %7D
 %0A       
@@ -3491,32 +3491,166 @@
                %7D
+%0A%0A                    // The normal case, for most files%0A                    return new FileInputStream(new File(getUri().getPath()));
 %0A               

fix for sharing mms