package com.keystar.plane.inspection.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.util.ResourceUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FileUtils {
    /**
     * 从json文件中读取数据
     */
    public static String getDataFromJsonFile() throws FileNotFoundException {

        StringBuilder jsonString = new StringBuilder();
        File file = ResourceUtils.getFile("classpath:pv.json");
        if (file.exists()) {
            BufferedReader bufferedReader = null;
            try {
//                bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
//                char[] chars = new char[100000];
//                int length;
//                while ((length = bufferedReader.read(chars)) != -1) {
//                    String temp = new String(chars, 0, length);
//                    jsonString.append(temp);
//                }
                bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));
                char[] chars =  new char[1000];
                while (bufferedReader.read(chars, 0, chars.length) > 0){
                    String temp = new String(chars, 0, chars.length);
                    jsonString.append(temp);
                }
            } catch (IOException e) {
                System.out.println("=====获取数据异常=====" + e);
            } finally {
                if (null != bufferedReader) {
                    try {
                        bufferedReader.close();
                    } catch (IOException ex) {
                        System.out.println("=======获取数据时，关闭流异常=======" + ex);
                    }
                }
            }
        }
        return jsonString.toString();
    }



    /**
     *  读取json文件
     **/
    public static String getPVJson() throws IOException {
        File file = ResourceUtils.getFile("classpath:pv.json");
        try(FileInputStream fileInputStream = new FileInputStream(file)) {
            int size = fileInputStream.available();
            StringBuilder jsonString = new StringBuilder();
            for (int i = 0; i <= size; i+=1000) {
                byte[] buffer = new byte[1000];
                fileInputStream.read(buffer);
                String str = new String(buffer, StandardCharsets.UTF_8);
                jsonString.append(str);
            }
            return jsonString.toString();
        }
    }

    public static Map<String, List> jsonTransData (String payload){
        JSONObject jsonObject = JSON.parseObject(payload);
        List<JSONObject> featureList = (List<JSONObject>)jsonObject.get("features");
        Map<String,List> result = new HashMap<>();
        for (int i = 0; i<featureList.size(); i++){
            JSONObject object1 = featureList.get(i);
            List<List> coordinatesAll = (List<List>) object1.get("coordinates");
            List<List> coordinates = coordinatesAll.get(0);
            JSONObject properties = (JSONObject) object1.get("properties");
            result.put((String) properties.get("名称"),coordinates);
        }
        return result;
    }


}
