package org.nl.acs.ext.wms.data;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.type.TypeFactory;

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

public class JsonUtl {
    private static ObjectMapper objectMapper = null;
    private static ObjectMapper objectMapperLog = null;

    private JsonUtl() {
    }

    private static ObjectMapper init() {
        ObjectMapper objectMapper = new ObjectMapper();
        SimpleModule simpleModule = new SimpleModule();
//        simpleModule.addSerializer(Enum.class, new EnumSerializer());
//        simpleModule.addSerializer(Date.class, new DateSerializer());
//        simpleModule.addDeserializer(Enum.class, new EnumDeserializer());
//        simpleModule.addDeserializer(Date.class, new DateDeserializers.DateDeserializer());
        objectMapper.registerModule(simpleModule);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        return objectMapper;
    }

    public static ObjectMapper getInstance() {
        if (objectMapper == null) {
            Class var0 = JsonUtl.class;
            synchronized(JsonUtl.class) {
                if (objectMapper == null) {
                    objectMapper = init();
                }
            }
        }

        return objectMapper;
    }

    public static ObjectMapper getInstanceLog() {
        if (objectMapperLog == null) {
            Class var0 = JsonUtl.class;
            synchronized(JsonUtl.class) {
                if (objectMapperLog == null) {
                    objectMapperLog = init();
                    objectMapperLog.setSerializationInclusion(Include.NON_NULL);
                }
            }
        }

        return objectMapperLog;
    }

    public static ObjectMapper getObjectMapper() {
        return getInstance();
    }

    public static String parse(Object object) throws RuntimeException {
        try {
            return getObjectMapper().writeValueAsString(object);
        } catch (JsonProcessingException var2) {
            throw new RuntimeException(var2);
        }
    }

    public static String parseWithoutException(Object object) {
        try {
            return parse(object);
        } catch (Exception var2) {
            return null;
        }
    }

    public static String parseLog(Object object) {
        try {
            return getInstanceLog().writeValueAsString(object);
        } catch (Exception var2) {
            return null;
        }
    }

    public static <T> T format(String json, Class<T> clazz) throws RuntimeException {
        try {
            return getObjectMapper().readValue(json, clazz);
        } catch (IOException var3) {
            throw new RuntimeException(var3);
        }
    }

    public static <T> List<T> formatList(String json, Class<T> clazz) throws RuntimeException {
        try {
            JavaType type = getObjectMapper().getTypeFactory().constructParametricType(List.class, new Class[]{clazz});
            return (List)getObjectMapper().readValue(json, type);
        } catch (IOException var3) {
            throw new RuntimeException(var3);
        }
    }

    public static <T, U> Map<T, U> formatMap(String json, Class<T> clazzKey, Class<U> clazzValue) throws RuntimeException {
        try {
            JavaType type = getObjectMapper().getTypeFactory().constructParametricType(Map.class, new Class[]{clazzKey, clazzValue});
            return (Map)getObjectMapper().readValue(json, type);
        } catch (IOException var4) {
            throw new RuntimeException(var4);
        }
    }

    public static <T> List<List<T>> formatListTwo(String json, Class<T> clazz) throws RuntimeException {
        try {
            TypeFactory typeFactory = getObjectMapper().getTypeFactory();
            JavaType type = typeFactory.constructParametrizedType(List.class, List.class, new Class[]{clazz});
            type = typeFactory.constructParametrizedType(List.class, List.class, new JavaType[]{type});
            return (List)getObjectMapper().readValue(json, type);
        } catch (IOException var4) {
            throw new RuntimeException(var4);
        }
    }
}
