package com.hotwj.platform.integration.ocr;

import com.hotwj.platform.integration.ocr.baidu.BaiduOcrService;
import com.hotwj.platform.integration.ocr.baidu.domain.bo.HandwritingRecognizeBo;
import com.hotwj.platform.integration.ocr.baidu.domain.vo.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * OCR 统一接口
 */
@RestController
@RequestMapping("/integration/ocr")
@RequiredArgsConstructor
@Tag(name = "OCR接口", description = "营业执照、身份证、行驶证、驾驶证识别")
public class OcrController {

    private final Map<String, OcrStrategy> ocrStrategyMap;
    private final OcrProperties ocrProperties;
    private final BaiduOcrService baiduOcrService;

    private OcrStrategy getStrategy(String specificProvider) {
        // 优先使用指定的功能提供商，如果没有指定，则使用默认提供商
        String type = StringUtils.isNotBlank(specificProvider) ? specificProvider : ocrProperties.getProvider();
        OcrStrategy strategy = ocrStrategyMap.get(type + "OcrStrategy");
        if (strategy == null) {
            // 尝试默认策略
            strategy = ocrStrategyMap.get(ocrProperties.getProvider() + "OcrStrategy");
        }
        if (strategy == null) {
            throw new IllegalArgumentException("未找到OCR提供商: " + type);
        }
        return strategy;
    }

    /**
     * 营业执照识别
     */
    @PostMapping(value = "/business-license", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @Operation(summary = "营业执照识别")
    public R<BusinessLicenseVo> recognizeBusinessLicense(
            @Parameter(description = "图片文件", required = true) @RequestParam("file") MultipartFile file) {
        return R.ok(getStrategy(ocrProperties.getBusinessLicenseProvider()).recognizeBusinessLicense(file));
    }

    /**
     * 身份证识别
     */
    @PostMapping(value = "/id-card", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @Operation(summary = "身份证识别")
    public R<IdCardVo> recognizeIdCard(
            @Parameter(description = "图片文件", required = true) @RequestParam("file") MultipartFile file,
            @Parameter(description = "识别面: front-正面, back-反面", required = true) @RequestParam("side") String side) {
        return R.ok(getStrategy(ocrProperties.getIdCardProvider()).recognizeIdCard(file, side));
    }

    /**
     * 行驶证识别
     */
    @PostMapping(value = "/vehicle-license", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @Operation(summary = "行驶证识别")
    public R<VehicleLicenseVo> recognizeVehicleLicense(
            @Parameter(description = "图片文件", required = true) @RequestParam("file") MultipartFile file,
            @Parameter(description = "识别面: front-正面, back-反面", required = true) @RequestParam("side") String side) {
        return R.ok(getStrategy(ocrProperties.getVehicleLicenseProvider()).recognizeVehicleLicense(file, side));
    }

    /**
     * 驾驶证识别
     */
    @PostMapping(value = "/driving-license", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @Operation(summary = "驾驶证识别")
    public R<DrivingLicenseVo> recognizeDrivingLicense(
            @Parameter(description = "图片文件", required = true) @RequestParam("file") MultipartFile file,
            @Parameter(description = "识别面: front-正面, back-反面", required = true) @RequestParam("side") String side) {
        return R.ok(getStrategy(ocrProperties.getDrivingLicenseProvider()).recognizeDrivingLicense(file, side));
    }

    @PostMapping(value = "/handwriting/recognize", consumes = MediaType.APPLICATION_JSON_VALUE)
    @Operation(summary = "手写体识别")
    public R<HandwritingOcrVo> recognizeHandwriting(@RequestBody HandwritingRecognizeBo bo) {
        // TODO 暂时去掉手写体校验，始终返回校验通过的成功结果；原逻辑保留，后续恢复时取消下方注释并移除这段 mock 返回即可
        // return R.ok(baiduOcrService.recognizeHandwriting(bo));
        HandwritingOcrVo vo = new HandwritingOcrVo();
        vo.setMatched(true);
        vo.setReason("手写体校验已临时关闭，默认通过");
        return R.ok(vo);
    }
}
