package com.sifang.modules.order.utils;


import com.google.gson.*;
import com.google.gson.internal.LinkedTreeMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.sql.SQLOutput;
import java.text.DecimalFormat;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

public class GetDistance {
    private static Logger logger =  LoggerFactory.getLogger(GetDistance.class);

    public static double getDistance(String startLonLat, String endLonLat){
        double d;
        DecimalFormat df=new DecimalFormat("0.00");
        String sendGet = sendGet("https://restapi.amap.com/v3/direction/driving","origin="+startLonLat+"&destination="+endLonLat+"&key="+KEY+"&extensions=all");
     //   logger.info(sendGet);
        Gson gson = new Gson();

        JsonObject json = gson.fromJson(sendGet, JsonObject.class);
        String status = json.get("status").getAsString();
        if(status.equals("1")){
            JsonObject json2 = gson.fromJson(json.get("route"), JsonObject.class);
            JsonArray json3 = gson.fromJson(json2.get("paths"), JsonArray.class);
          //  System.out.println(json3);
            JsonObject json4= json3.get(0).getAsJsonObject();

            Integer distance = Integer.parseInt(json4.get("distance").getAsString());
            d = Double.valueOf(df.format((float)distance/1000));
        }else{
            d= 0;
        }
    return d;
    }


    private static String KEY = "912130939fdb92d90ce51f75d03bd668";

    private static Pattern pattern = Pattern.compile("\"location\":\"(\\d+\\.\\d+),(\\d+\\.\\d+)\"");


    public static String sendGet(String url, String param) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url + "?" + param.replaceAll("\r|\n", "");
            logger.info("get 请求地址 ："+urlNameString);
            URL realUrl = new URL(urlNameString);
            // 打开和URL之间的连接
            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();
            // 遍历所有的响应头字段
            for (String key : map.keySet()) {
//                System.out.println(key + "--->" + map.get(key));
            }
            // 定义 BufferedReader输入流来读取URL的响应
           /* in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));*/
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream(),"UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
           logger.error("发送GET请求出现异常！" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }



    public static void main(String[] args){

       String sendGet = sendGet("http://api.isolarcloud.com/sungws/AppService","service=login&login_type=1&sys_code=901&user_account=15333966762&user_password=pw1111&appkey=047AA5BB868E2D0CBA10A4D638586EEF");
        Gson gson1 = new Gson();

        JsonObject json1 = gson1.fromJson(sendGet, JsonObject.class);
        //System.out.println(json1);
        String state = json1.get("result_code").getAsString();


        if("1".equals(state)){

            JsonObject json2 = gson1.fromJson(json1.get("result_data"), JsonObject.class);
            //System.out.println("json2 : "+json2);
            String userId = json2.get("user_id").getAsString();
            String token = json2.get("token").getAsString();
            //System.out.println("userId : "+userId + ", token : "+token);
            String sendGet1 = sendGet("http://api.isolarcloud.com/sungws/AppService","service=getFaultDetail&fault_code=2740.201907.339df55822195&token="+token+"&appkey=047AA5BB868E2D0CBA10A4D638586EEF");

            JsonObject json = gson1.fromJson(sendGet1, JsonObject.class);
            JsonObject json3 = gson1.fromJson(json.get("result_data"), JsonObject.class);
            JsonArray array = json3.get("repair_advise").getAsJsonArray();
            for (JsonElement j : array) {
                JsonObject jsonObject = j.getAsJsonObject();
                System.out.println(jsonObject.get("step_content").getAsString());

            }

  /*          //登录成功
            String senddev = sendGet("http://api.isolarcloud.com/sungws/AppService","service=getPsList&user_id="+userId+"&curPage=1&size=100&token="+token+"&appkey=047AA5BB868E2D0CBA10A4D638586EEF");
       System.out.println("senddev: "+senddev);
            JsonObject json4 = gson1.fromJson(senddev, JsonObject.class);

            JsonObject devstate3 = gson1.fromJson(json4.get("result_data"), JsonObject.class);
            JsonArray array = gson1.fromJson(devstate3.get("pageList"), JsonArray.class);
            JsonObject jsone= array.get(0).getAsJsonObject();
            Map<String,String> maps = new Gson().fromJson(jsone.get("total_energy"), Map.class);
            Map<String,String> map1 = new Gson().fromJson(jsone.get("today_energy"), Map.class);
            Map<String,String> map2 = new Gson().fromJson(jsone.get("curr_power"), Map.class);
            Map<String,String> map3 = new Gson().fromJson(jsone.get("today_income"), Map.class);
*/

        }
    }

}
