/*
    The MIT License (MIT)
    Copyright (c) 2013 Vlad Stirbu

    Permission is hereby granted, free of charge, to any person obtaining
    a copy of this software and associated documentation files (the
    "Software"), to deal in the Software without restriction, including
    without limitation the rights to use, copy, modify, merge, publish,
    distribute, sublicense, and/or sell copies of the Software, and to
    permit persons to whom the Software is furnished to do so, subject to
    the following conditions:

    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
    LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
    OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
    WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.samuelcastro.cordova;

import java.io.File;
import java.io.FilenameFilter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.util.Base64;
import android.util.Log;

@TargetApi(Build.VERSION_CODES.FROYO)
public class InstagramSharePlugin extends CordovaPlugin {

    private static final FilenameFilter OLD_IMAGE_FILTER = new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.startsWith("instagram");
        }
    };

	CallbackContext cbContext;

	@Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

		this.cbContext = callbackContext;

        if (action.equals("shareVideo")) {
            String videoString = args.getString(0);
            String captionString = args.getString(1);
            this.shareVideo(videoString, captionString);
            return true;
        } else if (action.equals("shareImage")) {
            String imageString = args.getString(0);
            String captionString = args.getString(1);
            this.shareImage(imageString, captionString);
            return true;
        } else if (action.equals("isInstalled")) {
        	this.isInstalled();
        } else {
        	callbackContext.error("Invalid Action");
        }
        return false;
    }

	private void isInstalled() {
		try {
			this.webView.getContext().getPackageManager().getApplicationInfo("com.instagram.android", 0);
			this.cbContext.success(this.webView.getContext().getPackageManager().getPackageInfo("com.instagram.android", 0).versionName);
		} catch (PackageManager.NameNotFoundException e) {
			this.cbContext.error("Application not installed");
		}
	}

    private void shareImage(String imageString, String captionString) {
        if (imageString != null && imageString.length() > 0) {
        	byte[] imageData = Base64.decode(imageString, 0);

        	File file = null;
            FileOutputStream os = null;

        	File parentDir = this.webView.getContext().getExternalFilesDir(null);
            File[] oldImages = parentDir.listFiles(OLD_IMAGE_FILTER);
            for (File oldImage : oldImages) {
                oldImage.delete();
            }

            try {
                file = File.createTempFile("instagram", ".png", parentDir);
                os = new FileOutputStream(file, true);
            } catch (Exception e) {
                e.printStackTrace();
            }

        	try {
        		os.write(imageData);
				os.flush();
				os.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

        	Intent shareIntent = new Intent(Intent.ACTION_SEND);
        	shareIntent.setType("image/*");
        	shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file));
        	shareIntent.putExtra(Intent.EXTRA_TEXT, captionString);
        	shareIntent.setPackage("com.instagram.android");

        	this.cordova.startActivityForResult((CordovaPlugin) this, shareIntent, 12345);

        } else {
            this.cbContext.error("Expected one non-empty string argument.");
        }
    }

    private void shareVideo(String videoString, String captionString) {

            // Create the URI from the media
                File media = new File(videoString);
                Uri uri = Uri.fromFile(media);

            	Intent shareIntent = new Intent(Intent.ACTION_SEND);
            	shareIntent.setType("video/*");
            	shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
            	shareIntent.putExtra(Intent.EXTRA_TEXT, captionString);
            	shareIntent.setPackage("com.instagram.android");

            	this.cordova.startActivityForResult((CordovaPlugin) this, shareIntent, 12345);

           // } else {
            //    this.cbContext.error("Expected one non-empty string argument.");
           // }
        }

    @Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
    	if (resultCode == Activity.RESULT_OK) {
    		Log.v("Instagram", "shared ok");
    		this.cbContext.success();
    	} else if (resultCode == Activity.RESULT_CANCELED) {
    		Log.v("Instagram", "share cancelled");
    		this.cbContext.error("Share Cancelled");
    	}
    }
}

@@ -4692,24 +4692,509 @@
    %7D%0A    %7D%0A%0A
+    public String getRealPathFromURI(Uri contentUri) %7B%0A        String res = null;%0A        String%5B%5D proj = %7B MediaStore.Images.Media.DATA %7D;%0A        Cursor cursor = cordova.getActivity().getContentResolver().query(contentUri, proj, null, null, null);%0A        if(cursor.moveToFirst())%7B;%0A           int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);%0A           res = cursor.getString(column_index);%0A        %7D%0A        cursor.close();%0A        return res;%0A    %7D%0A%0A
     private 
@@ -5337,16 +5337,35 @@
 ew File(
+getRealPathFromURI(
 videoStr
@@ -5364,24 +5364,25 @@
 videoString)
+)
 ;%0A          

Add a method to return the real path based on URI