package jj.tech.paolu.utils;


import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;

import jj.tech.paolu.config.security.user.WebAdminUtil;

/**
 * json转换工具类
 */
public class JsonUtil {
	private static Logger logger = LoggerFactory.getLogger(WebAdminUtil.class);

    private static ObjectMapper objectMapper = new ObjectMapper();
    static {
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNRESOLVED_OBJECT_IDS, false);
        objectMapper.registerModule(javaTimeModule);

    }

    //将单个对象转换成json格式的字符串（没有格式化后的json）
    public static <T> String writeValueAsString(T obj){
        if (obj == null){
            return null;
        }
        try {
            return obj instanceof String ? (String) obj:objectMapper.writeValueAsString(obj);
        } catch (IOException e) {
        	logger.warn("Parse object to String error", e);
            return null;
        }
    }

    //将单个对象转换成json格式的字符串（格式化后的json）
    public static <T> String writeValueAsStringPretty(T obj){
        if (obj == null){
            return null;
        }
        try {
            return obj instanceof String ? (String) obj:objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
        } catch (IOException e) {
        	logger.warn("Parse object to String error", e);
            return null;
        }
    }
    //将json形式的字符串数据转换成单个对象
    public static <T> T readValueToObj(String str, Class<T> clazz){
        if (StringUtils.isEmpty(str) || clazz == null){
            return null;
        }
        try {
            return clazz.equals(String.class) ? (T) str : objectMapper.readValue(str,clazz);
        } catch (IOException e) {
        	logger.warn("Parse object to Object error", e);
            return null;
        }
    }

    //将json形式的字符串数据转换成多个对象
//    public static <T> T string2Obj(String str, TypeReference<T> typeReference){
//        if (StringUtils.isEmpty(str) || typeReference == null){
//            return null;
//        }
//        try {
//            return typeReference.getType().equals(String.class) ? (T) str : (T) objectMapper.readValue(str, typeReference);
//        } catch (IOException e) {
//            log.warn("Parse object to Object error", e);
//            return null;
//        }
//    }

    //将json形式的字符串数据转换成多个对象
    public static <T> T readValueToList(String str, Class<T> collectionClass, Class<?>... elementClasses){
        try {
            return objectMapper.readValue(str, objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses));
        } catch (IOException e) {
        	logger.warn("Parse object to Object error", e);
            return null;
        }
    }


}
