package com.qmth.boot.tools.excel.convertor;

import com.qmth.boot.tools.excel.convertor.impl.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ValueConvertors {

    private static final Map<Class<?>, ValueConvertor<?>> defaultMap = new HashMap<>();

    private static final Map<Class<?>, ValueConvertor<?>> extendMap = new ConcurrentHashMap<>();

    static {
        defaultMap.put(boolean.class, BooleanConvertor.instance);
        defaultMap.put(Boolean.class, BooleanConvertor.instance);
        defaultMap.put(int.class, IntegerConvertor.instance);
        defaultMap.put(Integer.class, IntegerConvertor.instance);
        defaultMap.put(long.class, LongConvertor.instance);
        defaultMap.put(Long.class, LongConvertor.instance);
        defaultMap.put(float.class, FloatConvertor.instance);
        defaultMap.put(Float.class, FloatConvertor.instance);
        defaultMap.put(double.class, DoubleConvertor.instance);
        defaultMap.put(Double.class, DoubleConvertor.instance);
        defaultMap.put(BigDecimal.class, BigDecimalConvertor.instance);
        defaultMap.put(String.class, StringConvertor.instance);
        defaultMap.put(Date.class, DateConvertor.instance);
        defaultMap.put(LocalDateTime.class, LocalDateTimeConvertor.instance);
    }

    public static void register(Class<?> type, ValueConvertor<?> parser) {
        extendMap.computeIfAbsent(type, key -> parser);
    }

    public static boolean containsType(Class<?> type) {
        return defaultMap.containsKey(type) || extendMap.containsKey(type);
    }

    public static ValueConvertor<?> getConvertor(Class<?> type) {
        ValueConvertor<?> convertor = defaultMap.get(type);
        if (convertor == null) {
            convertor = extendMap.get(type);
        }
        return convertor;
    }

}
