package cn.report.excel.easyexcel.api.converters;

import java.util.Map;

import cn.report.excel.easyexcel.api.converters.ConverterKeyBuild.ConverterKey;
import cn.report.excel.easyexcel.api.converters.bigdecimal.BigDecimalBooleanConverter;
import cn.report.excel.easyexcel.api.converters.bigdecimal.BigDecimalNumberConverter;
import cn.report.excel.easyexcel.api.converters.bigdecimal.BigDecimalStringConverter;
import cn.report.excel.easyexcel.api.converters.biginteger.BigIntegerBooleanConverter;
import cn.report.excel.easyexcel.api.converters.biginteger.BigIntegerNumberConverter;
import cn.report.excel.easyexcel.api.converters.biginteger.BigIntegerStringConverter;
import cn.report.excel.easyexcel.api.converters.booleanconverter.BooleanBooleanConverter;
import cn.report.excel.easyexcel.api.converters.booleanconverter.BooleanNumberConverter;
import cn.report.excel.easyexcel.api.converters.booleanconverter.BooleanStringConverter;
import cn.report.excel.easyexcel.api.converters.bytearray.BoxingByteArrayImageConverter;
import cn.report.excel.easyexcel.api.converters.bytearray.ByteArrayImageConverter;
import cn.report.excel.easyexcel.api.converters.byteconverter.ByteBooleanConverter;
import cn.report.excel.easyexcel.api.converters.byteconverter.ByteNumberConverter;
import cn.report.excel.easyexcel.api.converters.byteconverter.ByteStringConverter;
import cn.report.excel.easyexcel.api.converters.date.DateDateConverter;
import cn.report.excel.easyexcel.api.converters.date.DateNumberConverter;
import cn.report.excel.easyexcel.api.converters.date.DateStringConverter;
import cn.report.excel.easyexcel.api.converters.doubleconverter.DoubleBooleanConverter;
import cn.report.excel.easyexcel.api.converters.doubleconverter.DoubleNumberConverter;
import cn.report.excel.easyexcel.api.converters.doubleconverter.DoubleStringConverter;
import cn.report.excel.easyexcel.api.converters.file.FileImageConverter;
import cn.report.excel.easyexcel.api.converters.floatconverter.FloatBooleanConverter;
import cn.report.excel.easyexcel.api.converters.floatconverter.FloatNumberConverter;
import cn.report.excel.easyexcel.api.converters.floatconverter.FloatStringConverter;
import cn.report.excel.easyexcel.api.converters.inputstream.InputStreamImageConverter;
import cn.report.excel.easyexcel.api.converters.integer.IntegerBooleanConverter;
import cn.report.excel.easyexcel.api.converters.integer.IntegerNumberConverter;
import cn.report.excel.easyexcel.api.converters.integer.IntegerStringConverter;
import cn.report.excel.easyexcel.api.converters.localdatetime.LocalDateNumberConverter;
import cn.report.excel.easyexcel.api.converters.localdatetime.LocalDateTimeDateConverter;
import cn.report.excel.easyexcel.api.converters.localdatetime.LocalDateTimeStringConverter;
import cn.report.excel.easyexcel.api.converters.longconverter.LongBooleanConverter;
import cn.report.excel.easyexcel.api.converters.longconverter.LongNumberConverter;
import cn.report.excel.easyexcel.api.converters.longconverter.LongStringConverter;
import cn.report.excel.easyexcel.api.converters.shortconverter.ShortBooleanConverter;
import cn.report.excel.easyexcel.api.converters.shortconverter.ShortNumberConverter;
import cn.report.excel.easyexcel.api.converters.shortconverter.ShortStringConverter;
import cn.report.excel.easyexcel.api.converters.string.StringBooleanConverter;
import cn.report.excel.easyexcel.api.converters.string.StringErrorConverter;
import cn.report.excel.easyexcel.api.converters.string.StringNumberConverter;
import cn.report.excel.easyexcel.api.converters.string.StringStringConverter;
import cn.report.excel.easyexcel.api.converters.url.UrlImageConverter;
import cn.report.excel.easyexcel.api.util.MapUtils;

/**
 * Load default handler
 *
 * @author Jiaju Zhuang
 */
public class DefaultConverterLoader {
    private static Map<ConverterKey, Converter<?>> defaultWriteConverter;
    private static Map<ConverterKey, Converter<?>> allConverter;

    static {
        initDefaultWriteConverter();
        initAllConverter();
    }

