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

import android.content.Context;

import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.location.LocationDataSource;
import com.esri.arcgisruntime.mapping.view.LocationDisplay;
import com.esri.arcgisruntime.mapping.view.MapView;

/*Arcgis定位*/
public class ArcgisLocationHelper {

    private LocationDisplay arcgisLdm;

    private MapView mapView;

    private Context mContext;

    protected Point mLocation = null;

    protected String mAddress = null;

    private boolean isFirstLoc = true;


    public ArcgisLocationHelper(Context context, MapView mapView){
        this.mContext = context;
        this.mapView = mapView;

        init();
    }

    public void init(){
        initArcgisLocationService();
    }

    /**
     * 定位监听
     */

    public void resetLocation(){
        isFirstLoc = true;
    }

    /*定位服务*/
    private void initArcgisLocationService() {
        arcgisLdm = mapView.getLocationDisplay();
        arcgisLdm.setAutoPanMode(LocationDisplay.AutoPanMode.RECENTER);
        arcgisLdm.startAsync();
        //arcgisLdm.addLocationChangedListener(arcgisListener);
    }

    public interface ArcgisLocChangeListener{
        void onLocationed(Point location);
    }

    private ArcgisLocChangeListener mLocationListener = null;

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

    private LocationDisplay.LocationChangedListener arcgisListener = event -> {
        if (event == null || event.getLocation() == null)
            return;
        if (isFirstLoc) {
            isFirstLoc = false;
            LocationDataSource.Location location = event.getLocation();
            mLocation = location.getPosition();

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


    public void onPause() {
        arcgisLdm.stop();
    }

    public void onStop() {
        arcgisLdm.stop();
    }

    public void onResume() {
        arcgisLdm.startAsync();
    }

    public void onDestroy(){
        arcgisLdm.stop();
        arcgisLdm = null;
    }
}
