package com.qmth.exam.reserve.util;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qmth.boot.core.exception.StatusException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JsonHelper {

    private static final Logger log = LoggerFactory.getLogger(JsonHelper.class);

    public static String toJson(Object object) {
        if (object == null) {
            return null;
        }

        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
            return mapper.writeValueAsString(object);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new StatusException("转换JSON失败！", e);
        }
    }

    public static <T> T toObj(String jsonStr, Class<T> clazz) {
        if (StringUtils.isEmpty(jsonStr)) {
            return null;
        }

        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
            return mapper.readValue(jsonStr, clazz);
        } catch (Exception e) {
            log.error("JSON:{} ERR:{}", jsonStr, e.getMessage());
            throw new StatusException("JSON解析失败！", e);
        }
    }

    public static <T> List<T> toList(String jsonStr, Class<T> clazz) {
        if (StringUtils.isEmpty(jsonStr)) {
            return null;
        }

        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
            JavaType javaType = mapper.getTypeFactory().constructCollectionType(List.class, clazz);
            return mapper.readValue(jsonStr, javaType);
        } catch (Exception e) {
            log.error("JSON:{} ERR:{}", jsonStr, e.getMessage());
            throw new StatusException("JSON解析失败！", e);
        }
    }

    public static <T> Map<String, T> toMap(String jsonStr, Class<T> clazz) {
        if (StringUtils.isEmpty(jsonStr)) {
            return null;
        }

        try {
            ObjectMapper mapper = new ObjectMapper();
            JavaType javaType = mapper.getTypeFactory().constructMapType(HashMap.class, String.class, clazz);
            return mapper.readValue(jsonStr, javaType);
        } catch (Exception e) {
            log.error("JSON:{} ERR:{}", jsonStr, e.getMessage());
            throw new StatusException("JSON解析失败！", e);
        }
    }

    public static JsonNode getNode(String jsonStr) {
        if (StringUtils.isEmpty(jsonStr)) {
            return null;
        }

        try {
            ObjectMapper mapper = new ObjectMapper();
            return mapper.readTree(jsonStr);
        } catch (Exception e) {
            log.error("JSON:{} ERR:{}", jsonStr, e.getMessage());
            throw new StatusException("JSON解析失败！", e);
        }
    }

}
