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

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.map.MapUtil;
import com.simuwang.base.mapper.EmailFieldMappingMapper;
import com.simuwang.base.pojo.dto.report.MonthlyReportData;
import com.simuwang.base.pojo.dto.report.ReportBaseInfoDTO;
import com.simuwang.base.pojo.dto.report.ReportFundInfoDTO;
import com.simuwang.base.pojo.dto.report.ReportNetReportDTO;
import com.simuwang.daq.components.report.parser.ReportParserConstant;
import org.springframework.stereotype.Component;
import technology.tabula.RectangularTextContainer;
import technology.tabula.Table;

import java.util.List;
import java.util.Map;

/**
 * @author wangzaijun
 * @date 2024/9/11 16:19
 * @description pdf格式的月报解析
 */
@Component(ReportParserConstant.PARSER_PDF_MONTHLY)
public class PDMonthlyReportParser extends AbstractPDReportParser<MonthlyReportData> {
    private final List<Table> extNavTables = ListUtil.list(true);

    public PDMonthlyReportParser(EmailFieldMappingMapper fieldMappingMapper) {
        super(fieldMappingMapper);
    }

    @Override
    public String getParser() {
        return ReportParserConstant.PARSER_PDF_MONTHLY;
    }

    @Override
    protected void initTableInfo(List<Table> tables) {
        // 一般月报是固定的模板，4列表格是基金基本信息，其他5列的表格是月净值
        for (Table table : tables) {
            int colCount = table.getColCount();
            int rowCount = table.getRowCount();
            if (colCount == 0 && rowCount == 0) {
                continue;
            }
            if (colCount == 4) {
                this.fundInfoTable = table;
            } else if (colCount >= 5) {
                this.extNavTables.add(table);
            }
        }
    }

    @Override
    protected Map<String, Object> parseFundInfo(Table fundInfoTable) {
        // 月报的基金基本信息是四列的表格
        Map<String, Object> baseInfoMap = MapUtil.newHashMap(32);
        for (int i = 0; i < fundInfoTable.getRows().size(); i++) {
            @SuppressWarnings("all")
            List<RectangularTextContainer> cols = fundInfoTable.getRows().get(i);
            for (int j = 0; j < 2; j++) {
                baseInfoMap.put(cols.get(j * 2).getText(), cols.get(j * 2 + 1).getText());
            }
        }
        return baseInfoMap;
    }

    @Override
    protected MonthlyReportData parseExtInfoAndSetData(ReportBaseInfoDTO reportInfo, ReportFundInfoDTO fundInfo) {
        MonthlyReportData reportData = new MonthlyReportData(reportInfo, fundInfo);
        // 母基金和分级基金的净值
        List<ReportNetReportDTO> dtos = this.buildLevelDto(reportInfo.getFileId(), this.extNavTables,
                ReportNetReportDTO.class, t -> {
                    Map<String, Object> extInfoMap = MapUtil.newHashMap(16);
                    for (int i = 0; i < t.getColCount(); i++) {
                        String key = t.getCell(0, i).getText();
                        String value = t.getCell(1, i).getText();
                        extInfoMap.put(key, value);
                    }
                    return extInfoMap;
                });
        reportData.setNetReport(dtos);
        return reportData;
    }

    @Override
    protected void cleaningReportData(MonthlyReportData reportData) {
        // todo 数据清洗
    }
}
