package com.ruoyi.ibs.grid.service.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.ruoyi.ibs.grid.config.BaiduApiProperties;
import com.ruoyi.ibs.grid.domain.dto.BaiduResponseDTO;
import com.ruoyi.ibs.grid.service.BaiduApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import javax.annotation.Resource;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
@Slf4j
public class BaiduApiServiceImpl implements BaiduApiService {
    @Resource
    private BaiduApiProperties baiduApiProperties;

    @Resource
    private RestTemplate restTemplate;

    @Override
    public BaiduResponseDTO geocoding(String address) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String format = simpleDateFormat.format(new Date().getTime());

        // 使用 UriComponentsBuilder 构建 URL，但不进行编码
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(baiduApiProperties.getGeocodingUrl())
                .queryParam("address", address)
                .queryParam("output", "json")
                .queryParam("ak", baiduApiProperties.getAk())
//                .queryParam("ret_coordtype", "gcj02ll")
                .encode(StandardCharsets.UTF_8);

        // 创建 URI，使用 encode(false) 来防止二次编码
        URI uri = builder.build(false).toUri();

        HttpHeaders headers = new HttpHeaders();
        headers.set("Accept", "application/json");

        HttpEntity<?> entity = new HttpEntity<>(headers);

        ResponseEntity<BaiduResponseDTO> response = restTemplate.exchange(
                uri,
                HttpMethod.GET,
                entity,
                BaiduResponseDTO.class
        );
        log.info(String.valueOf(response.getBody()));
        return response.getBody();
    }

    /**
     * @param address
     * @param ak
     * @return
     */
    @Override
    public JsonNode geocoding(String address, String ak) {
        // 使用 UriComponentsBuilder 构建 URL，但不进行编码
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(baiduApiProperties.getGeocodingUrl())
                .queryParam("address", address)
                .queryParam("output", "json")
                .queryParam("ak", ak)
//                .queryParam("ret_coordtype", "gcj02ll")
                .encode(StandardCharsets.UTF_8);

        // 创建 URI，使用 encode(false) 来防止二次编码
        URI uri = builder.build(false).toUri();

        HttpHeaders headers = new HttpHeaders();
        headers.set("Accept", "application/json");

        HttpEntity<?> entity = new HttpEntity<>(headers);

        ResponseEntity<JsonNode> response = restTemplate.exchange(
                uri,
                HttpMethod.GET,
                entity,
                JsonNode.class
        );
        log.info(String.valueOf(response.getBody()));
        return response.getBody();
    }

    /**
     * @param keyword
     * @param regionCode
     * @return
     */
    @Override
    public JsonNode placeSearch(String region,String address) {
        // 使用 UriComponentsBuilder 构建 URL，但不进行编码
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(baiduApiProperties.getPlaceSearchUrl())
                .queryParam("query", address)
                .queryParam("region", region)
                .queryParam("output", "json")
                .queryParam("city_limit", "true")
                .queryParam("page_size", 20)
                .queryParam("ak", baiduApiProperties.getAk())
                .encode(StandardCharsets.UTF_8);

        // 创建 URI，使用 encode(false) 来防止二次编码
        URI uri = builder.build(false).toUri();
        log.info(String.valueOf(uri));
        HttpHeaders headers = new HttpHeaders();
        headers.set("Accept", "application/json");

        HttpEntity<?> entity = new HttpEntity<>(headers);

        ResponseEntity<JsonNode> response = restTemplate.exchange(
                uri,
                HttpMethod.GET,
                entity,
                JsonNode.class
        );
        log.info(String.valueOf(response.getBody()));
        return response.getBody();
    }

    /**
     * @param address
     * @return
     */
    @Override
    public JsonNode placeSearch(String address) {
        // 使用 UriComponentsBuilder 构建 URL，但不进行编码
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(baiduApiProperties.getPlaceSearchUrl())
                .queryParam("query", address)
                .queryParam("region", "浙江")
                .queryParam("output", "json")
                .queryParam("city_limit", "true")
                .queryParam("page_size", 20)
                .queryParam("ak", baiduApiProperties.getAk())
                .encode(StandardCharsets.UTF_8);

        // 创建 URI，使用 encode(false) 来防止二次编码
        URI uri = builder.build(false).toUri();
        log.info(String.valueOf(uri));
        HttpHeaders headers = new HttpHeaders();
        headers.set("Accept", "application/json");

        HttpEntity<?> entity = new HttpEntity<>(headers);

        ResponseEntity<JsonNode> response = restTemplate.exchange(
                uri,
                HttpMethod.GET,
                entity,
                JsonNode.class
        );
        log.info(String.valueOf(response.getBody()));
        return response.getBody();
    }
}
