package com.gree.mall.miniapp.logic.common;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.gree.mall.miniapp.bean.common.RegionBean;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@Service
public class LbsAmapLogic {

    private static final String lbsWebKey = "b772f8b0ace6bc96c04ae8e48f241e36";
    /**
     * 根据经纬度反查省市区街道
     * @Param lng,lat
     * @Return {"area":"番禺区","province":"广东省","city":"广州市","street":"大龙街道"}
     */
    public RegionBean getRegionByLngLat(String lnglat){
        //String lnglat = lng + "," + lat;
        //String lbsWebKey = "b772f8b0ace6bc96c04ae8e48f241e36";
        String url = "https://restapi.amap.com/v3/geocode/regeo?key="+lbsWebKey+"&location="+lnglat;
        String s = HttpUtil.get(url);
        JSONObject jsonObject = new JSONObject(s);
        String status = jsonObject.getStr("status");
        if(!status.equals("1")){
            return null;
        }
        JSONObject regeocode = jsonObject.getJSONObject("regeocode");
        JSONObject addressComponent = regeocode.getJSONObject("addressComponent");
        Map<String,String> map = new HashMap<>();
        map.put("province",addressComponent.getStr("province"));
        if(addressComponent.getStr("city").equals("[]")) {
            map.put("city", addressComponent.getStr("province"));
        }else{
            map.put("city", addressComponent.getStr("city"));
        }
        if(addressComponent.getStr("district").equals("[]")) {
            map.put("area", addressComponent.getStr("city"));
        }else{
            map.put("area", addressComponent.getStr("district"));
        }
        map.put("street",addressComponent.getStr("township"));
        //System.out.print(new JSONObject(map).toString());
        log.info("根据经纬度反查地址,经纬度:{},反查的地址：{}",lnglat,new JSONObject(addressComponent));

        String street = map.get("street");
        String province = map.get("province");
        String city = map.get("city");
        String area = map.get("area");
//        LbsAmap lbsAmap = this.getByRegion(province, city, area, street);
//        if(lbsAmap == null){
//            return null;
//        }
//        RegionBean regionBean = BeanUtil.copyProperties(lbsAmap, RegionBean.class);
        RegionBean regionBean = new RegionBean();
        regionBean.setLnglat(lnglat);
        regionBean.setProvinceName(province);
        regionBean.setCityName(city);
        regionBean.setAreaName(area);
        regionBean.setName(street);
        return regionBean;
    }


    /**
     * 根据地址反查经纬度
     * @Param 中文地址(最好详细)
     * @return lng,lat
     */
    public String getLocationByAddress(String address){
        //String lbsWebKey = "b772f8b0ace6bc96c04ae8e48f241e36";
        //String address = "广东省广州市天河区华景新城";
        String url = "https://restapi.amap.com/v3/geocode/geo?key="+lbsWebKey+"&address="+address;
        String s = HttpUtil.get(url);
        JSONObject jsonObject = new JSONObject(s);
        String status = jsonObject.getStr("status");
        if(!status.equals("1")){
            return null;
        }
        JSONArray geocodes = jsonObject.getJSONArray("geocodes");
        if(CollectionUtils.isEmpty(geocodes))
            return null;
        Map<String, Object> map = (Map<String, Object>) geocodes.get(0);
        String location = (String)map.get("location");
        log.info("根据地址【{}】反查经纬度【{}】",address,new JSONObject(map));
        return location;
    }



}
