package com.ruoyi.web.controller.map.util;

import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

public class GeoJsonUtil {

    /**
     * 转换投影坐标为经纬度
     * @return
     */
    public static void convertCoordinate(JSONArray featuresArray) {
        for (int i = 0; i < featuresArray.size(); i++) {
            JSONObject properties = featuresArray.getJSONObject(i).getJSONObject("properties");
            String layer = properties.getString("Layer");
            properties.put("color", getColor(layer));
            JSONObject geometry = featuresArray.getJSONObject(i).getJSONObject("geometry");
            String geometryType = geometry.getString("type");
            CoordinateUtil convert = new CoordinateUtil(0, 94.5, 0, 499980, 1345);
            JSONArray latlngList = new JSONArray();
            if (!geometryType.equals("GeometryCollection")) {
                if (geometryType.equals("Point")) {
                    featuresArray.getJSONObject(i).getJSONObject("geometry").put("coordinates", convertPoint(geometry, convert));
                }
                else {
                    if (geometryType.equals("MultiPoint") || geometryType.equals("LineString")) {
                        convertMultiPointOrLineString(geometry, convert, latlngList);
                    }
                    if (geometryType.equals("MultiLineString") || geometryType.equals("Polygon")) {
                        convertMultiLineStringOrPolygon(geometry, convert, latlngList);
                    }
                    if (geometryType.equals("MultiPolygon")) {
                        convertMultiPolygon(geometry, convert, latlngList);
                    }
                    featuresArray.getJSONObject(i).getJSONObject("geometry").put("coordinates", latlngList);
                }
            }
            else {
                JSONArray geometries = geometry.getJSONArray("geometries");
                JSONArray geometryArray = new JSONArray();
                for (Object geom : geometries){
                    JSONObject geomObject = (JSONObject) geom;
                    String geomObjectType = geomObject.getString("type");
                    JSONArray latlngArray = new JSONArray();
                    if (geomObjectType.equals("Point")) {
                        geomObject.put("coordinates", convertPoint(geomObject, convert));
                    }
                    else {
                        if (geomObjectType.equals("MultiPoint") || geomObjectType.equals("LineString")) {
                            convertMultiPointOrLineString(geomObject, convert, latlngArray);
                        }
                        if (geomObjectType.equals("MultiLineString") || geomObjectType.equals("Polygon")) {
                            convertMultiLineStringOrPolygon(geomObject, convert, latlngArray);
                        }
                        if (geomObjectType.equals("MultiPolygon")) {
                            convertMultiPolygon(geomObject, convert, latlngArray);
                        }
                        geomObject.put("coordinates", latlngArray);
                    }
                    geometryArray.add(geomObject);
                }
                featuresArray.getJSONObject(i).getJSONObject("geometry").put("geometries", geometryArray);
            }
        }
    }

    private static String getColor(String layer) {
        String color = "#FF0000";
        if (NumberUtil.isNumber(layer)) {
            int layerNum = Integer.parseInt(layer);
            if (layerNum < 4000)
                color = "#FF0000"; //1
            else if (layerNum < 5000)
                color = "#00FFFF"; //4
            else if (layerNum < 5300)
                color = "#FF0000"; //1
            else if (layerNum < 5500)
                color = "#0000FF"; //5
            else if (layerNum < 6000)
                color = "#FF0000"; //1
            else if (layerNum < 6400)
                color = "#0000FF"; //5
            else if (layerNum < 6740)
                color = "#FF0000"; //1
            else if (layerNum < 6742)
                color = "#FFFFFF"; //7
            else if (layerNum < 6743)
                color = "#FFFF00"; //2
            else if (layerNum < 6752)
                color = "#FFFFFF"; //7
            else if (layerNum < 6753)
                color = "#FFFF00"; //2
            else if (layerNum < 8000)
                color = "#FF0000"; //1
            else if (layerNum < 8111)
                color = "#FFFFFF"; //7
            else if (layerNum < 8121)
                color = "#FFFF00"; //2
            else if (layerNum < 8122)
                color = "#FF0000"; //1
            else if (layerNum < 8131)
                color = "#FFFFFF"; //7
            else if (layerNum < 8300)
                color = "#FFFF00"; //2
            else if (layerNum < 8700)
                color = "#FF0000"; //1
            else if (layerNum < 9612)
                color = "#00FF00"; //3
            else if (layerNum < 9613)
                color = "#00FFFF"; //4
            else if (layerNum < 9614)
                color = "#00FF00"; //3
            else if (layerNum < 9801)
                color = "#FF0000"; //1
            else if (layerNum < 10000)
                color = "#00FF00"; //3
        }
        return color;
    }

