package com.smppw.modaq.application.components;

import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.smppw.modaq.common.enums.ReportParseStatus;
import com.smppw.modaq.common.exception.ReportParseException;
import com.smppw.modaq.domain.dto.report.OCRParseData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public class OCRReportParser {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private static final Map<String, Object> RESULT_SCHEMA_MAP = MapUtil.newHashMap(8);

    static {
        RESULT_SCHEMA_MAP.put("基金名称", "");
        RESULT_SCHEMA_MAP.put("产品代码", "");
        RESULT_SCHEMA_MAP.put("是否有红色印章", "");
        RESULT_SCHEMA_MAP.put("是否有电话", "");
//        RESULT_SCHEMA_MAP.put("是否有地址", "");
//        RESULT_SCHEMA_MAP.put("是否有关注我们", "");
    }

    public OCRParseData parse(String filename, String ocrApi, String ocrImgUrl) throws ReportParseException {
        Map<String, Object> paramsMap = MapUtil.newHashMap(4);
        paramsMap.put("image_url", ocrImgUrl);
        paramsMap.put("result_schema", JSONUtil.toJsonStr(RESULT_SCHEMA_MAP));
        OCRParseData res = new OCRParseData();
        try {
            String body = HttpUtil.get(ocrApi, paramsMap);
            JSONObject jsonResult = JSONUtil.parseObj(body);
            String content = StrUtil.split(jsonResult.getStr("content"), "```").get(1);
            String aiParserContent = "{" + StrUtil.subAfter(content, "{", false) + "}";
            JSONObject jsonObject = JSONUtil.parseObj(aiParserContent);
            String fundName = this.cleanData(jsonObject.getStr("基金名称"));
            String fundCode = this.cleanData(jsonObject.getStr("产品代码"));
            String seals = this.cleanData(jsonObject.getStr("是否有红色印章"));
            String phone = this.cleanData(jsonObject.getStr("是否有电话"));
            if (StrUtil.isNotBlank(fundName) && fundName.contains("基金") && !fundName.contains("公司")) {
                res.setFundName(fundName);
            }
            if (StrUtil.isNotBlank(fundCode)) {
                res.setFundCode(ReportParseUtils.matchFundCode(fundCode));
            }
            if (StrUtil.isNotBlank(seals)) {
                res.setWithSeals(true);
            }
            if (StrUtil.isNotBlank(phone)) {
                res.setWithContacts(true);
            }
            return res;
        } catch (IORuntimeException e) {
            throw new ReportParseException(ReportParseStatus.AI_NOT_FOUND);
        } catch (Exception e) {
            this.logger.warn("报告{} OCR识别错误：{}", filename, e.getMessage());
            throw new ReportParseException(ReportParseStatus.SYSTEM_ERROR);
        } finally {
            if (logger.isInfoEnabled()) {
                this.logger.info("报告{} OCR识别参数{}，OCR识别结果：{}", filename, paramsMap, res);
            }
        }
    }

    private String cleanData(String text) {
        if (text == null) {
            return null;
        }
        String trim = StrUtil.trim(text);
        if (StrUtil.isBlank(trim)) {
            return null;
        }
        if ("无".equals(trim) || "否".equals(trim)) {
            return null;
        }
        String value = ReportParseUtils.cleaningValue(trim);
        if (value == null) {
            return null;
        }
        // 识别到多个基金
        if (value.contains("、") || value.contains("，")) {
            return value.replaceAll("、，", ",");
        }
        return value;
    }
}
