/*
 * Copyright (C) 2015  Xiaolong Tang
 *
 * 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.
 */

/**
 * Retrieve the last picture by criteria
 */
package com.infobeyond.nxdrive;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;

public class LastPhotoTaken extends CordovaPlugin {
	public static String TAG = "LastPhotoTaken";
    public static string ACTION = "getLastPhoto";

    final String[] projection = { MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_TAKEN};
    final String orderBy = MediaStore.Images.Media.DATE_ADDED;
	 
	private CallbackContext callbackContext;

    public class Result {
        public double timestamp = 0.0;
        public string path = null;
        public JSONObject toJSONObject() throws JSONException {
            return new JSONObject(
                                  "{path:" + path +
                                  ",timestamp:" + timestamp + "}");
        }
    }
	 
	public boolean execute(String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException {
        this.callbackContext = callbackContext;
		if (action.equals(ACTION)) {
            // Expect three params: max, and a pair of time ticks
            int max = args.getInt(0);
            double startTimeTick = args.getFloat(1);
            double endTimeTick = args.getFloat(2);
            boolean found = false;
            Result searchResult = new Result();

            Cursor cursor =  managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, orderBy);

            if (cursor.moveToFirst()) {
                int dataColumn = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
                int dateColumn = cur.getColumnIndex(MediaStore.Images.Media.DATE_TAKEN);
                
                do {
                    // Do some math here ...
                    string url = cursor.getString(dataColumn);
                    // COnvert it into a path
                    searchResult.path = url;
                    // timeStamp = cursor.getDouble(dateColumn);
                    found = true;
                    break;
                } while (cursor.moveToNext());
            }
            cursor.close();

            // Return
            if (found) {
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, searchResult.toJSONObject()));
            } else {
                callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, "There are no photos."));
            }
        } else {
            callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.IO_EXCEPTION, "Something wrong."));
        }
    }
    return true;
}


@@ -1275,16 +1275,56 @@
 aPlugin;
+%0Aimport org.apache.cordova.PluginResult;
 %0A%0Aimport
@@ -1462,16 +1462,48 @@
 tivity;%0A
+import android.content.Context;%0A
 import a
@@ -1549,16 +1549,124 @@
 til.Log;
+%0Aimport android.content.ContentProvider;%0Aimport android.database.Cursor;%0Aimport android.provider.MediaStore;
 %0A%0Apublic
@@ -1775,17 +1775,17 @@
  static 
-s
+S
 tring AC
@@ -2015,18 +2015,59 @@
 kContext
-;%0A
+ = null;%0A%09private Context context = null;%0A%09
 %0A    pub
@@ -2139,17 +2139,17 @@
  public 
-s
+S
 tring pa
@@ -2394,16 +2394,171 @@
 %7D%0A    %7D%0A
+    %0A    /**%0A     * Ctor%0A     */%0A    public LastPhotoTaken() %7B%0A    %09super();%0A    %09this.context = this.cordova.getActivity().getApplicationContext();%0A    %7D%0A
 %09 %0A%09publ
@@ -2901,21 +2901,22 @@
 args.get
-Float
+Double
 (1);%0A   
@@ -2957,13 +2957,14 @@
 .get
-Float
+Double
 (2);
@@ -3043,24 +3043,35 @@
 w Result();%0A
+           
 %0A           
@@ -3092,21 +3092,82 @@
  =  
-managedQuery(
+MediaStore.Images.Media.query(context.getContentResolver(),%0A            %09%09
 Medi
@@ -3405,16 +3405,19 @@
 mn = cur
+sor
 .getColu
@@ -3564,17 +3564,17 @@
         
-s
+S
 tring ur
@@ -4381,26 +4381,18 @@
       %7D%0A
-    %7D%0A    
+%09%09
 return t
@@ -4396,11 +4396,17 @@
 n true;%0A
+    %7D%0A
 %7D%0A%0A

Fix bugs in android support