package com.smppw.modaq.application.components.report.writer;

import cn.hutool.core.map.MapUtil;
import com.smppw.modaq.common.enums.ReportType;
import com.smppw.modaq.domain.dto.report.ReportData;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class ReportWriterFactory {
    private static final ReportWriter<?> DEFAULT = (ReportWriter<ReportData>) reportData -> {
    };

    private static final Map<String, ReportWriter<? extends ReportData>> REPORT_WRITER_MAP = MapUtil.newHashMap(8);

    public ReportWriterFactory(Map<String, ReportWriter<? extends ReportData>> components) {
        REPORT_WRITER_MAP.putAll(components);
    }

    @SuppressWarnings("unchecked")
    public <T extends ReportData> ReportWriter<T> getInstance(ReportType reportType) {
        String beanName = ReportWriterConstant.REPORT_TYPE_BEAN_MAP.get(reportType);
        return (ReportWriter<T>) REPORT_WRITER_MAP.getOrDefault(beanName, DEFAULT);
    }
}
