package cn.component.context;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import org.springframework.context.annotation.Profile;
import org.springframework.core.io.ClassPathResource;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import cn.util.core.IsKit;

/**读取或更新resources下的json文件
 * 本地读：
 * File new_file = new File(dir);
 * jsonStr = FileUtils.readFileToString(new_file,Charset.forName("GBK"));
 */
public class JsonFile 
{
	//config/json/city.json
	public static String readJSON(String fileName)  
	{
		//InputStream config = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
		ClassPathResource classPathResource = new ClassPathResource(fileName);
		try {
			InputStream config = classPathResource.getInputStream();
			return inputStreamToString(config);
		} catch (IOException e) {
		}
		//String result = sb.toString().replaceAll("\r\n", "").replaceAll(" +", "");
		return null;
	}
	private static  String inputStreamToString(InputStream inputStream) 
	{
		InputStreamReader inputStreamReader = null;
		BufferedReader bufferedReader = null;
		StringBuffer buffer = new StringBuffer();
		try {
			//GBK
			inputStreamReader = new InputStreamReader(inputStream, "utf-8");
			bufferedReader = new BufferedReader(inputStreamReader);
			String str = null;
			while ((str = bufferedReader.readLine()) != null) {
				buffer.append(str);
			}
		} catch (Exception e) {

		} finally {// 释放资源
			try {
				if(IsKit.isNotNull(bufferedReader)) {		
					bufferedReader.close();
				}
				if(IsKit.isNotNull(inputStreamReader)) {		
					inputStreamReader.close();
				}
				if(IsKit.isNotNull(inputStream)) {		
					inputStream.close();
				}
			}catch (Exception e) {
				bufferedReader = null;
				inputStreamReader = null;
				inputStream = null;
			}
		}
		return buffer.toString();
	}

	public static void updateJSON(String fileName, HashMap<String, String> map)
	{
		JSONObject jsonObject = JSON.parseObject(readJSON(fileName));
		JSONArray links = jsonObject.getJSONArray("links");

		for (int i = 0; i < links.size(); i++) {
			JSONObject item1 = links.getJSONObject(i);
			if (item1.getString("name").equalsIgnoreCase(map.get("name"))) {
				item1.put("content", map.get("content"));
			}
		}
		JSONObject nItem = new JSONObject();
		nItem.put("links", links);
		String nContent = JSON.toJSONString(nItem);
		BufferedWriter bw = null;
		try {
			String filePath = Profile.class.getClassLoader().getResource(fileName).toURI().getPath();
			bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath)));
			bw.write("");
			bw.write(nContent);
			bw.close();
		} catch (Exception e) {

		} finally {
			try {
				bw.close();
			} catch (IOException e) {
				bw = null;
			}
		}
	}
}