package sgc.portal.base.util;

import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * The type Json util.
 *
 * @Class KorName : Json Util
 * @Author : SWA
 */
@Slf4j
public class JsonUtil {


    /**
     * Gets json from map.
     *
     * @param map the map
     * @return json from map
     * @Method Name : getJsonFromMap
     * @Method description : Map을 json으로 변환한다.
     * @Author : user
     */
    @SuppressWarnings("unchecked")
    public static JSONObject getJsonFromMap(Map<String, Object> map) {

        JSONObject json = new JSONObject();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            json.put(key, value);
        }

        return json;
    }


    /**
     * Gets json string from map.
     *
     * @param map the map
     * @return json string from map
     * @Method Name : getJsonStringFromMap
     * @Method description : Map을 jsonString으로 변환한다.
     * @Author : user
     */
    @SuppressWarnings("unchecked")
    public static String getJsonStringFromMap(Map<String, Object> map) {

        JSONObject json = new JSONObject();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            json.put(key, value);
        }

        return json.toJSONString();
    }


    /**
     * Gets json array from list.
     *
     * @param list the list
     * @return json array from list
     * @Method Name : getJsonArrayFromList
     * @Method description : List<Map>을 json으로 변환한다.
     * @Author : user
     */
    @SuppressWarnings("unchecked")
    public static JSONArray getJsonArrayFromList(List<Map<String, Object>> list) {

        JSONArray jsonArray = new JSONArray();
        for (Map<String, Object> map : list) {
            jsonArray.add(getJsonFromMap(map));
        }

        return jsonArray;
    }


    /**
     * Gets json string from list.
     *
     * @param list the list
     * @return json string from list
     * @Method Name : getJsonStringFromList
     * @Method description : List<Map>을 jsonString으로 변환한다.
     * @Author : user
     */
    @SuppressWarnings("unchecked")
    public static String getJsonStringFromList(List<Map<String, Object>> list) {

        JSONArray jsonArray = new JSONArray();
        for (Map<String, Object> map : list) {
            jsonArray.add(getJsonFromMap(map));
        }

        return jsonArray.toJSONString();
    }


    /**
     * Gets map from json object.
     *
     * @param jsonObj the json obj
     * @return map from json object
     * @Method Name : getMapFromJsonObject
     * @Method description : JsonObject를 Map<String, String>으로 변환한다.
     * @Author : user
     */
    @SuppressWarnings("unchecked")
    private static Map<String, Object> getMapFromJsonObject(JSONObject jsonObj) {

        Map<String, Object> map = null;

        try {

            map = new ObjectMapper().readValue(jsonObj.toJSONString(), Map.class);

        } catch (JsonMappingException e) {
            log.error(e.getMessage());
        } catch (IOException e) {
            log.error(e.getMessage());
        }

        return map;
    }


    /**
     * Gets list map from json array.
     *
     * @param jsonArray the json array
     * @return list map from json array
     * @Method Name : getListMapFromJsonArray
     * @Method description : JsonArray를 List<Map<String, String>>으로 변환한다.
     * @Author : user
     */
    public static List<Map<String, Object>> getListMapFromJsonArray(JSONArray jsonArray) {

        List<Map<String, Object>> list = new ArrayList<>();

        if (jsonArray != null) {
            int jsonSize = jsonArray.size();
            for (Object o : jsonArray) {
                Map<String, Object> map = JsonUtil.getMapFromJsonObject((JSONObject) o);
                list.add(map);
            }
        }

        return list;
    }

}
