package com.oo.seex.oo_statistics_lib.utils;

import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.Looper;
import android.text.TextUtils;

/**
 * Created by efort on 15/11/11.
 */
public class LocationUtils {
    private static String latitude = "";
    private static String longitude = "";
    static LocationListener locationListener;
    private static boolean isRemove = false;//判断网络监听是否移除

    public static void getLocat(final Context context,final LocationManager locationManager, final MyLocationListener listener) {
        try {
            locationListener = new LocationListener() {
                // Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
                @Override
                public void onStatusChanged(String provider, int status, Bundle extras) {
                    if (LocationProvider.OUT_OF_SERVICE == status) {
                        CommonUtil.printLog("", "网络定位服务无法使用,切换至Gps定位");
                    }
                }

                // Provider被enable时触发此函数，比如GPS被打开
                @Override
                public void onProviderEnabled(String provider) {

                }

                // Provider被disable时触发此函数，比如GPS被关闭
                @Override
                public void onProviderDisabled(String provider) {

                }

                //当坐标改变时触发此函数，如果Provider传进相同的坐标，它就不会被触发
                @Override
                public void onLocationChanged(Location location) {
                    try {
                        if (location != null) {
                            latitude = location.getLatitude() + ""; //经度
                            longitude = location.getLongitude() + ""; //纬度
                            CommonUtil.printLog("location:" + latitude, longitude);
                            if (!TextUtils.isEmpty(latitude) && !TextUtils.isEmpty(longitude)) {
                                listener.locat(location);
                            }
                        }
                        // 获得GPS服务后，移除network监听
                        if (location != null && !isRemove) {
                            locationManager.removeUpdates(locationListener);
                            isRemove = true;
                        }
                    }catch (Exception e){
                        CommonUtil.printLog(e);
                    }

                }
            };

            boolean isGpsAvalible = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
            boolean isNetAvalible = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (isGpsAvalible) {
                Looper.prepare();
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locationListener);
                Looper.loop();
                Location locate = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                if (locate != null) {
                    latitude = locate.getLatitude() + "";
                    longitude = locate.getLongitude() + "";
                    if (!TextUtils.isEmpty(latitude) && !TextUtils.isEmpty(longitude)) {
                        listener.locat(locate);
                        CommonUtil.printLog("location", latitude + "--" + longitude);
                        return;
                    }
                }
            } else if (isNetAvalible && CommonUtil.isNetworkAvailable(context)) {
                Looper.prepare();
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 1, locationListener, Looper.myLooper());
                Looper.loop();
                Location locate = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                if (locate != null) {
                    latitude = locate.getLatitude() + "";
                    longitude = locate.getLongitude() + "";
                    if (!TextUtils.isEmpty(latitude) && !TextUtils.isEmpty(longitude)) {
                        listener.locat(locate);
                        CommonUtil.printLog("location", latitude + "--" + longitude);
                        return;
                    }
                }
            }
        }catch (Exception e){
            CommonUtil.printLog(e);
        }
    }

    public interface MyLocationListener {
        void locat(Location location);
    }
}
