package com.skyversation.poiaddr.addquery;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.skyversation.poiaddr.bean.AddressResult;
import com.skyversation.poiaddr.util.AddressTools;
import com.skyversation.poiaddr.util.CoordTransform2;
import com.skyversation.poiaddr.util.status.AddressResultEnum;

import java.util.ArrayList;
import java.util.List;

public class TransfromDataTool {

    public static AddressResult wdResultToResult(JSONObject json) {
        AddressResult result = new AddressResult();
        JSONArray array = json.getJSONObject("data").getJSONArray("addrList");
        if (array == null || array.size() < 1) {
            result.setCode(AddressResultEnum.RESULT_NULL);
            return result;
        }
        for (int i = 0; i < array.size(); i++) {
            JSONObject jsonObject = array.getJSONObject(i);
            AddressResult.ContentBean content = new AddressResult.ContentBean();
            content.setPname(jsonObject.getString("city"));
            content.setCityname(jsonObject.getString("city"));
            content.setAdname(jsonObject.getString("town"));
            content.setCommunity(jsonObject.getString("community"));
            content.setCommunityCode(jsonObject.getString("communitycode"));
            content.setType(jsonObject.getString("datasource"));
            content.setAddress(jsonObject.getString("addr"));
            content.setDistance(jsonObject.getString("lv"));
            content.setName(jsonObject.getString("addr"));
            double[] points = CoordTransform2.getInstance().shcj_to_wgs84(
                    Double.parseDouble(jsonObject.getString("x")), Double.parseDouble(jsonObject.getString("y")));
            content.setLocation(points[0] + "," + points[1]);
            content.setLon(points[0]);
            content.setLat(points[1]);
            if (result.getData() == null) {
                result.setData(new ArrayList<>());
            }
            result.getData().add(content);
        }
        result.setCode(AddressResultEnum.WDJA_SUCCESS);
        return result;
    }

    public static AddressResult szxResultToResult2(JSONArray array) {
        AddressResult result = new AddressResult();
        if (array == null || array.size() < 1) {
            result.setCode(AddressResultEnum.RESULT_NULL);
            return result;
        }
        for (int i = 0; i < array.size(); i++) {
            JSONObject jsonObject = array.getJSONObject(i);
            AddressResult.ContentBean content = new AddressResult.ContentBean();
            content.setPname(jsonObject.getString("province"));
            content.setCityname(jsonObject.getString("district"));
            content.setAdname(jsonObject.getString("town"));
            content.setType(jsonObject.getString("type"));
            content.setAddress(jsonObject.getString("address"));
            content.setName(jsonObject.getString("address"));
            if (jsonObject.containsKey("location") && jsonObject.getJSONObject("location").containsKey("lat") && jsonObject.getJSONObject("location").containsKey("lng")) {
                double[] points = CoordTransform2.getInstance().shcj_to_wgs84(
                        Double.parseDouble(jsonObject.getJSONObject("location").getString("lat")), Double.parseDouble(jsonObject.getJSONObject("location").getString("lng")));
                content.setLocation(points[0] + "," + points[1]);
                content.setLat(points[0]);
                content.setLon(points[1]);
            }
            if (result.getData() == null) {
                result.setData(new ArrayList<>());
            }
            result.getData().add(content);
        }
        result.setCode(AddressResultEnum.WDJA_SUCCESS);
        return result;
    }

    public static AddressResult szxResultToResult2(JSONObject jsonObject) {
        AddressResult result = new AddressResult();
        if (jsonObject == null || jsonObject.size() < 1) {
            result.setCode(AddressResultEnum.RESULT_NULL);
            return result;
        }
        AddressResult.ContentBean content = new AddressResult.ContentBean();
        if (jsonObject.containsKey("总分") && jsonObject.get("总分") != null) {
            content.setPname(jsonObject.getString("province"));
            content.setCityname(jsonObject.getString("district"));
            content.setAdname(jsonObject.getString("town"));
            content.setType(jsonObject.getString("type"));
            content.setAddress(jsonObject.getString("address"));
            content.setName(jsonObject.getString("address"));
            content.setSearchAddress(jsonObject.getString("searchAddress"));
            content.setScore(jsonObject.getString("总分"));
            if (jsonObject.containsKey("location") && jsonObject.getJSONObject("location").containsKey("lat") && jsonObject.getJSONObject("location").containsKey("lng")) {
                double[] points = CoordTransform2.getInstance().shcj_to_wgs84(
                        Double.parseDouble(jsonObject.getJSONObject("location").getString("lat")), Double.parseDouble(jsonObject.getJSONObject("location").getString("lng")));
                content.setLocation(points[0] + "," + points[1]);
                content.setLat(points[0]);
                content.setLon(points[1]);
            }
            if (result.getData() == null) {
                result.setData(new ArrayList<>());
            }
            result.getData().add(content);
            result.setCode(AddressResultEnum.WDJA_SUCCESS);
        }
        return result;
    }

