/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.mozstumbler.client.mapview;

import android.location.Location;

import com.ekito.simpleKML.model.Coordinate;

import org.json.JSONException;
import org.json.JSONObject;
import org.mozilla.mozstumbler.client.ClientPrefs;
import org.mozilla.mozstumbler.service.core.logging.ClientLog;
import org.mozilla.mozstumbler.service.stumblerthread.datahandling.DataStorageContract;
import org.mozilla.mozstumbler.service.stumblerthread.datahandling.StumblerBundle;
import org.mozilla.mozstumbler.service.utils.NetworkInfo;
import org.mozilla.mozstumbler.svclocator.ServiceLocator;
import org.mozilla.mozstumbler.svclocator.services.log.ILogger;
import org.mozilla.mozstumbler.svclocator.services.log.LoggerUtil;
import org.mozilla.osmdroid.util.GeoPoint;

public class ObservationPoint implements MLSLocationGetter.MLSLocationGetterCallback {

    private static final ILogger Log = (ILogger) ServiceLocator.getInstance().getService(ILogger.class);
    private static final String LOG_TAG = LoggerUtil.makeLogTag(ObservationPoint.class);

    public final GeoPoint pointGPS;
    public GeoPoint pointMLS;
    public long mTimestamp;
    public int mWifiCount;
    public int mCellCount;
    public boolean mWasReadFromFile;
    public double mHeading;
    private JSONObject mMLSQuery;
    private boolean mIsMLSLocationQueryRunning;

    public ObservationPoint(GeoPoint pointGPS) {
        this.pointGPS = pointGPS;
        mTimestamp = System.currentTimeMillis();
    }

    public ObservationPoint(Coordinate pointGPS, int wifis, int cells/*, long timestamp*/) {
        this.pointGPS = new GeoPoint(pointGPS.getLatitude(), pointGPS.getLongitude());
        mWifiCount = wifis;
        mCellCount = cells;
        /*mTimestamp = timestamp;*/
    }

    public void setMLSQuery(StumblerBundle stumbleBundle) {
        try {
            mMLSQuery = stumbleBundle.toMLSGeolocate();
            //Log.d(LOG_TAG, "PII geolocate: " + mMLSQuery.toString(2));

        } catch (JSONException e) {
            ClientLog.w(LOG_TAG, "Failed to convert bundle to JSON: " + e);
        }
    }

    public void setCounts(JSONObject ichnaeaQueryObj) {
        try {
            mCellCount = ichnaeaQueryObj.getJSONArray(DataStorageContract.ReportsColumns.CELL).length();
            mWifiCount = ichnaeaQueryObj.getJSONArray(DataStorageContract.ReportsColumns.WIFI).length();
        } catch (JSONException ex) {
            Log.w(LOG_TAG, "Unable to set count length: " + ex.toString());
        }
    }

    public void fetchMLS() {
        if (mMLSQuery == null || pointMLS != null || mIsMLSLocationQueryRunning) {
            return;
        }
        ClientPrefs prefs = ClientPrefs.getInstanceWithoutContext();
        if (prefs == null || !NetworkInfo.getInstance().isConnected() ||
                (prefs.getUseWifiOnly() && !NetworkInfo.getInstance().isWifiAvailable())) {
            return;
        }

        mIsMLSLocationQueryRunning = true;
        new MLSLocationGetter(this, mMLSQuery).execute();
    }

    public boolean needsToFetchMLS() {
        return pointMLS == null && mMLSQuery != null;
    }

    public void setMLSResponseLocation(Location location) {
        mIsMLSLocationQueryRunning = false;

        if (location != null) {
            mMLSQuery = null; // todo decide how to persist this to kml
            pointMLS = new GeoPoint(location);
        }
    }

    public Coordinate getGPSCoordinate() {
        return new Coordinate(pointGPS.getLongitude(), pointGPS.getLatitude(), 0.0);
    }

    public Coordinate getMLSCoordinate() {
        return new Coordinate(pointMLS.getLongitude(), pointMLS.getLatitude(), 0.0);
    }

    public void setMLSCoordinate(Coordinate c) {
        pointMLS = new GeoPoint(c.getLatitude(), c.getLongitude());
    }

    public void errorMLSResponse(boolean stopRequesting) {
        if (stopRequesting) {
            ClientLog.i(ObservationPoint.class.getSimpleName(), "Error:" + mMLSQuery.toString());
            mMLSQuery = null;
        }
        mIsMLSLocationQueryRunning = false;
    }
}

@@ -329,16 +329,43 @@
 inate;%0A%0A
+import org.json.JSONArray;%0A
 import o
@@ -2389,36 +2389,23 @@
     
-try %7B%0A            mCellCount
+JSONArray cells
  = i
@@ -2411,34 +2411,34 @@
 ichnaeaQueryObj.
-ge
+op
 tJSONArray(DataS
@@ -2472,16 +2472,62 @@
 ns.CELL)
+;%0A        mCellCount = (cells != null) ? cells
 .length(
@@ -2519,33 +2519,38 @@
 ? cells.length()
-;
+ : 0;%0A
 %0A            mWi
@@ -2542,30 +2542,31 @@
         
-    mWifiCount
+JSONArray wifis
  = ichna
@@ -2576,18 +2576,18 @@
 ueryObj.
-ge
+op
 tJSONArr
@@ -2629,25 +2629,16 @@
 ns.WIFI)
-.length()
 ;%0A      
@@ -2643,122 +2643,58 @@
     
-%7D catch (JSONException ex) %7B%0A            Log.w(LOG_TAG, %22Unable to set count length: %22 + ex.toString());%0A        %7D
+mWifiCount = (wifis != null) ? wifis.length() : 0;
 %0A   

fix wifi only observation point