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

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

import java.util.Map;

@Component
public class ReportParserFactory {
    private static final Map<String, ReportParser<? extends ReportData>> REPORT_WRITER_MAP = MapUtil.newHashMap(32);

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

    @SuppressWarnings("unchecked")
    public <T extends ReportData> ReportParser<T> getInstance(ReportType reportType, ReportParserFileType reportParserFileType) {
        String beanName = ReportParserConstant.REPORT_PARSER_BEAN_MAP.getOrDefault(reportType, MapUtil.empty()).get(reportParserFileType);
        ReportParser<? extends ReportData> reportParser = REPORT_WRITER_MAP.get(beanName);
        // 不支持的解析格式
        if (reportParser == null) {
            throw new NotSupportReportException(ReportParseStatus.NO_SUPPORT_TEMPLATE);
        }
        return (ReportParser<T>) reportParser;
    }
}
