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

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.smppw.modaq.application.components.ReportParseUtils;
import com.smppw.modaq.application.components.report.parser.ReportParserConstant;
import com.smppw.modaq.domain.dto.report.*;
import com.smppw.modaq.domain.mapper.EmailFieldMappingMapper;
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 Table fundInfoTable;
    private List<Table> extNavTables;

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

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

    @Override
    protected void init() {
        super.init();
        // 这里初始化
        this.fundInfoTable = null;
        this.extNavTables = ListUtil.list(true);
    }

    @Override
    protected void initTableInfo(List<Table> tables) {
        Map<Integer, List<Table>> spanningPageTableMap = MapUtil.newHashMap(8, true);
        int index = 0;
        // 一般月报是固定的模板，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) {
                index = this.splitTables(table, 2, index, this.extNavTables, spanningPageTableMap);
            }
        }
        // 跨页的净值记录表（包括表头一共有2行）
        this.handleSpanningPageTables(this.extNavTables, spanningPageTableMap);
    }

    @Override
    protected ReportFundInfoDTO buildFundInfo(ReportParserParams params) {
        if (this.fundInfoTable == null) {
            return null;
        }
        // 月报的基金基本信息是四列的表格
        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 this.buildDto(params.getFileId(), ReportFundInfoDTO.class, 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);
                    // 限制只能两行数据
                    if (t.getRowCount() != 2) {
                        return extInfoMap;
                    }
                    for (int i = 0; i < t.getColCount(); i++) {
                        String key = ReportParseUtils.cleaningValue(t.getCell(0, i).getText());
                        key = StrUtil.subBefore(key, "(", false);
                        String value = t.getCell(1, i).getText();
                        extInfoMap.put(key, value);
                    }
                    return extInfoMap;
                });
        reportData.setNetReport(dtos);
        return reportData;
    }
}
