package com.sikey.wa04.api.location;

import com.sikey.wa04.business.infrastructure.adapter.RequestParametersBuilder;
import com.sikey.wa04.business.infrastructure.configuration.AmapConfig;
import jakarta.annotation.Resource;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

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

@SpringBootTest
public class AmapRequestServiceTests {

    @Resource
    private AmapConfig amapConfig;

    @Test
    public void example() throws Exception {
        String requestUrl = "https://restapi.amap.com/v5/position/IoT?key=%s&accesstype=1&cdma=1&network=GSM&bts=13824,1,16689,1674783,575440,-52,0&show_fields=addressComponent"
                .formatted(amapConfig.getKey());
        Request post = new Request.Builder()
                .url(requestUrl)
                .post(RequestBody.create("{}".getBytes()))
                .build();
        OkHttpClient httpClient = new OkHttpClient();
        Response response = httpClient.newCall(post).execute();
        if (!response.isSuccessful()) {
            throw new RuntimeException("Unexpected code " + response);
        }

        /**
         * {
         *     "status": "1",
         *     "info": "OK",
         *     "infocode": "10000",
         *     "position": {
         *         "location": "116.310923,39.963495",
         *         "radius": 550
         *     },
         *     "addressComponent": {
         *         "country": "中国",
         *         "province": "北京市",
         *         "city": "北京市",
         *         "district": "海淀区",
         *         "citycode": "010",
         *         "adcode": "110108",
         *         "street": "北三环西路",
         *         "road": "北三环西路辅路",
         *         "poi": "美天美容美体"
         *     }
         * }
         */
        System.out.println(response.body().string());
        System.out.println(response);
    }


    @Test
    public void request() throws Exception {
        Map<String, String> queryMap = new HashMap<>();
        queryMap.put("accesstype", "1");
        queryMap.put("cdma", "0");
        queryMap.put("network", "NR");
        queryMap.put("diu", "352315052834187");
        queryMap.put("bts", "460,11,59478,8435468,-99");
        queryMap.put("nearbts", "460,11,0,186181011,-102|");
        queryMap.put("historybts", "");
        queryMap.put("show_fields", "addressComponent");
        queryMap.put("key", amapConfig.getKey());
        RequestParametersBuilder builder = new RequestParametersBuilder(queryMap);

        String query = builder.queryEncode();
        String requestUrl = String.format("https://restapi.amap.com/v5/position/IoT?%s", query);

        Request get = new Request.Builder()
                .url(requestUrl)
                .post(RequestBody.create("{}".getBytes()))
                .build();
        OkHttpClient httpClient = new OkHttpClient();
        Response response = httpClient.newCall(get).execute();
        if (!response.isSuccessful()) {
            throw new RuntimeException("Unexpected code " + response);
        }

        // {"status":"0","info":"INVALID_PARAMS","infocode":"20000"}
        System.out.println(response.body().string());
        System.out.println(response);
    }

}

