package com.simuwang.daq.components.report.parser;

import cn.hutool.core.map.MapUtil;
import com.simuwang.base.common.enums.ReportParserFileType;
import com.simuwang.base.common.enums.ReportType;
import com.simuwang.base.common.exception.ReportParseException;
import com.simuwang.base.pojo.dto.report.ReportData;
import com.simuwang.base.pojo.dto.report.ReportParseStatus;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class ReportParserFactory {
    private static final ReportParser<? extends ReportData> DEFAULT = (ReportParser<ReportData>) params -> null;

    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 ReportParseException(ReportParseStatus.NO_SUPPORT_TEMPLATE);
        }
        return (ReportParser<T>) reportParser;
    }
}
