package com.heyu.api.alibaba.handle.common.text;

import com.aliyun.ocr20191230.models.RecognizeTableRequest;
import com.aliyun.ocr20191230.models.RecognizeTableResponse;
import com.aliyun.teautil.models.RuntimeOptions;
import com.heyu.api.data.annotation.CustomPath;
import com.heyu.api.data.utils.StringUtils;
import com.heyu.api.alibaba.AlibabaBaseHandle;
import com.heyu.api.alibaba.request.common.text.ARecognizeTableRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * https://next.api.aliyun.com/api/ocr/2019-12-30/RecognizeQrCode
 * 二维码识别
 */
@Component
@Slf4j
@CustomPath("table")
public class ARecognizeTableHandle extends AlibabaBaseHandle<ARecognizeTableRequest, RecognizeTableResponse> {

    @Autowired
    private com.aliyun.ocr20191230.Client client;

    @Override
    public String check(ARecognizeTableRequest aRecognizeTableRequest) {
        String check = checkUrlBase64(aRecognizeTableRequest);
        if (StringUtils.isEmpty(check)) {
            return check;
        }
        if (aRecognizeTableRequest.getUseFinanceModel() == null) {
            return "请上传useFinanceModel字段，true：财务报表模型，false：非财务报表模型";
        }
        if (aRecognizeTableRequest.getAssureDirection() == null) {
            return "请上传assureDirection字段，图像方向是否确定为正向, true：图像方向确定为正向, false：图像方向确定为反向";
        }

        if (aRecognizeTableRequest.getHasLine() == null) {
            return "请上传hasLine字段，true：无线条或者只有横线没有竖线，false：有线条";
        }
        if (aRecognizeTableRequest.getSkipDetection() == null) {
            return "请上传skipDetection字段，true：跳过检查，false：不跳过检查";
        }

        return null;
    }


    @Override
    public RecognizeTableResponse run(ARecognizeTableRequest aRecognizeTableRequest, RuntimeOptions runtime) throws Exception {


        RecognizeTableRequest request = new RecognizeTableRequest();
        request.setImageURL(aRecognizeTableRequest.getImageUrl());
        request.setUseFinanceModel(aRecognizeTableRequest.getUseFinanceModel());
        request.setAssureDirection(aRecognizeTableRequest.getAssureDirection());
        request.setHasLine(aRecognizeTableRequest.getHasLine());
        request.setSkipDetection(aRecognizeTableRequest.getSkipDetection());
        request.setOutputFormat(aRecognizeTableRequest.getOutputFormat());


        // 复制代码运行请自行打印 API 的返回值
        return client.recognizeTableWithOptions(request, runtime);
    }
}
