package com.android.chmo.im.location;

import android.content.Context;
import android.location.Address;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.text.TextUtils;
import android.util.Log;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeAddress;
import com.amap.api.services.geocoder.RegeocodeQuery;

import java.util.List;

/**
 * Created by Administrator on 2018/8/8.
 */

public class ChmoLocationManager{
    private final String TAG = "ChmoLocationManager";
    private Context mContext;
    private Criteria criteria;
    private AMapLocationClient client;

    private AMapLocationListener locationListener;

    public ChmoLocationManager(Context context, AMapLocationListener listener) {
        mContext = context;
        locationListener = listener;
    }

    public static boolean isLocationEnable(Context context) {
        LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        Criteria cri = new Criteria();
        cri.setAccuracy(Criteria.ACCURACY_COARSE);
        cri.setAltitudeRequired(false);
        cri.setBearingRequired(false);
        cri.setCostAllowed(false);
        String bestProvider = locationManager.getBestProvider(cri, true);
        return !TextUtils.isEmpty(bestProvider);
    }

    public Location getLastKnownLocation() {
        try {
            if (criteria == null) {
                criteria = new Criteria();
                criteria.setAccuracy(Criteria.ACCURACY_COARSE);
                criteria.setAltitudeRequired(false);
                criteria.setBearingRequired(false);
                criteria.setCostAllowed(false);
            }
            return client.getLastKnownLocation();
        } catch (Exception e) {
            Log.i(TAG, "get last known location failed: " + e.toString());
        }
        return null;
    }

    public void request() {
        if (client == null) {
            AMapLocationClientOption option = new AMapLocationClientOption();
            option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
            option.setInterval(30 * 1000);
            option.setHttpTimeOut(10 * 1000);
            client = new AMapLocationClient(mContext);
            client.setLocationOption(option);
            client.setLocationListener(locationListener);
            client.startLocation();
        }
    }

    public void stop() {
        if (client != null) {
            client.unRegisterLocationListener(locationListener);
            client.stopLocation();
            client.onDestroy();
        }
        client = null;
    }

    public RegeocodeAddress getLocationAddress(double latitude, double longitude) {
        List<Address> list;
        try {
            GeocodeSearch search = new GeocodeSearch(mContext);
            LatLonPoint point = new LatLonPoint(latitude, longitude);
            RegeocodeQuery query = new RegeocodeQuery(point, 100, GeocodeSearch.AMAP);
                RegeocodeAddress address = search.getFromLocation(query);
                if (address != null && !TextUtils.isEmpty(address.getFormatAddress())) {
                  return address;
                }
        } catch (Exception e) {
        }

        return null;
    }

}
