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

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

@Component
@Slf4j
/**
 * https://next.api.aliyun.com/api/ocr/2019-12-30/RecognizeCharacter?tab=DOC&lang=JAVA
 * 通用识别
 */
@CustomPath("character")
public class ARecognizeCharacterHandle extends AlibabaBaseHandle<ARecognizeCharacterRequest, RecognizeCharacterResponse> {


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


    @Override
    public String check(ARecognizeCharacterRequest aRecognizeCharacterRequest) {
        String check = checkUrlBase64(aRecognizeCharacterRequest);
        if (StringUtils.isNotBlank(check)) {
            return check;
        }
        if (NumberUtil.isNull(aRecognizeCharacterRequest.getMinHeight())) {
            log.info("aRecognizeCharacterRequest minHeight is null");
            return "minHeight 不允许为空";
        }
        if (aRecognizeCharacterRequest.getMinHeight() < 5) {
            log.info("aRecognizeCharacterRequest minHeight is less than 5 !");
            return "minHeight 必须大于等于5";
        }
        if (aRecognizeCharacterRequest.getOutputProbability() == null) {
            aRecognizeCharacterRequest.setOutputProbability(false);
        }
        return null;
    }


    @Override
    public RecognizeCharacterResponse run(ARecognizeCharacterRequest aRecognizeCharacterRequest, RuntimeOptions runtime) throws Exception {
        RecognizeCharacterRequest baseRecognizeCharacterRequest = new RecognizeCharacterRequest();
        baseRecognizeCharacterRequest.setImageURL(aRecognizeCharacterRequest.getImageUrl());
        ;
        baseRecognizeCharacterRequest.setMinHeight(aRecognizeCharacterRequest.getMinHeight());
        baseRecognizeCharacterRequest.setOutputProbability(aRecognizeCharacterRequest.getOutputProbability());
        // 复制代码运行请自行打印 API 的返回值
        return client.recognizeCharacterWithOptions(baseRecognizeCharacterRequest, runtime);
    }
}
