package com.elephant.framework.galaxy.aroad.module.traffic.controller;

import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

/**
 * *
 *
 * @description: 通过路段编码查询线型经纬度
 * @author : Xingyuyang
 * @date : 2019/4/19 0019 9:37
 */
@RestController
@AllArgsConstructor
@RequestMapping("/getTrafficCoordinates")
public class TCoordinatesByCodeAndM {
  public static String replaceLast(String text, String regex, String replacement) {
    return text.replaceFirst("(?s)" + regex + "(?!.*?" + regex + ")", replacement);
  }

  /**
   * *
   *
   * @author : Xingyuyang
   * @date : 2019/4/19 0019 9:37
   * @param roadInfo:
   * @return
   */
  @GetMapping("/{roadInfo}")
  String getCoordinatesByRoadCode(@PathVariable("roadInfo") String roadInfo) {
    String RoadCode = roadInfo.split(",")[0];
    String spotM=roadInfo.split(",")[1].replace("K","");
    String urls = "http://47.105.99.44:8080/sdlkapi/LRS/GetRoadSectionGeometry";
    String result = ""; // 访问返回结果
    BufferedReader read = null; // 读取访问结果
    try {
      URL realurl = new URL(urls + "?roadcode=" + RoadCode+"&fromM="+spotM);
      // 打开连接
      URLConnection connection = realurl.openConnection();
      // 设置通用的请求属性
      connection.setRequestProperty("accept", "*/*");
      connection.setRequestProperty("connection", "Keep-Alive");
      connection.setRequestProperty(
          "user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
      // 建立连接
      connection.connect();
      // 获取所有响应头字段
      Map<String, List<String>> map = connection.getHeaderFields();
      // 遍历所有的响应头字段，获取到cookies等
      for (String key : map.keySet()) {
        System.out.println(key + "--->" + map.get(key));
      }
      // 定义 BufferedReader输入流来读取URL的响应
      read = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
      String line; // 循环读取
      while ((line = read.readLine()) != null) {
        result += line;
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (read != null) {
        try {
          read.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    //    String a =
    //        "[\n"
    //            + "  {\n"
    //            + "    \"longitude\": 121.35026615250384,\n"
    //            + "    \"latitude\": 32.376700873233865\n"
    //            + "  },\n"
    //            + "  {\n"
    //            + "    \"longitude\": 118.16719561602959,\n"
    //            + "    \"latitude\": 34.917386856816904\n"
    //            + "  },\n"
    //            + "  {\n"
    //            + "    \"longitude\": 116.68559017702764,\n"
    //            + "    \"latitude\": 36.492319275295657\n"
    //            + "  },\n"
    //            + "  {\n"
    //            + "    \"longitude\": 119.74509279731635,\n"
    //            + "    \"latitude\": 36.292381987051705\n"
    //            + "  }\n"
    //            + "]";
    //    List<RoadCodeCoordinateDto> results = new ArrayList<>();
    //    String[] aA = a.replace("{", "").replace("}", "").replace("(", "").replace(")",
    // "").split(",");
    //    RoadCodeCoordinateDto roadCodeCoordinateDto = new RoadCodeCoordinateDto();
    //    List<String> x = new ArrayList<>();
    //    List<String> y = new ArrayList<>();
    //    Map<String,String> xy = new HashMap<>();
    //    for (int i = 0; i < aA.length; i++) {
    //      if (i % 2 == 0) {
    //        x.add(aA[i]);
    //      } else {
    //        y.add(aA[i]);
    //      }
    //    }
    //    //    roadCodeCoordinateDto.setLongituateDto););
    //    ////    results.add(roadCodeCoordinateDto);
    //    for(int i = 0;i<x.size();i++){
    //      xy.put(x.get(i), y.get(i));
    //    }
    //    roadCodeCoordinateDto.setLatitude(new BigDecimal(aA[i]));
    //    roadCodeCoordinateDto.setLongitude(new BigDecimal(aA[i]));
    result = result.replace("\\r", "").replace("\\n", "").replace("\\", "").replaceFirst("\"", "");
    result = replaceLast(result, "\"", "");
    return result;
  }
}
