package com.izouma.dingdong;

import cn.hutool.core.util.ObjectUtil;
import com.izouma.dingdong.domain.merchant.Merchant;
import com.izouma.dingdong.repo.merchant.MerchantRepo;
import com.izouma.dingdong.utils.MapUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.stream.Collectors;

@RunWith(SpringRunner.class)
@SpringBootTest
public class MapTest {

    @Autowired
    private MerchantRepo merchantRepo;

    @Test
    public void test() {

//         TencentLocationManagerOptions.setKey("LQTBZ-EEUCU-UX7V4-4CY7M-NROM7-NLFI5");

//         MapView
        // System.out.println(b);

        //System.out.println(TencentLocationUtils.distanceBetween(1.0, 1.0, 2.0, 2.0));
//        TencentLocationRequest tencentLocationRequest = TencentLocationRequest.create();
//        System.out.println(tencentLocationRequest.getRequestLevel());

//        TencentLocationUtils.distanceBetween(31.981746 ,118.734661,31.990671 , 118.754795);

        List<Merchant> collect = merchantRepo.findAll().stream()
                .filter(s -> ObjectUtil.isNotNull(s.getLongitude()) && ObjectUtil.isNotNull(s.getLatitude()))
                .collect(Collectors.toList());
        for (Merchant m : collect) {
            System.out.println(m.getId() + "---->" + MapUtils.distance(118.734661, 31.981746, m.getLongitude(), m.getLatitude()));
        }
    }

    @Test
    public void testUrl() {
        String key = "LQTBZ-EEUCU-UX7V4-4CY7M-NROM7-NLFI5";
//        Map<String, Object> resultMap = new HashMap<String, Object>();

        List<Merchant> collect = merchantRepo.findAll().stream()
                .filter(s -> ObjectUtil.isNotNull(s.getLongitude()) && ObjectUtil.isNotNull(s.getLatitude()))
                .collect(Collectors.toList());

        StringBuilder add = new StringBuilder();
        for (Merchant m : collect) {
            System.out.println(m.getId());
            add.append(m.getLatitude()).append(",").append(m.getLongitude()).append(";");
        }
        String substring = add.substring(0, add.length() - 1);
        System.out.println(substring);

        // 参数解释：lng：经度，lat：维度。KEY：腾讯地图key，get_poi：返回状态。1返回，0不返回
        String urlString = "http://apis.map.qq.com/ws/distance/v1/matrix?model=walking&from=" + 31.981746 + "," + 118.734661 +
                "&to=" + substring
                + "&key=" + key;
        System.out.println(urlString);
        String result = "";
        try {
            URL url = new URL(urlString);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            // 腾讯地图使用GET
            conn.setRequestMethod("GET");
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line;
            // 获取地址解析结果
            while ((line = in.readLine()) != null) {
                result += line + "\n";
            }
            in.close();
        } catch (Exception e) {
            e.getMessage();
        }

        System.out.println(result);
/*        // 转JSON格式
        JSONObject jsonObject = JSONObject.parseObject(result);
        // 获取地址（行政区划信息） 包含有国籍，省份，城市
        JSONObject adInfo = jsonObject.getJSONObject("ad_info");
        resultMap.put("nation", adInfo.get("nation"));
        resultMap.put("nationCode", adInfo.get("nation_code"));
        resultMap.put("province", adInfo.get("province"));
        resultMap.put("provinceCode", adInfo.get("adcode"));
        resultMap.put("city", adInfo.get("city"));
        resultMap.put("cityCode", adInfo.get("city_code"));

        System.out.println(resultMap);*/
    }
}
