package com.bc_chat.im.activity;


import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeAddress;
import com.amap.api.services.geocoder.RegeocodeQuery;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;

import java.util.ArrayList;
import java.util.List;

import io.rong.common.RLog;
import io.rong.imkit.RongBaseActivity;
import io.rong.imkit.plugin.location.AMapLocationInfo;
import io.rong.imkit.plugin.location.IMyLocationChangedListener;
import io.rong.imkit.plugin.location.LocationManager;
import io.rong.imkit.plugin.location.SearchLocationActivity;
import io.rong.imkit.utilities.RongUtils;

/**
 * author：Created by zhaohaoting on 2018/11/20
 * email：526309416@qq.com
 * desc：
 */

public class MapLocationActivity extends RongBaseActivity implements LocationSource, AMap.OnCameraChangeListener, GeocodeSearch.OnGeocodeSearchListener, IMyLocationChangedListener, View.OnClickListener {
    private static final String TAG = "AMapLocationActivity";
    private static final int REQUEST_CODE_ASK_PERMISSIONS = 100;
    private static final int REQUEST_SEARCH_LOCATION = 1;
    private static final int PAGE_COUNT = 20;
    private int currentPage = 1;
    private int mTouchSlop;
    private BitmapDescriptor mBitmapDescriptor;
    private MapView mAMapView;
    private ImageView myLocationView;
    private AMap mAMap;
    private TextView mLocationTip;
    private Handler mHandler;
    private Marker mMarker;
    private GeocodeSearch mGeocodeSearch;
    private OnLocationChangedListener mLocationChangedListener;
    private double mMyLat;
    private double mMyLng;
    private String mMyPoi;
    private double mLatResult;
    private double mLngResult;
    private String mPoiResult;
    private ListView listViewNearby;
    private NearbyListAdapter nearbyListAdapter;
    private ProgressBar listLoadingView;
    private float Y;
    private float downY;
    private float lastY;
    private int flag = 0;
    private String cityCode = "";
    ValueAnimator animator;
    private boolean isStartSelectAddress = false;//是否是选择的地址

    private AbsListView.OnScrollListener onScrollListener = new AbsListView.OnScrollListener() {
        public void onScrollStateChanged(AbsListView view, int scrollState) {
        }

        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            if (firstVisibleItem != 0 && firstVisibleItem + visibleItemCount == totalItemCount) {
                View lastVisibleItemView = MapLocationActivity.this.listViewNearby.getChildAt(MapLocationActivity.this.listViewNearby.getChildCount() - 1);
                if (lastVisibleItemView != null && lastVisibleItemView.getBottom() == MapLocationActivity.this.listViewNearby.getHeight()) {
                    MapLocationActivity.this.loadNextPageNearByView();
                }
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(io.rong.imkit.R.layout.rc_plugin_location_activity);
        this.mAMapView = (MapView) this.findViewById(io.rong.imkit.R.id.rc_ext_amap);
        this.initNearbyView();
        this.mHandler = new Handler();
        this.mLocationTip = (TextView) this.findViewById(io.rong.imkit.R.id.rc_ext_location_marker);
        this.searchButton.setVisibility(View.VISIBLE);
        this.searchButton.setOnClickListener(this);
        this.myLocationView = (ImageView) this.findViewById(io.rong.imkit.R.id.rc_ext_my_location);
        this.myLocationView.setOnClickListener(this);
        Button ok = (Button) this.findViewById(io.rong.imkit.R.id.rc_action_bar_ok);
        ok.setText(this.getResources().getString(io.rong.imkit.R.string.rc_ext_send));
        ok.setVisibility(View.VISIBLE);
        ok.setOnClickListener(this);
        TextView title = (TextView) this.findViewById(io.rong.imkit.R.id.rc_action_bar_title);
        title.setText(io.rong.imkit.R.string.rc_plugin_location);
        this.mAMapView.onCreate(savedInstanceState);
        this.initMap();
    }

    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 100:
                if (grantResults[0] == 0 && permissions[0].equals("android.permission.ACCESS_COARSE_LOCATION")) {
                    this.initMap();
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }

    }

