package net.northking.cctp.se.util;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

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


public class JsonUtils {
    private final static Logger logger = LoggerFactory.getLogger(JsonUtils.class);
    private static ObjectMapper objectMapper = new ObjectMapper();
    private static final MapTypeReference MAP_TYPE = new MapTypeReference();
    private static final ListTypeReference LIST_TYPE = new ListTypeReference();
    static {
        objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT);
        objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
    }

    public static <T> String toJson(T obj){
        String ret = null;
        if (null != obj){
            try{
               ret = objectMapper.writeValueAsString(obj);
            }catch (Exception e){
                logger.error("Error when parse obj to Json",e);
            }
        }
        return ret;
    }
    public static <T> Map<String,T> readMap(String jsonString,Class<T> valTypeClazz){
        Map<String,T> obj  = null;
        if (!StringUtils.isEmpty(jsonString)){
            try{
                obj = (Map<String,T>)objectMapper.readValue(jsonString,MAP_TYPE);
            }catch (Exception e){
                logger.error("Error when parse map from jsonString",e);
            }
        }
        return obj;
    }
    public static <T> List<T> readList(String jsonString,Class<T> clazz){
        List<T> obj  = null;
        if (!StringUtils.isEmpty(jsonString)){
            try{
                obj = (List<T>) objectMapper.readValue(jsonString,LIST_TYPE);
            }catch (Exception e){
                logger.error("Error when parse list from jsonString",e);
            }
        }
        return obj;
    }
    public static <T> T fromJsonString(String jsonString, TypeReference<T> valTypeRef){
        T obj  = null;
        if (!StringUtils.isEmpty(jsonString) && null != valTypeRef){
            try{
                obj = objectMapper.readValue(jsonString,valTypeRef);

            }catch (Exception e){
                logger.error("Error when parse obj from jsonString",e);
            }
        }
        return obj;
    }
    public static <T> T fromJsonString(String jsonString,Class<T> clazz){
        T obj  = null;
        if (!StringUtils.isEmpty(jsonString) && null !=clazz ){
            try{
                obj =  objectMapper.readValue(jsonString,clazz);
            }catch (Exception e){
                logger.error("Error when parse obj from jsonString",e);
            }
        }
        return obj;
    }
    private static class ListTypeReference extends TypeReference<List<Object>> {
        private ListTypeReference() {
        }
    }

    private static class MapTypeReference extends TypeReference<Map<String, Object>> {
        private MapTypeReference() {
        }
    }
}