    private static double[] convertPoint(JSONObject geometry, CoordinateUtil convert) {
        JSONArray coordinates = geometry.getJSONArray("coordinates");
        double[] latlng = new double[3];
        String lat = coordinates.get(0).toString();
        String lng = coordinates.get(1).toString();
        String alt = coordinates.size() > 2 ? coordinates.get(2).toString() : "0";
        latlng[0] = convert.GKgetJW(Double.parseDouble(lat), Double.parseDouble(lng))[0];
        latlng[1] = convert.GKgetJW(Double.parseDouble(lat), Double.parseDouble(lng))[1];
        latlng[2] = Double.parseDouble(alt);
        return latlng;
    }

    private static void convertMultiPointOrLineString(JSONObject geometry, CoordinateUtil convert, JSONArray latlngList) {
        JSONArray coordinates = geometry.getJSONArray("coordinates");
        for (Object coordinate : coordinates) {
            double[] latlng = new double[3];
            String lat = coordinate.toString().split(",")[0].replace("[", "");
            String lng = coordinate.toString().split(",")[1].replace("]", "");
            String alt = coordinate.toString().split(",").length > 2 ? coordinate.toString().split(",")[2].replace("]", "") : "0";
            latlng[0] = convert.GKgetJW(Double.parseDouble(lat), Double.parseDouble(lng))[0];
            latlng[1] = convert.GKgetJW(Double.parseDouble(lat), Double.parseDouble(lng))[1];
            latlng[2] = Double.parseDouble(alt);
            latlngList.add(latlng);
        }
    }

    private static void convertMultiLineStringOrPolygon(JSONObject geometry, CoordinateUtil convert, JSONArray latlngList) {
        JSONArray coordinatesList = geometry.getJSONArray("coordinates");
        for (Object coordinates : coordinatesList) {
            List<JSONArray> coordinateList = (List<JSONArray>) coordinates;
            JSONArray jwList = new JSONArray();
            for (JSONArray coordinate : coordinateList) {
                double[] latlng = new double[3];
                String lat = coordinate.toString().split(",")[0].replace("[", "");
                String lng = coordinate.toString().split(",")[1].replace("]", "");
                String alt = coordinate.toString().split(",").length > 2 ? coordinate.toString().split(",")[2].replace("]", "") : "0";
                latlng[0] = convert.GKgetJW(Double.parseDouble(lat), Double.parseDouble(lng))[0];
                latlng[1] = convert.GKgetJW(Double.parseDouble(lat), Double.parseDouble(lng))[1];
                latlng[2] = Double.parseDouble(alt);
                jwList.add(latlng);
            }
            latlngList.add(jwList);
        }
    }
    private static void convertMultiPolygon(JSONObject geometry, CoordinateUtil convert, JSONArray latlngList) {
        JSONArray coordinatesList = geometry.getJSONArray("coordinates");
        for (Object coordinates : coordinatesList) {
            List<JSONArray> coordinateList = (List<JSONArray>) coordinates;
            JSONArray jwList = new JSONArray();
            for (JSONArray coordinate : coordinateList) {
                JSONArray jwArray = new JSONArray();
                for (Object coor : coordinate) {
                    double[] latlng = new double[3];
                    String lat = coor.toString().split(",")[0].replace("[", "");
                    String lng = coor.toString().split(",")[1].replace("]", "");
                    String alt = coor.toString().split(",").length > 2 ? coor.toString().split(",")[2].replace("]", "") : "0";
                    latlng[0] = convert.GKgetJW(Double.parseDouble(lat), Double.parseDouble(lng))[0];
                    latlng[1] = convert.GKgetJW(Double.parseDouble(lat), Double.parseDouble(lng))[1];
                    latlng[2] = Double.parseDouble(alt);
                    jwArray.add(latlng);
                }
                jwList.add(jwArray);
            }
            latlngList.add(jwList);
        }
    }

}
