/**
 *
 * LocationPlugin
 * LocationPlugin
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2014 Haploid
 *
 * 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 fr.haploid.LocationPlugin;

import fr.cobaltians.cobalt.Cobalt;
import fr.cobaltians.cobalt.fragments.CobaltFragment;
import fr.cobaltians.cobalt.plugin.CobaltAbstractPlugin;
import fr.cobaltians.cobalt.plugin.CobaltPluginWebContainer;

import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.util.Log;

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

/**
 * @author Sébastien Famel
 */
public final class LocationPlugin extends CobaltAbstractPlugin {

    // TAG
    private static final String TAG = LocationPlugin.class.getSimpleName();

    /**********************************************************
     * MEMBERS
     **********************************************************/

    private static final String LONGITUDE = "longitude";
    private static final String LATITUDE = "latitude";
    private static final String ALTITUDE = "altitude";
    private static final String ACCURACY = "accuracy";

    private static final String GET_LOCATION = "getLocation";
    private static final String GET_LONGITUDE = "getLongitude";
    private static final String GET_LATITUDE = "getLatitude";
    private static final String GET_ALTITUDE = "getAltitude";
    private static final String GET_ACCURACY = "getAccuracy";

    /**************************************************************************************
     * CONSTRUCTORS
     **************************************************************************************/

    public static CobaltAbstractPlugin getInstance(CobaltPluginWebContainer webContainer) {
        if (sInstance == null) {
            sInstance = new LocationPlugin();
        }

        sInstance.addWebContainer(webContainer);

        return sInstance;
    }


    /***********************************************************************************************************
     * OVERRIDEN METHODS
     ***********************************************************************************************************/

    @Override
    public void onMessage(CobaltPluginWebContainer webContainer, JSONObject message) {
        Activity activity = webContainer.getActivity();
        CobaltFragment fragment = webContainer.getFragment();

        LocationManager locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        String provider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(provider);
        Log.d(TAG, "location : "+location.toString());

        try {
            String action = message.getString(Cobalt.kJSAction);

            if (action.equals(GET_LOCATION) ||
                    action.equals(GET_LONGITUDE) ||
                    action.equals(GET_LATITUDE) ||
                    action.equals(GET_ALTITUDE) ||
                    action.equals(GET_ACCURACY)) {

                String callback = message.getString(Cobalt.kJSCallback);
                JSONObject resultLocation = new JSONObject();

                if (location != null) {
                    if (action.equals(GET_LOCATION) ||
                            action.equals(GET_LONGITUDE))
                        resultLocation.put(LONGITUDE, location.getLongitude());

                    if (action.equals(GET_LOCATION) ||
                            action.equals(GET_LATITUDE))
                        resultLocation.put(LATITUDE, location.getLatitude());

                    if (action.equals(GET_ALTITUDE))
                        resultLocation.put(ALTITUDE, location.getAltitude());

                    if (action.equals(GET_ACCURACY))
                        resultLocation.put(ACCURACY, location.getAccuracy());

                    fragment.sendCallback(callback, resultLocation);
                }

                else if (location == null) {
                    fragment.sendCallback(callback, null);
                    if (Cobalt.DEBUG) Log.d(TAG, "location is NULL");
                }

                else if (Cobalt.DEBUG) Log.d(TAG, "onMessage : " + message.toString());
            }
        }
        catch (JSONException exception) {
            if (Cobalt.DEBUG) exception.printStackTrace();
        }
    }
}

@@ -3817,55 +3817,54 @@
     
-Log.d(TAG, %22location : %22+location.toString());%0A
+%0A        boolean catchAction = false;%0A        
 %0A   
@@ -4189,24 +4189,71 @@
 CURACY)) %7B%0A%0A
+            %09catchAction = true;%0A            %09%0A
             
@@ -5177,30 +5177,8 @@
 else
- if (location == null)
  %7B%0A 
@@ -5344,46 +5344,105 @@
     
-else if (Cobalt.DEBUG) Log.d(TAG, %22onM
+if (!catchAction) %0A                %09if (Cobalt.DEBUG) Log.d(TAG, %22ERROR - can't found action in m
 essa
@@ -5567,16 +5567,134 @@
 .DEBUG) 
+%7B%0A            %09if (Cobalt.DEBUG) Log.d(TAG, %22ERROR - can't find a good key in : %22 + message.toString());%0A            %09
 exceptio
@@ -5710,24 +5710,38 @@
 ackTrace();%0A
+            %7D%0A
         %7D%0A  

clean code