package com.loveboyuan.smarttrader;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

/**
 * Created by BSid on 15-11-19.
 */
public class UserLocation implements Runnable{

    private static final long TRACK_TIME = 2000;
    private Location currentlocation;
    private Context context;
    private boolean isRunning = false;
    private LocationManager manager;
    private LocationListener listener;

    public void run() {

        manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        listener  = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                updateLocation(location);
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {

            }
        };

        try {
            manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
        }catch (SecurityException e){
            System.out.println("Lack Permissions to use location");
        }

    }

    public void startTracking(Context context){
        this.context = context;
        isRunning = true;
        run();
    }

    private void stopTracking(){
        //TODO
        isRunning = false;
    }

    public void setItemLocation(Item item){

        if (isRunning){
            item.setLocation(currentlocation);
            stopTracking();
            try {
                manager.removeUpdates(listener);
            }catch (SecurityException e){
                System.out.println("Lack Permissions to use location");
            }
        }

    }

    public Location getUserLocation(){

        if (locationIsCurrent()){
            return currentlocation;
        } else {
            long start = System.currentTimeMillis();
            while((System.currentTimeMillis()-start)<TRACK_TIME){
                //wait
            }
            stopTracking();
        }
        return currentlocation;
    }

    public double itemDistanceFromUser(Item item) {

        return getUserLocation().distanceTo(item.getLocation());
    }

    private void updateLocation(Location location){
        if (!locationIsCurrent()){
            if (location.getAccuracy() > currentlocation.getAccuracy()){
                currentlocation = location;
            }
        }

    }

    private Boolean locationIsCurrent(){

        if (currentlocation == null || (System.currentTimeMillis() - currentlocation.getTime())<2000) {
            return false;
        } else{
            return true;
        }
    }

}

@@ -276,29 +276,10 @@
 ion 
-implements Runnable
 %7B%0A
+
 %0A   
@@ -511,869 +511,8 @@
 r;%0A%0A
-    public void run() %7B%0A%0A        manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);%0A        listener  = new LocationListener() %7B%0A            @Override%0A            public void onLocationChanged(Location location) %7B%0A                updateLocation(location);%0A            %7D%0A%0A            @Override%0A            public void onStatusChanged(String provider, int status, Bundle extras) %7B%0A%0A            %7D%0A%0A            @Override%0A            public void onProviderEnabled(String provider) %7B%0A%0A            %7D%0A%0A            @Override%0A            public void onProviderDisabled(String provider) %7B%0A%0A            %7D%0A        %7D;%0A%0A        try %7B%0A            manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);%0A        %7Dcatch (SecurityException e)%7B%0A            System.out.println(%22Lack Permissions to use location%22);%0A        %7D%0A%0A    %7D%0A
 %0A   
@@ -626,11 +626,12 @@
     
-run
+init
 ();%0A
@@ -674,23 +674,8 @@
 ()%7B%0A
-        //TODO%0A
     
@@ -693,24 +693,199 @@
 ng = false;%0A
+        try %7B%0A            manager.removeUpdates(listener);%0A        %7Dcatch (SecurityException e)%7B%0A            System.out.println(%22Lack Permissions to use location%22);%0A        %7D%0A
     %7D%0A%0A    p
@@ -946,16 +946,17 @@
 Running)
+ 
 %7B%0A      
@@ -1028,203 +1028,8 @@
 ();%0A
-            try %7B%0A                manager.removeUpdates(listener);%0A            %7Dcatch (SecurityException e)%7B%0A                System.out.println(%22Lack Permissions to use location%22);%0A            %7D%0A
     
@@ -1026,33 +1026,32 @@
 ng();%0A        %7D%0A
-%0A
     %7D%0A%0A    publi
@@ -1450,24 +1450,24 @@
 em item) %7B%0A%0A
-
         retu
@@ -1518,32 +1518,896 @@
 tion());%0A    %7D%0A%0A
+    private void init() %7B%0A%0A        manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);%0A        listener  = new LocationListener() %7B%0A            @Override%0A            public void onLocationChanged(Location location) %7B%0A                updateLocation(location);%0A            %7D%0A%0A            @Override%0A            public void onStatusChanged(String provider, int status, Bundle extras) %7B%0A%0A            %7D%0A%0A            @Override%0A            public void onProviderEnabled(String provider) %7B%0A%0A            %7D%0A%0A            @Override%0A            public void onProviderDisabled(String provider) %7B%0A%0A            %7D%0A        %7D;%0A%0A        try %7B%0A            manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);%0A        %7Dcatch (SecurityException e)%7B%0A            System.out.println(%22Lack Permissions to use location%22);%0A        %7D%0A%0A    %7D%0A%0A
     private void

update UserLocation