    private static void initAllConverter() {
        allConverter = MapUtils.newHashMapWithExpectedSize(40);
        putAllConverter(new BigDecimalBooleanConverter());
        putAllConverter(new BigDecimalNumberConverter());
        putAllConverter(new BigDecimalStringConverter());

        putAllConverter(new BigIntegerBooleanConverter());
        putAllConverter(new BigIntegerNumberConverter());
        putAllConverter(new BigIntegerStringConverter());

        putAllConverter(new BooleanBooleanConverter());
        putAllConverter(new BooleanNumberConverter());
        putAllConverter(new BooleanStringConverter());

        putAllConverter(new ByteBooleanConverter());
        putAllConverter(new ByteNumberConverter());
        putAllConverter(new ByteStringConverter());

        putAllConverter(new DateNumberConverter());
        putAllConverter(new DateStringConverter());

        putAllConverter(new LocalDateNumberConverter());
        putAllConverter(new LocalDateTimeStringConverter());

        putAllConverter(new DoubleBooleanConverter());
        putAllConverter(new DoubleNumberConverter());
        putAllConverter(new DoubleStringConverter());

        putAllConverter(new FloatBooleanConverter());
        putAllConverter(new FloatNumberConverter());
        putAllConverter(new FloatStringConverter());

        putAllConverter(new IntegerBooleanConverter());
        putAllConverter(new IntegerNumberConverter());
        putAllConverter(new IntegerStringConverter());

        putAllConverter(new LongBooleanConverter());
        putAllConverter(new LongNumberConverter());
        putAllConverter(new LongStringConverter());

        putAllConverter(new ShortBooleanConverter());
        putAllConverter(new ShortNumberConverter());
        putAllConverter(new ShortStringConverter());

        putAllConverter(new StringBooleanConverter());
        putAllConverter(new StringNumberConverter());
        putAllConverter(new StringStringConverter());
        putAllConverter(new StringErrorConverter());

        putAllConverter(new BigIntegerStringConverter());
    }

    private static void initDefaultWriteConverter() {
        defaultWriteConverter = MapUtils.newHashMapWithExpectedSize(40);
        putWriteConverter(new BigDecimalNumberConverter());
        putWriteConverter(new BigIntegerNumberConverter());
        putWriteConverter(new BooleanBooleanConverter());
        putWriteConverter(new ByteNumberConverter());
        putWriteConverter(new DateDateConverter());
        putWriteConverter(new LocalDateTimeDateConverter());
        putWriteConverter(new DoubleNumberConverter());
        putWriteConverter(new FloatNumberConverter());
        putWriteConverter(new IntegerNumberConverter());
        putWriteConverter(new LongNumberConverter());
        putWriteConverter(new ShortNumberConverter());
        putWriteConverter(new StringStringConverter());
        putWriteConverter(new FileImageConverter());
        putWriteConverter(new InputStreamImageConverter());
        putWriteConverter(new ByteArrayImageConverter());
        putWriteConverter(new BoxingByteArrayImageConverter());
        putWriteConverter(new UrlImageConverter());

        // In some cases, it must be converted to string
        putWriteStringConverter(new BigDecimalStringConverter());
        putWriteStringConverter(new BigIntegerStringConverter());
        putWriteStringConverter(new BooleanStringConverter());
        putWriteStringConverter(new ByteStringConverter());
        putWriteStringConverter(new DateStringConverter());
        putWriteStringConverter(new LocalDateTimeStringConverter());
        putWriteStringConverter(new DoubleStringConverter());
        putWriteStringConverter(new FloatStringConverter());
        putWriteStringConverter(new IntegerStringConverter());
        putWriteStringConverter(new LongStringConverter());
        putWriteStringConverter(new ShortStringConverter());
        putWriteStringConverter(new StringStringConverter());
        putWriteStringConverter(new BigIntegerStringConverter());
    }

    /**
     * Load default write converter
     *
     * @return
     */
    public static Map<ConverterKey, Converter<?>> loadDefaultWriteConverter() {
        return defaultWriteConverter;
    }

    private static void putWriteConverter(Converter<?> converter) {
        defaultWriteConverter.put(ConverterKeyBuild.buildKey(converter.supportJavaTypeKey()), converter);
    }

    private static void putWriteStringConverter(Converter<?> converter) {
        defaultWriteConverter.put(
            ConverterKeyBuild.buildKey(converter.supportJavaTypeKey(), converter.supportExcelTypeKey()), converter);
    }

    /**
     * Load default read converter
     *
     * @return
     */
    public static Map<ConverterKey, Converter<?>> loadDefaultReadConverter() {
        return loadAllConverter();
    }

    /**
     * Load all converter
     *
     * @return
     */
    public static Map<ConverterKey, Converter<?>> loadAllConverter() {
        return allConverter;
    }

    private static void putAllConverter(Converter<?> converter) {
        allConverter.put(ConverterKeyBuild.buildKey(converter.supportJavaTypeKey(), converter.supportExcelTypeKey()),
            converter);
    }
}
