package shop.alien.store.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.store.config.GaoDeMapUtil;

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

/**
 * 高德接口
 *
 * @author ssk
 * @version 1.0
 * @date 2025/3/18 13:25
 */
@Api(tags = {"高德接口"})
@Slf4j
@CrossOrigin
@RequestMapping("/gaode")
@RestController
@RequiredArgsConstructor
public class GaoDeController {

    private final GaoDeMapUtil gaodeMapUtil;

    @ApiOperation("根据详细地址获取经纬度")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "address", value = "地址", dataType = "String", paramType = "query", required = true)
    })
    @GetMapping("/getLonAndLatByAddress")
    public R<Map<String, Object>> getLonAndLatByAddress(@RequestParam(value = "address") String address) {
        String lonAndLatByAddress = gaodeMapUtil.getLonAndLatByAddress(address);
        if (StringUtils.isNotEmpty(lonAndLatByAddress)) {
            String[] split = lonAndLatByAddress.split(",");
            Map<String, Object> map = new HashMap<>();
            map.put("longitude", split[0]);
            map.put("latitude", split[1]);
            return R.data(map);
        }
        return null;
    }

    @ApiOperation("根据关键词获取提示词")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "addressName", value = "地址", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "city", value = "城市", dataType = "String", paramType = "query", required = false)
    })
    @GetMapping("/getInputPrompt")
    public R<JSONObject> getInputPrompt(@RequestParam(value = "addressName") String address, @RequestParam(value = "city", required = false) String city) {
        JSONObject inputPrompt = gaodeMapUtil.getInputPrompt(address, city);
        return R.data(inputPrompt);
    }

    @ApiOperation("获取行政区")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adCode", value = "城市编码", dataType = "String", paramType = "query", required = false)
    })
    @GetMapping("/getDistrict")
    public R<JSONObject> getDistrict(@RequestParam(value = "adCode", required = false) String adCode) {
        JSONObject inputPrompt = gaodeMapUtil.getDistrict(adCode);
        return R.data(inputPrompt);
    }

    @ApiOperation("获取附近商家、建筑信息")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "longitude", value = "经度", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "latitude", value = "纬度", dataType = "String", paramType = "query", required = true)
    })
    @GetMapping("/getNearList")
    public R<JSONArray> getNearList(String longitude, String latitude) {
        JSONArray result = gaodeMapUtil.getNear(longitude, latitude);
        return R.data(result);
    }

    @ApiOperation("获取10公里范围内的商场")
    @ApiOperationSupport(order = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "longitude", value = "经度", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "latitude", value = "纬度", dataType = "String", paramType = "query", required = true)
    })
    @GetMapping("/getNearbyShoppingMalls")
    public R<JSONArray> getNearbyShoppingMalls(@RequestParam(value = "longitude") String longitude,
                                               @RequestParam(value = "latitude") String latitude) {
        JSONArray result = gaodeMapUtil.getNearbyShoppingMalls(longitude, latitude);
        return R.data(result);
    }


    @ApiOperation("根据经纬度获取点信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "longitude", value = "经度", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "latitude", value = "纬度", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "radius", value = "查询范围,默认:0", dataType = "String", paramType = "query", required = false),
            @ApiImplicitParam(name = "extensions", value = "查询方式:base基础,all全部,默认all", dataType = "String", paramType = "query", required = false)
    })
    @GetMapping("/getAddressByLonAndLat")
    public R<Map<String,String>> getAddressByLonAndLat(@RequestParam(value = "longitude") String longitude,
                                                        @RequestParam(value = "latitude") String latitude,
                                                        @RequestParam(value = "radius",defaultValue = "0") String radius,
                                                        @RequestParam(value = "extensions",defaultValue = "all") String extensions) {
        Map<String,String> addressByLonAndLat = gaodeMapUtil.getAddressByLonAndLat(longitude, latitude, radius, extensions);
        if(!addressByLonAndLat.isEmpty()){
            return R.data(addressByLonAndLat);
        }else {
            return R.fail("获取失败");
        }

    }

    @ApiOperation("获取两个经纬度的距离")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "longitudeOne", value = "经度1", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "latitudeOne", value = "纬度1", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "longitudeTwo", value = "纬度2", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "latitudeTwo", value = "纬度2", dataType = "String", paramType = "query", required = true)
    })
    @GetMapping("/getDistance")
    public R<Double> getDistance(String longitudeOne, String latitudeOne, String longitudeTwo, String latitudeTwo) {
        return R.data(gaodeMapUtil.getDistance(longitudeOne, latitudeOne, longitudeTwo, latitudeTwo));
    }

    @ApiOperation("获取两个经纬度的距离(直线距离)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "longitudeOne", value = "经度1", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "latitudeOne", value = "纬度1", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "longitudeTwo", value = "纬度2", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "latitudeTwo", value = "纬度2", dataType = "String", paramType = "query", required = true)
    })
    @GetMapping("/getDistanceStraightLine")
    public R<Double> getDistanceStraightLine(String longitudeOne, String latitudeOne, String longitudeTwo, String latitudeTwo) {
        return R.data(gaodeMapUtil.getDistanceStraightLine(longitudeOne, latitudeOne, longitudeTwo, latitudeTwo));
    }

}
