package org.jeecg.ctop.material.constants;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

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

/**
 * Created by JQ.bi on 2020.06.03
 */
public class JsonResourceUtil {

    private static Logger logger = LoggerFactory.getLogger(JsonResourceUtil.class);

    private JsonResourceUtil() {

    }

    //filename 为文件名字 如 “/json/app_version_info.json”
    public static JSONObject getJsonObjFromResource(String filename) {
        JSONObject json = null;
        if (!filename.contains(".json")) {
            filename += ".json";
        }
        try {
            ClassPathResource classPathResource = new ClassPathResource(filename);
            if (classPathResource.exists()) {
                InputStream inputStream = classPathResource.getInputStream();
                String content = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
                json = JSON.parseObject(content);
            } else {
                logger.info("file not exist!");
            }

        } catch (Exception e) {
            e.printStackTrace();
            logger.info("readFileToString" + e.getMessage());
        }
        return json;
    }

    //json文件转化为JSONArray
    public static JSONArray getJsonArrFromResource(String filename) {
        JSONArray json = null;
        if (!filename.contains(".json")) {
            filename += ".json";
        }
        try {
            ClassPathResource classPathResource = new ClassPathResource(filename);
            if (classPathResource.exists()) {
                InputStream inputStream = classPathResource.getInputStream();
                String content = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
                json = JSON.parseArray(content);
            } else {
                logger.info("file not exist!");
            }

        } catch (Exception e) {
            e.printStackTrace();
            logger.info("readFileToString" + e.getMessage());
        }
        return json;
    }

    public static Map<String, Map<String, Object>> jsonToMap(Object jsonObj) {
        JSONObject jsonObject = JSONObject.parseObject(jsonObj.toString());
        Map<String, Map<String, Object>> mapMap = new HashMap<>();
        jsonObject.forEach((key, value) -> mapMap.put(key, JSONObject.parseObject(value.toString())));
        return mapMap;
    }

    /**
     * 拼接字段
     */
    public static String joinFiled(Map<String, Map<String,Object>> mapMap,JSONArray array) {
        StringBuilder sb = new StringBuilder();
        array.forEach(it -> {
            String filedName = it.toString();
            if (null== mapMap.get(filedName)||mapMap.get(filedName).isEmpty()) {
                sb.append(it).append(",");
            } else {
                sb.append(mapMap.get(filedName).get("filed")).append(",");
            }
        });
        return sb.deleteCharAt(sb.length() - 1).toString();
    }

    /**
     * 拼接title
     */
    public static List<String> joinTitle(Map<String, Map<String,Object>> mapMap,JSONArray array) {
        List<String> titles = new ArrayList<>(20);
        array.forEach(it -> {
            String titleName = it.toString();
            if (null== mapMap.get(titleName)||mapMap.get(titleName).isEmpty()) {
                titles.add(it.toString());
            } else {
                titles.add(mapMap.get(it.toString()).get("comment").toString());
            }
        });
        return titles;
    }

    /**
     * 拼接字段
     */
    public static String joinAllFiled(Map<String, Map<String,Object>> mapMap) {
        StringBuilder sb = new StringBuilder();
        mapMap.forEach((k, v) -> sb.append(v.get("filed")).append(","));
        return sb.deleteCharAt(sb.length() - 1).toString();
    }
}
