package org.jeecg.modules.system.app.controller;


import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.utils.UuidUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.baiduAddress.BaiDuError;
import org.jeecg.common.aspect.baiduAddress.BaiduMapApiHelper;
import org.jeecg.common.aspect.baiduAddress.GeoCode;
import org.jeecg.common.aspect.baiduAddress.RegionSearchModel;
import org.jeecg.modules.system.app.entity.BaiduAddress;
import org.jeecg.modules.system.app.service.IBaiduAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@Tag(name="百度地图")
@Slf4j
@RestController
@RequestMapping
public class BaiDuApi {

    @Autowired
    BaiduMapApiHelper baiduMapApiHelper;

    @Autowired
    IBaiduAddressService iBaiduAddressService;


    @Operation(summary="百度地图-通过地址查询")
    @GetMapping(value = "/findByAddress")
    public Result<RegionSearchModel> findByAddress(@RequestParam(name="address",required=true) String address) {
        if (StringUtils.isEmpty(address)){
            return Result.error("请求参数地址为空");
        }
        BaiduAddress baiduAddress =iBaiduAddressService.findByAddressName(address);
        GeoCode geocoding =null;
        if (baiduAddress!=null&&StringUtils.isNotEmpty(baiduAddress.getBaiduResponse())){
            geocoding = JSONObject.toJavaObject(JSONObject.parseObject(baiduAddress.getBaiduResponse()), GeoCode.class);
        }else {
            geocoding = baiduMapApiHelper.geocoding(address);

            if (geocoding==null){
                return Result.error("经纬度获取失败,请联系管理员");
            }else if (geocoding.getStatus()!=0){
                BaiDuError byType = BaiDuError.getByType(geocoding.getStatus());
                if (byType!=null){
                    return Result.error(geocoding.getStatus(), byType.getValue());
                }else {
                    return Result.error(geocoding.getStatus(), "未知错误信息联系管理员");
                }
            }else {
                String baiduResponse = JSONObject.toJSONString(geocoding);
                BaiduAddress baiduAddressNew = new BaiduAddress();
                baiduAddressNew.setId(UuidUtils.getUUID());
                baiduAddressNew.setAddressName(address);
                baiduAddressNew.setBaiduResponse(baiduResponse);
                iBaiduAddressService.save(baiduAddressNew);
            }
        }

        RegionSearchModel regionSearchModel = getRegionSearchModel(geocoding,address);
        if (regionSearchModel==null||ObjectUtils.isEmpty(regionSearchModel.getLatitude())||ObjectUtils.isEmpty(regionSearchModel.getLongitude())){
            return Result.error("经纬度获取失败");
        }
        return Result.OK(regionSearchModel);
    }

    public RegionSearchModel getRegionSearchModel(GeoCode geocoding,String addressName) {
        RegionSearchModel regionSearchModel = new RegionSearchModel();
        regionSearchModel.setAddressName(addressName);
        if (geocoding.getResult()!=null&&geocoding.getResult().getLocation()!=null){
            if (ObjectUtils.isNotEmpty(geocoding.getResult().getLocation().getLat())){
                regionSearchModel.setLatitude(geocoding.getResult().getLocation().getLat());
            }
            if (ObjectUtils.isNotEmpty(geocoding.getResult().getLocation().getLng())){
                regionSearchModel.setLongitude(geocoding.getResult().getLocation().getLng());
            }
        }
        return regionSearchModel;
    }
}