    public static AddressResult szxResultToResult(JSONObject json, String searchAddress) {
        AddressResult result = new AddressResult();
        JSONArray array = json.getJSONArray("result");
        if (array == null || array.size() < 1) {
            result.setCode(AddressResultEnum.RESULT_NULL);
            return result;
        }
        JSONObject jsonObject = com.skyversation.poiaddr.util.AddressTools.getInstance().findBestMatch(searchAddress, array, "address");
        if (jsonObject.containsKey("总分") && jsonObject.get("总分") != null && !jsonObject.getString("总分").isEmpty()) {
            AddressResult.ContentBean content = new AddressResult.ContentBean();
            content.setScore(jsonObject.getString("总分"));
            content.setSearchAddress(jsonObject.getString("searchAddress"));
            content.setType(jsonObject.getString("type_name"));
            List<String> addressList = new ArrayList<>();
            content.setAddress(jsonObject.getString("address"));
            if (!jsonObject.getString("address").isEmpty()) {
                addressList.add(jsonObject.getString("address"));
            }
            if (!jsonObject.getString("std_address").isEmpty()) {
                addressList.add(jsonObject.getString("std_address"));
            }
            content.setName(jsonObject.getString("name"));
            double[] points = new double[]{jsonObject.getJSONObject("location").getDouble("lng"),
                    jsonObject.getJSONObject("location").getDouble("lat")};
            content.setLocation(points[0] + "," + points[1]);
            content.setLon(points[1]);
            content.setLat(points[0]);
            if (jsonObject.containsKey("ext_data")) {
                JSONObject extData = jsonObject.getJSONObject("ext_data");
                if (extData.containsKey("address") && !extData.getString("address").isEmpty()) {
                    addressList.add(extData.getString("address"));
                }
                if (extData.containsKey("source_address") && !extData.getString("source_address").isEmpty()) {
                    addressList.add(extData.getString("source_address"));
                }

                if (extData.containsKey("region_jw") && !extData.getString("region_jw").isEmpty()) {
                    JSONObject cjJson = new JSONObject();
                    cjJson.put("所属街道", extData.getString("region_jd"));
                    cjJson.put("所属居委", extData.getString("region_jw"));
                    content.setCjJson(cjJson);
                }
            }
            for (String item : addressList) {
                if (item.contains("上海市") && item.contains(Constant.getArea() + "区") && item.length() > content.getAddress().length()) {
                    content.setAddress(item);
                }
            }
            if (result.getData() == null) {
                result.setData(new ArrayList<>());
            }
            result.getData().add(content);
            result.setCode(AddressResultEnum.SZX_SUCCESS);
        }
        return result;
    }

    public static AddressResult gdResultToResult(JSONObject json) {
        AddressResult result = new AddressResult();
        JSONArray array = json.getJSONArray("geocodes");
        if (array == null || array.size() < 1) {
            result.setCode(AddressResultEnum.RESULT_NULL);
            return result;
        }
        for (int i = 0; i < array.size(); i++) {
            JSONObject jsonObject = array.getJSONObject(i);
            AddressResult.ContentBean content = new AddressResult.ContentBean();
            content.setPname(jsonObject.getString("city"));
            content.setCityname(jsonObject.getString("city"));
            content.setAdname(jsonObject.getString("district"));
            content.setType(jsonObject.getString("level"));
            content.setAddress(jsonObject.getString("formatted_address"));
            content.setName(jsonObject.getString("street"));
            String[] loc = jsonObject.getString("location").split(",");
            double[] points = CoordTransform2.getInstance().gcj02_to_wgs84(
                    Double.valueOf(loc[0]), Double.valueOf(loc[1]));
            content.setLocation(points[0] + "," + points[1]);
            content.setLon(points[0]);
            content.setLat(points[1]);
            if (result.getData() == null) {
                result.setData(new ArrayList<>());
            }
            result.getData().add(content);
        }
        result.setCode(AddressResultEnum.GD_SUCCESS);
        return result;
    }

    public static AddressResult gdV3ResultToResult(JSONArray array) {
        AddressResult result = new AddressResult();
        if (array == null || array.size() < 1) {
            result.setCode(AddressResultEnum.RESULT_NULL);
            return result;
        }
        try {
            for (int i = 0; i < array.size(); i++) {
                JSONObject jsonObject = array.getJSONObject(i);
                AddressResult.ContentBean content = new AddressResult.ContentBean();
                content.setPname(jsonObject.getString("pname"));
                content.setCityname(jsonObject.getString("cityname"));
                content.setAdname(jsonObject.getString("adname"));
                content.setType(jsonObject.getString("type"));
                content.setAddress(content.getCityname() + content.getAdname() + jsonObject.getString("address"));
                content.setName(jsonObject.getString("name"));
                String[] loc = jsonObject.getString("location").split(",");
                double[] points = CoordTransform2.getInstance().gcj02_to_wgs84(
                        Double.parseDouble(loc[0]), Double.parseDouble(loc[1]));
                content.setLocation(points[0] + "," + points[1]);
                content.setLon(points[0]);
                content.setLat(points[1]);
                if (result.getData() == null) {
                    result.setData(new ArrayList<>());
                }
                result.getData().add(content);
            }
        } catch (Exception e) {
            System.err.println("gdV3ResultToResult err:" + e);
        }
        result.setCode(AddressResultEnum.WDJA_SUCCESS);

        result.setCode(AddressResultEnum.GDV3_SUCCESS);
        return result;
    }

}
