package com.atmob.map_amap_android.poi;

import android.content.Context;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItemV2;
import com.amap.api.services.poisearch.PoiResultV2;
import com.amap.api.services.poisearch.PoiSearchV2;
import com.atmob.map_amap_android.util.LogUtil;
import com.atmob.map_amap_android.util.ParamUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

public class PoiHelper {

    static final String TAG = "PoiHelper";

    public static void paginatePoiSearch(Context context, MethodCall call, MethodChannel.Result result) {
        try {
            Map<String, Object> arguments = call.arguments();
            Map<String, Object> queryMap = (Map<String, Object>) arguments.get("query");
            if (queryMap == null || queryMap.isEmpty()) {
                result.error("query is null", "query is null", null);
                return;
            }
            Integer pageSize = ParamUtil.getInt(arguments, "pageSize");
            Integer pageNum = ParamUtil.getInt(arguments, "pageNum");
            if (pageNum == null || pageSize == null) {
                result.error("pageNum or pageSize is null", "pageNum or pageSize is null", null);
                return;
            }

            String keyWord = ParamUtil.getString(queryMap, "keyWord");
            String ctgr = ParamUtil.getString(queryMap, "ctgr");
            String city = ParamUtil.getString(queryMap, "city");


            PoiSearchV2.Query query = new PoiSearchV2.Query(keyWord, ctgr, city);
            query.setPageSize(pageSize);
            query.setPageNum(pageNum);
            PoiSearchV2 poiSearch = new PoiSearchV2(context, query);
            Map<String, Object> bound = (Map<String, Object>) arguments.get("bound");
            if (bound != null && !bound.isEmpty()) {
                String type = ParamUtil.getString(bound, "type");
                //以中心点半径方式检索
                if (Objects.equals(type, "circle")) {
                    int radius = ParamUtil.getInt(bound, "radius");
                    Map<String, Object> center = (Map<String, Object>) bound.get("center");
                    if (center == null || center.isEmpty()) {
                        result.error("center is null", "center is null", null);
                        return;
                    }
                    double latitude = ParamUtil.getDouble(center, "latitude", 0);
                    double longitude = ParamUtil.getDouble(center, "longitude", 0);
                    LogUtil.d(TAG, "paginatePoiSearch: latitude=" + latitude + " longitude=" + longitude + " radius=" + radius);
                    final PoiSearchV2.SearchBound searchBound = new PoiSearchV2.SearchBound(new LatLonPoint(latitude, longitude), radius);
                    poiSearch.setBound(searchBound);
                }
            }
            poiSearch.setOnPoiSearchListener(new PoiSearchV2.OnPoiSearchListener() {
                @Override
                public void onPoiSearched(PoiResultV2 poiResultV2, int errorCode) {
                    //返回POI搜索异步处理的结果。
                    LogUtil.d(TAG, "onPoiSearched: errorCode=" + errorCode + " poiResultV2=" + poiResultV2);
                    if (errorCode == 1000) {
                        //成功
                        ArrayList<PoiItemV2> pois = poiResultV2.getPois();
                        //返回list
                        ArrayList<Map<String, Object>> list = new ArrayList<>();
                        for (PoiItemV2 poiItemV2 : pois) {
                            Map<String, Object> resultMap = new HashMap<>();
                            resultMap.put("title", poiItemV2.getTitle());
                            String address = poiItemV2.getProvinceName() + poiItemV2.getCityName() + poiItemV2.getAdName() + poiItemV2.getSnippet();
                            resultMap.put("address", address);
                            resultMap.put("latitude", poiItemV2.getLatLonPoint().getLatitude());
                            resultMap.put("longitude", poiItemV2.getLatLonPoint().getLongitude());
                            list.add(resultMap);
                            LogUtil.d(TAG, "onPoiSearched: resultMap=" + resultMap);
                        }
                        try {
                            result.success(list);
                        } catch (Exception e) {
                            LogUtil.e(TAG, "onPoiSearched error", e);
                            result.error("onPoiSearched error", e.getMessage(), null);
                        }
                    } else {
                        result.error("onPoiSearched error", "errorCode=" + errorCode, null);
                    }
                }

                @Override
                public void onPoiItemSearched(PoiItemV2 poiItemV2, int onPoiItemSearched) {
                    //poi id搜索的结果回调
                    LogUtil.d(TAG, "onPoiItemSearched: onPoiItemSearched=" + onPoiItemSearched + " poiItemV2=" + poiItemV2);
                }
            });
            poiSearch.searchPOIAsyn();
        } catch (Exception e) {
            result.error("paginatePoiSearch error", e.getMessage(), null);
        }
    }

}
