package com.tofly.ghrq_route.module.gps.service;

import android.content.Context;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.esri.arcgisruntime.geometry.Point;
import com.mgq.base.utils.toast.ToastUtil;
import com.tofly.ghrq_route.module.gps.AmapLocationUtils;
import com.tofly.ghrq_route.common.util.ArcgisUtils.ArcgisUtils;
import com.tofly.ghrq_route.common.util.Location.LocationUtil_xu;

/*高德定位*/
public class AmapLocationHelper implements GeocodeSearch.OnGeocodeSearchListener {

    private AMapLocationClient locationClient;

    private GeocodeSearch geocodeSearch;

    private Context mContext;

    protected Point mLocation = null;

    protected Point mProjectLoc = null;

    protected String mAddress = null;

    private boolean isFirstLoc = true;

    private boolean isConsistLocating = false;

    public AmapLocationHelper(Context context){
        this.mContext = context;

        init();
    }

    private void init(){
        initAmapGeocodeSearch();
        initAmapLocationService();
    }

    /**
     * 定位监听
     * */

    public void resetLocation(){
        isFirstLoc = true;
    }

    /*定位服务*/
    private void initAmapLocationService(){
        locationClient = new AMapLocationClient(mContext);
        locationClient.setLocationOption(AmapLocationUtils.getInstance().getDefaultOption());
        locationClient.setLocationListener(aMapLocationListener);
    }

    /*开始定位*/
    public void startLocating(){
        if (locationClient != null)
            locationClient.startLocation();
    }

    /*开始定位*/
    public void startLocating(boolean consist){
        isConsistLocating = consist;
        if (locationClient != null)
            locationClient.startLocation();
    }

    public interface AmapLocChangeListener{
        void onLocationed(Point location, String addr, Point projLoc);
    }

    private AmapLocChangeListener mLocationListener = null;

    public void setMapLocationListener(AmapLocChangeListener locationListener) {
        this.mLocationListener = locationListener;
    }

    private AMapLocationListener aMapLocationListener = location -> {
        if (null != location) {
            //errCode等于0代表定位成功，其他的为定位失败，具体的可以参照官网定位错误码说明
            if (location.getErrorCode() == 0) {
                if (isFirstLoc || isConsistLocating) {
                    isFirstLoc = false;

                    LocationUtil_xu.CoordinateClass coordinate =
                            LocationUtil_xu.gcj02towgs84(location.getLongitude(), location.getLatitude());
                    if (coordinate == null)
                        return;
                    mLocation = new Point(coordinate.Longtitude, coordinate.Latitude);
                    mProjectLoc = ArcgisUtils.loctionProjectDefault(coordinate.Longtitude, coordinate.Latitude);
                    mAddress = location.getAddress();

                    if (mLocationListener != null)
                        mLocationListener.onLocationed(mLocation, mAddress, mProjectLoc);
                }
            }
        }
    };

    /**
     * 地址编码
     */

    /*地址编码，翻地址编码服务*/
    private void initAmapGeocodeSearch(){
        geocodeSearch = new GeocodeSearch(mContext);
        geocodeSearch.setOnGeocodeSearchListener(this);
    }

    public void reserveLocation(double lng, double lat){
        LatLonPoint latLonPoint = new LatLonPoint(lat, lng);
        RegeocodeQuery query = new RegeocodeQuery(latLonPoint,200, GeocodeSearch.GPS);
        geocodeSearch.getFromLocationAsyn(query);
    }

    public interface AmapReserveLocListener{
        void onLocationReserved(boolean isSuccess, String addr);
    }


    private AmapReserveLocListener mReserveLocListener = null;

    public void setReserveLocListener(AmapReserveLocListener mReserveLocListener) {
        this.mReserveLocListener = mReserveLocListener;
    }

    @Override
    public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
        boolean isSuccess = false;
        String address = null;
        if (rCode == 1000) {
            if (result != null && result.getRegeocodeAddress() != null) {
                isSuccess = true;
                address = result.getRegeocodeAddress().getFormatAddress();
                mAddress = address;
            } else {
                ToastUtil.showToast(mContext, "\"抱歉，未能找到结果\"");
            }
            if (mReserveLocListener != null)
                mReserveLocListener.onLocationReserved(isSuccess, address);
        }
    }

    @Override
    public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {

    }

    public void onPause() {
        locationClient.stopLocation();
    }

    public void onResume() {
        locationClient.startLocation();
    }

    public void onDestroy() {
        locationClient.stopLocation();
        locationClient.onDestroy();
        geocodeSearch = null;
    }
}