    private void initMap() {
        this.mAMap = this.mAMapView.getMap();
        this.mAMap.setLocationSource(this);
        this.mAMap.setMyLocationEnabled(true);
        this.mAMap.getUiSettings().setZoomControlsEnabled(false);
        this.mAMap.getUiSettings().setMyLocationButtonEnabled(false);
        this.mAMap.setMapType(1);
        MyLocationStyle myLocationStyle = new MyLocationStyle();
        myLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(io.rong.imkit.R.drawable.rc_ext_my_locator));
        myLocationStyle.strokeWidth(0.0F);
        myLocationStyle.strokeColor(io.rong.imkit.R.color.rc_main_theme);
        myLocationStyle.radiusFillColor(0);
        this.mAMap.setMyLocationStyle(myLocationStyle);
        this.mGeocodeSearch = new GeocodeSearch(this);
        this.mGeocodeSearch.setOnGeocodeSearchListener(this);
        LocationManager.getInstance().setMyLocationChangedListener(this);
        this.mAMap.setOnCameraChangeListener(this);
    }
    @Override
    public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
        RLog.e("AMapLocationActivity", "onRegeocodeSearched");
        if (regeocodeResult != null) {
            if (!isStartSelectAddress) {
                RegeocodeAddress regeocodeAddress = regeocodeResult.getRegeocodeAddress();
                this.mLatResult = regeocodeResult.getRegeocodeQuery().getPoint().getLatitude();
                this.mLngResult = regeocodeResult.getRegeocodeQuery().getPoint().getLongitude();
                String formatAddress = regeocodeResult.getRegeocodeAddress().getFormatAddress();
                this.mPoiResult = formatAddress.replace(regeocodeAddress.getProvince(), "").replace(regeocodeAddress.getCity(), "").replace(regeocodeAddress.getDistrict(), "");
                this.mLocationTip.setText(this.mPoiResult);
                this.updateNearByView(this.mPoiResult);
            }
            isStartSelectAddress = false;
        } else {
            Toast.makeText(this, this.getString(io.rong.imkit.R.string.rc_location_fail), Toast.LENGTH_SHORT).show();
        }

    }

    private String getMapUrl(double latitude, double longitude) {
        return "http://restapi.amap.com/v3/staticmap?location=" + longitude + "," + latitude + "&zoom=16&scale=2&size=408*240&markers=mid,,A:" + longitude + "," + latitude + "&key=e09af6a2b26c02086e9216bd07c960ae";
    }

    public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
    }

    public void activate(OnLocationChangedListener onLocationChangedListener) {
        this.mLocationChangedListener = onLocationChangedListener;
    }

    public void deactivate() {
    }

    public void onCameraChange(CameraPosition cameraPosition) {
        RLog.d("AMapLocationActivity", "onCameraChange");
        if (Build.VERSION.SDK_INT < 11) {
            this.mMarker.setPosition(cameraPosition.target);
        }

    }

    public void onCameraChangeFinish(CameraPosition cameraPosition) {
        RLog.d("AMapLocationActivity", "onCameraChangeFinish");
        LatLonPoint point = new LatLonPoint(cameraPosition.target.latitude, cameraPosition.target.longitude);
        RegeocodeQuery query = new RegeocodeQuery(point, 50.0F, "autonavi");
        this.mGeocodeSearch.getFromLocationAsyn(query);
        if (this.mMarker != null) {
            this.animMarker();
        }

    }

    private void addLocatedMarker(LatLng latLng, String poi) {
        this.mBitmapDescriptor = BitmapDescriptorFactory.fromResource(io.rong.imkit.R.drawable.rc_ext_location_marker);
        MarkerOptions markerOptions = (new MarkerOptions()).position(latLng).icon(this.mBitmapDescriptor);
        if (this.mMarker != null) {
            this.mMarker.remove();
        }

        this.mMarker = this.mAMap.addMarker(markerOptions);
        this.mMarker.setPositionByPixels(this.mAMapView.getWidth() / 2, this.mAMapView.getHeight() / 2);
        this.mLocationTip.setText(String.format("%s", poi));
    }

    @TargetApi(11)
    private void animMarker() {
        if (Build.VERSION.SDK_INT > 11) {
            if (this.animator != null) {
                this.animator.start();
                return;
            }

            this.animator = ValueAnimator.ofFloat(new float[]{(float) (this.mAMapView.getHeight() / 2), (float) (this.mAMapView.getHeight() / 2 - 30)});
            this.animator.setInterpolator(new DecelerateInterpolator());
            this.animator.setDuration(150L);
            this.animator.setRepeatCount(1);
            this.animator.setRepeatMode(ValueAnimator.REVERSE);
            this.animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                public void onAnimationUpdate(ValueAnimator animation) {
                    RLog.d("AMapLocationActivity", "onAnimationUpdate");
                    Float value = (Float) animation.getAnimatedValue();
                    MapLocationActivity.this.mMarker.setPositionByPixels(MapLocationActivity.this.mAMapView.getWidth() / 2, Math.round(value));
                }
            });
            this.animator.addListener(new AnimatorListenerAdapter() {
                public void onAnimationEnd(Animator animation) {
                    MapLocationActivity.this.mMarker.setIcon(MapLocationActivity.this.mBitmapDescriptor);
                }
            });
            this.animator.start();
        }

    }

    protected void onDestroy() {
        this.mAMapView.onDestroy();
        LocationManager.getInstance().setMyLocationChangedListener((IMyLocationChangedListener) null);
        super.onDestroy();
    }
    @Override
    public void onMyLocationChanged(final AMapLocationInfo locationInfo) {
        RLog.d("AMapLocationActivity", "onLocationChanged");
        if (this.mLocationChangedListener != null) {
            this.mHandler.post(new Runnable() {
                public void run() {
                    if (locationInfo != null) {
                        MapLocationActivity.this.mMyLat = MapLocationActivity.this.mLatResult = locationInfo.getLat();
                        MapLocationActivity.this.mMyLng = MapLocationActivity.this.mLngResult = locationInfo.getLng();
                        MapLocationActivity.this.mMyPoi = MapLocationActivity.this.mPoiResult = locationInfo.getStreet() + locationInfo.getPoiname();
                        MapLocationActivity.this.cityCode = locationInfo.getCitycode();
                        MapLocationActivity.this.updateNearByView(MapLocationActivity.this.mMyPoi);
                        Location location = new Location("AMap");
                        location.setLatitude(locationInfo.getLat());
                        location.setLongitude(locationInfo.getLng());
                        location.setTime(locationInfo.getTime());
                        location.setAccuracy(locationInfo.getAccuracy());
                        MapLocationActivity.this.mLocationChangedListener.onLocationChanged(location);
                        LatLng mLatLang = new LatLng(MapLocationActivity.this.mLatResult, MapLocationActivity.this.mLngResult);
                        MapLocationActivity.this.addLocatedMarker(mLatLang, MapLocationActivity.this.mPoiResult);
                        MapLocationActivity.this.mAMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mLatLang, 17.0F));
                    } else {
                        Toast.makeText(MapLocationActivity.this, MapLocationActivity.this.getString(io.rong.imkit.R.string.rc_location_fail), Toast.LENGTH_SHORT).show();
                    }

                }
            });
        }

    }

    private void initNearbyView() {
        this.listViewNearby = (ListView) this.findViewById(io.rong.imkit.R.id.rc_list_nearby);
        this.listLoadingView = (ProgressBar) this.findViewById(io.rong.imkit.R.id.rc_ext_loading);
        this.listViewNearby.setVisibility(View.VISIBLE);
        this.listLoadingView.setVisibility(View.GONE);
        this.mTouchSlop = ViewConfiguration.get(this).getScaledTouchSlop();
        this.listViewNearby.setOnScrollListener(this.onScrollListener);
        this.listViewNearby.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                for (int i = 0; i < MapLocationActivity.this.nearbyListAdapter.getCount(); ++i) {
                    MapLocationActivity.MapNearbyInfo mapNearbyInfo = (MapLocationActivity.MapNearbyInfo) MapLocationActivity.this.nearbyListAdapter.getItem(i);
                    if (i == position) {
                        mapNearbyInfo.setChecked(true);
                        MapLocationActivity.this.mLngResult = mapNearbyInfo.getLongitude();
                        MapLocationActivity.this.mLatResult = mapNearbyInfo.getLatitude();
                        MapLocationActivity.this.mPoiResult = mapNearbyInfo.getPoi();
                        MapLocationActivity.this.updateCheckedMapView(MapLocationActivity.this.mLngResult, MapLocationActivity.this.mLatResult, MapLocationActivity.this.mPoiResult);
                        isStartSelectAddress = true;
                    } else {
                        mapNearbyInfo.setChecked(false);
                    }
                }

                MapLocationActivity.this.nearbyListAdapter.notifyDataSetChanged();
            }
        });
        this.listViewNearby.setOnTouchListener(new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                MapLocationActivity.this.Y = event.getRawY();
                switch (event.getAction()) {
                    case 0:
                        MapLocationActivity.this.downY = MapLocationActivity.this.Y;
                        MapLocationActivity.this.lastY = MapLocationActivity.this.Y;
                        break;
                    case 1:
                        MapLocationActivity.this.Y = 0.0F;
                        if (MapLocationActivity.this.flag == 1) {
                            MapLocationActivity.this.flag = 0;
                            return true;
                        }

                        MapLocationActivity.this.lastY = 0.0F;
                        break;
                    case 2:
                        if (Math.abs(MapLocationActivity.this.Y - MapLocationActivity.this.downY) > (float) MapLocationActivity.this.mTouchSlop) {
                            if (MapLocationActivity.this.Y - MapLocationActivity.this.downY >= 0.0F && MapLocationActivity.this.Y - MapLocationActivity.this.lastY >= 0.0F) {
                                MapLocationActivity.this.lastY = MapLocationActivity.this.Y;
                                return MapLocationActivity.this.handleScrollState(MapLocationActivity.ScrollDirection.SCROLL_DOWN);
                            }

                            MapLocationActivity.this.lastY = MapLocationActivity.this.Y;
                            return MapLocationActivity.this.handleScrollState(MapLocationActivity.ScrollDirection.SCROLL_UP);
                        }
                }

                return false;
            }
        });
    }

    private boolean handleScrollState(MapLocationActivity.ScrollDirection scrollDirection) {
        int minHeight = (int) this.getResources().getDimension(io.rong.imkit.R.dimen.rc_ext_location_nearby_list_min_height);
        int maxHeight = (int) this.getResources().getDimension(io.rong.imkit.R.dimen.rc_ext_location_nearby_list_max_height);
        return scrollDirection == MapLocationActivity.ScrollDirection.SCROLL_DOWN ? this.updateListViewHeight(maxHeight, minHeight) : this.updateListViewHeight(minHeight, maxHeight);
    }

    private boolean updateListViewHeight(int oldHeight, int newHeight) {
        int height = this.listViewNearby.getHeight();
        if (this.listViewNearby != null && this.listViewNearby.getChildAt(0) != null) {
            int top = this.listViewNearby.getChildAt(0).getTop();
            if (top == 0 && Math.abs(this.Y - this.downY) > (float) this.mTouchSlop && this.flag == 0 && height == oldHeight) {
                this.flag = 1;
            }

            if (this.flag == 1) {
                ViewGroup.LayoutParams layoutParams = this.listViewNearby.getLayoutParams();
                layoutParams.height = newHeight;
                this.listViewNearby.setLayoutParams(layoutParams);
                LatLng latLng = new LatLng(this.mLatResult, this.mLngResult);
                if (this.mMarker != null) {
                    this.mMarker.setPosition(latLng);
                }

                android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) ((android.widget.FrameLayout.LayoutParams) this.mLocationTip.getLayoutParams());
                int marginTop;
                int marginLeft;
                int marginRight;
                if (oldHeight < newHeight) {
                    marginTop = RongUtils.dip2px(3.0F);
                    marginLeft = RongUtils.dip2px(20.0F);
                    marginRight = RongUtils.dip2px(20.0F);
                    params.setMargins(marginLeft, marginTop, marginRight, 0);
                    this.mLocationTip.setLayoutParams(params);
                } else {
                    marginTop = RongUtils.dip2px(20.0F);
                    marginLeft = RongUtils.dip2px(20.0F);
                    marginRight = RongUtils.dip2px(20.0F);
                    params.setMargins(marginLeft, marginTop, marginRight, 0);
                    this.mLocationTip.setLayoutParams(params);
                }

                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    private void updateNearByView(final String poi) {
        PoiSearch.Query query = new PoiSearch.Query("", "汽车服务|汽车销售|汽车维修|摩托车服务|餐饮服务|购物服务|生活服务|体育休闲服务|医疗保健服务|住宿服务|风景名胜|商务住宅|政府机构及社会团体|科教文化服务|交通设施服务|金融保险服务|公司企业|道路附属设施|地名地址信息|公共设施", "");
        query.setPageSize(20);
        this.currentPage = 1;
        query.setPageNum(this.currentPage);
        PoiSearch poiSearch = new PoiSearch(this, query);
        double longitude = this.mLngResult;
        double latitude = this.mLatResult;
        if (latitude != 0.0D && longitude != 0.0D) {
            poiSearch.setBound(new PoiSearch.SearchBound(new LatLonPoint(latitude, longitude), 6000));
            poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
                public void onPoiSearched(PoiResult poiResult, int i) {
                    if (poiResult != null && poiResult.getPois().size() > 0) {
                        List<MapNearbyInfo> mapNearbyInfos = new ArrayList();
                        List<PoiItem> poiItemList = poiResult.getPois();
                        MapLocationActivity.MapNearbyInfo nearbyInfo = MapLocationActivity.this.new MapNearbyInfo();
                        nearbyInfo.setName(poi);
                        nearbyInfo.setChecked(true);
                        nearbyInfo.setLongitude(MapLocationActivity.this.mLngResult);
                        nearbyInfo.setLatitude(MapLocationActivity.this.mLatResult);
                        nearbyInfo.setPoi(poi);
                        mapNearbyInfos.add(nearbyInfo);

                        for (int j = 0; j < poiItemList.size(); ++j) {
                            PoiItem poiItem = (PoiItem) poiItemList.get(j);
                            nearbyInfo = MapLocationActivity.this.new MapNearbyInfo(poiItem.getTitle(), poiItem.getSnippet());
                            nearbyInfo.setLongitude(poiItem.getLatLonPoint().getLongitude());
                            nearbyInfo.setLatitude(poiItem.getLatLonPoint().getLatitude());
                            nearbyInfo.setPoi(poiItem.getTitle());
                            mapNearbyInfos.add(nearbyInfo);
                        }

                        MapLocationActivity.this.nearbyListAdapter = MapLocationActivity.this.new NearbyListAdapter(MapLocationActivity.this, mapNearbyInfos);
                        MapLocationActivity.this.listViewNearby.setAdapter(MapLocationActivity.this.nearbyListAdapter);
                        MapLocationActivity.this.listViewNearby.setVisibility(View.VISIBLE);
                        MapLocationActivity.this.listLoadingView.setVisibility(View.GONE);
                    }

                }

                public void onPoiItemSearched(PoiItem poiItem, int i) {
                }
            });
            poiSearch.searchPOIAsyn();
        } else {
            this.listViewNearby.setVisibility(View.VISIBLE);
            this.listLoadingView.setVisibility(View.GONE);
            Toast.makeText(this, this.getResources().getString(io.rong.imkit.R.string.rc_location_fail), Toast.LENGTH_SHORT).show();
        }

    }

    private void loadNextPageNearByView() {
        PoiSearch.Query query = new PoiSearch.Query("", "汽车服务|汽车销售|汽车维修|摩托车服务|餐饮服务|购物服务|生活服务|体育休闲服务|医疗保健服务|住宿服务|风景名胜|商务住宅|政府机构及社会团体|科教文化服务|交通设施服务|金融保险服务|公司企业|道路附属设施|地名地址信息|公共设施", "");
        query.setPageSize(20);
        query.setPageNum(++this.currentPage);
        PoiSearch poiSearch = new PoiSearch(this, query);
        double longitude = this.mLngResult;
        double latitude = this.mLatResult;
        if (latitude != 0.0D && longitude != 0.0D) {
            poiSearch.setBound(new PoiSearch.SearchBound(new LatLonPoint(latitude, longitude), 6000));
            poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
                public void onPoiSearched(PoiResult poiResult, int i) {
                    if (poiResult != null && poiResult.getPois().size() > 0) {
                        List<MapNearbyInfo> mapNearbyInfos = new ArrayList();
                        List<PoiItem> poiItemList = poiResult.getPois();

                        for (int j = 0; j < poiItemList.size(); ++j) {
                            PoiItem poiItem = (PoiItem) poiItemList.get(j);
                            MapLocationActivity.MapNearbyInfo nearbyInfo = MapLocationActivity.this.new MapNearbyInfo(poiItem.getTitle(), poiItem.getSnippet());
                            nearbyInfo.setLongitude(poiItem.getLatLonPoint().getLongitude());
                            nearbyInfo.setLatitude(poiItem.getLatLonPoint().getLatitude());
                            nearbyInfo.setPoi(poiItem.getTitle());
                            mapNearbyInfos.add(nearbyInfo);
                        }

                        MapLocationActivity.this.nearbyListAdapter.addItems(mapNearbyInfos);
                        MapLocationActivity.this.nearbyListAdapter.notifyDataSetChanged();
                    }

                }

                public void onPoiItemSearched(PoiItem poiItem, int i) {
                }
            });
            poiSearch.searchPOIAsyn();
        } else {
            Toast.makeText(this, this.getResources().getString(io.rong.imkit.R.string.rc_location_fail), Toast.LENGTH_SHORT).show();
        }

    }

    public void onClick(View v) {
        if (v.getId() == io.rong.imkit.R.id.rc_ext_my_location) {
            this.handleMyLocation();
        } else if (v.getId() == io.rong.imkit.R.id.rc_action_bar_ok) {
            this.handleOkButton();
        } else if (v.getId() == io.rong.imkit.R.id.rc_search) {
            Intent intent = new Intent(this, SearchLocationActivity.class);
            intent.putExtra("city code", this.cityCode);
            this.startActivityForResult(intent, 1);
        }

    }

    private void handleMyLocation() {
        if (this.mMyPoi != null) {
            this.mAMap.setOnCameraChangeListener((AMap.OnCameraChangeListener) null);
            CameraUpdate update = CameraUpdateFactory.changeLatLng(new LatLng(this.mMyLat, this.mMyLng));
            this.mAMap.animateCamera(update, new AMap.CancelableCallback() {
                public void onFinish() {
                    MapLocationActivity.this.mAMap.setOnCameraChangeListener(MapLocationActivity.this);
                }

                public void onCancel() {
                }
            });
//            this.mLocationTip.setText(this.mMyPoi);
            this.mLatResult = this.mMyLat;
            this.mLngResult = this.mMyLng;
            this.mPoiResult = this.mMyPoi;
            LatLng latLng = new LatLng(this.mLatResult, this.mLngResult);
//            this.updateNearByView(this.mMyPoi);
            if (this.mMarker != null) {
                this.mMarker.setPosition(latLng);
            }
        } else {
            LocationManager.getInstance().updateMyLocation();
        }

    }

    private void handleOkButton() {
        if (this.mLatResult == 0.0D && this.mLngResult == 0.0D && TextUtils.isEmpty(this.mPoiResult)) {
            Toast.makeText(this, this.getString(io.rong.imkit.R.string.rc_location_temp_failed), Toast.LENGTH_SHORT).show();
        } else {
            Intent intent = new Intent();
            intent.putExtra("thumb", this.getMapUrl(this.mLatResult, this.mLngResult));
            intent.putExtra("lat", this.mLatResult);
            intent.putExtra("lng", this.mLngResult);
            intent.putExtra("poi", this.mPoiResult);
            this.setResult(-1, intent);
            this.finish();
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && data != null) {
            isStartSelectAddress = true;
            this.mLngResult = data.getDoubleExtra("longitude", 0.0D);
            this.mLatResult = data.getDoubleExtra("latitude", 0.0D);
            this.mPoiResult = data.getStringExtra("poi");
            this.resetViewHeight();
            this.updateToPosition(this.mLngResult, this.mLatResult, this.mPoiResult, true);
        }

    }

    private void resetViewHeight() {
        if (this.listViewNearby != null) {
            int minHeight = (int) this.getResources().getDimension(io.rong.imkit.R.dimen.rc_ext_location_nearby_list_min_height);
            ViewGroup.LayoutParams layoutParams = this.listViewNearby.getLayoutParams();
            layoutParams.height = minHeight;
            this.listViewNearby.setLayoutParams(layoutParams);
        }

        final android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) ((android.widget.FrameLayout.LayoutParams) this.mLocationTip.getLayoutParams());
        int marginTop = RongUtils.dip2px(20.0F);
        int marginLeft = RongUtils.dip2px(20.0F);
        int marginRight = RongUtils.dip2px(20.0F);
        params.setMargins(marginLeft, marginTop, marginRight, 0);
        this.mLocationTip.post(new Runnable() {
            public void run() {
                MapLocationActivity.this.mLocationTip.setLayoutParams(params);
            }
        });
    }

    private void updateToPosition(double longitude, double latitude, String poi, boolean updateNearby) {
        if (poi != null) {
            this.mLatResult = latitude;
            this.mLngResult = longitude;
            this.mPoiResult = poi;
            this.mLocationTip.setText(this.mPoiResult);
            if (updateNearby) {
                this.updateNearByView(this.mPoiResult);
            }

            final LatLng latLng = new LatLng(this.mLatResult, this.mLngResult);
            CameraUpdate update = CameraUpdateFactory.changeLatLng(latLng);
            this.mAMap.setOnCameraChangeListener((AMap.OnCameraChangeListener) null);
            this.mAMap.animateCamera(update, new AMap.CancelableCallback() {
                public void onFinish() {
                    MapLocationActivity.this.mAMap.setOnCameraChangeListener(MapLocationActivity.this);
                    if (MapLocationActivity.this.mMarker != null) {
                        MapLocationActivity.this.mMarker.setPosition(latLng);
                    }

                }

                public void onCancel() {
                }
            });
        }

    }

    private void updateCheckedMapView(double longitude, double latitude, String poi) {
        this.updateToPosition(longitude, latitude, poi, false);
    }

    protected void onResume() {
        super.onResume();
        this.mAMapView.onResume();
    }

    protected void onPause() {
        super.onPause();
        this.mAMapView.onPause();
    }

    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        this.mAMapView.onSaveInstanceState(outState);
    }

    private static enum ScrollDirection {
        SCROLL_UP,
        SCROLL_DOWN;

        private ScrollDirection() {
        }
    }

    private class MapNearbyInfo {
        String name;
        String address;
        double latitude;
        double longitude;
        String poi;
        boolean checked;

        public MapNearbyInfo() {
        }

        public MapNearbyInfo(String name, String address) {
            this.name = name;
            this.address = address;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getName() {
            return this.name;
        }

        public void setAddress(String address) {
            this.address = address;
        }

        public String getAddress() {
            return this.address;
        }

        public double getLongitude() {
            return this.longitude;
        }

        public void setLongitude(double longitude) {
            this.longitude = longitude;
        }

        public double getLatitude() {
            return this.latitude;
        }

        public void setLatitude(double latitude) {
            this.latitude = latitude;
        }

        public String getPoi() {
            return this.poi;
        }

        public void setPoi(String poi) {
            this.poi = poi;
        }

        public void setChecked(boolean checked) {
            this.checked = checked;
        }

        public boolean getChecked() {
            return this.checked;
        }
    }

    private class NearbyListAdapter extends BaseAdapter {
        List<MapNearbyInfo> nearbyInfoList = new ArrayList();
        Context context;

        public NearbyListAdapter(Context context, List<MapNearbyInfo> nearbyInfoList) {
            this.context = context;
            this.nearbyInfoList = nearbyInfoList;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            MapLocationActivity.MapNearbyInfo mapNearbyInfo = (MapLocationActivity.MapNearbyInfo) this.nearbyInfoList.get(position);
            MapLocationActivity.NearbyListAdapter.NearbyViewHolder nearbyViewHolder;
            if (convertView == null) {
                nearbyViewHolder = new MapLocationActivity.NearbyListAdapter.NearbyViewHolder();
                convertView = View.inflate(this.context, io.rong.imkit.R.layout.rc_map_nearby_info_item, (ViewGroup) null);
                nearbyViewHolder.tvNearbyName = (TextView) convertView.findViewById(io.rong.imkit.R.id.rc_nearby_name);
                nearbyViewHolder.tvNearbyAddress = (TextView) convertView.findViewById(io.rong.imkit.R.id.rc_nearby_address);
                nearbyViewHolder.ivNearbyChecked = (ImageView) convertView.findViewById(io.rong.imkit.R.id.rc_nearby_checked);
                convertView.setTag(nearbyViewHolder);
            } else {
                nearbyViewHolder = (MapLocationActivity.NearbyListAdapter.NearbyViewHolder) convertView.getTag();
            }

            if (position == 0) {
                nearbyViewHolder.tvNearbyAddress.setVisibility(View.GONE);
                nearbyViewHolder.tvNearbyName.setText(mapNearbyInfo.getName());
            } else {
                nearbyViewHolder.tvNearbyAddress.setVisibility(View.VISIBLE);
                nearbyViewHolder.tvNearbyName.setText(mapNearbyInfo.getName());
                nearbyViewHolder.tvNearbyAddress.setText(mapNearbyInfo.getAddress());
            }

            if (mapNearbyInfo.getChecked()) {
                nearbyViewHolder.ivNearbyChecked.setVisibility(View.VISIBLE);
            } else {
                nearbyViewHolder.ivNearbyChecked.setVisibility(View.GONE);
            }

            return convertView;
        }

        public Object getItem(int position) {
            return this.nearbyInfoList != null && this.nearbyInfoList.size() > 0 ? this.nearbyInfoList.get(position) : null;
        }

        public long getItemId(int position) {
            return (long) position;
        }

        public int getCount() {
            return this.nearbyInfoList.size();
        }

        public void addItems(List<MapNearbyInfo> nearbyInfoList) {
            if (this.nearbyInfoList != null) {
                this.nearbyInfoList.addAll(nearbyInfoList);
            }

        }

        class NearbyViewHolder {
            TextView tvNearbyName;
            TextView tvNearbyAddress;
            ImageView ivNearbyChecked;

            NearbyViewHolder() {
            }
        }
    }

}
