package com.heyu.api.alibaba.handle.objectdet;


import com.aliyun.objectdet20191230.models.DetectKitchenAnimalsRequest;
import com.aliyun.objectdet20191230.models.DetectKitchenAnimalsResponse;
import com.aliyun.teautil.models.RuntimeOptions;
import com.heyu.api.data.annotation.CustomPath;
import com.heyu.api.data.constants.ApiConstants;
import com.heyu.api.data.utils.StringUtils;
import com.heyu.api.alibaba.AlibabaBaseHandle;
import com.heyu.api.alibaba.request.objectdet.ADetectKitchenAnimalsRequest;
import com.heyu.api.oss.OssFileUploadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * https://next.api.aliyun.com/api/objectdet/2019-12-30/DetectKitchenAnimals?tab=DEMO&lang=JAVA
 * <p>
 * 猫鼠识别
 * <p>
 * DetectKitchenAnimals
 */

@Component
@Slf4j
@CustomPath("detectKitchenAnimals")
public class ADetectKitchenAnimalsHandle extends AlibabaBaseHandle<ADetectKitchenAnimalsRequest, DetectKitchenAnimalsResponse> {


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

    @Autowired
    private OssFileUploadService ossFileUploadService;

    @Override
    public String check(ADetectKitchenAnimalsRequest aDetectKitchenAnimalsRequest) {
        if (StringUtils.isBlank(aDetectKitchenAnimalsRequest.getImageURLA())
                && StringUtils.isBlank(aDetectKitchenAnimalsRequest.getImageBase64A())) {
            return "imageURLA和imageBase64A不能同时为空";
        }
        if (StringUtils.isBlank(aDetectKitchenAnimalsRequest.getImageURLA())
                && StringUtils.isNotBlank(aDetectKitchenAnimalsRequest.getImageBase64A())) {
            String url = ossFileUploadService.uploadFileContentByBase64(aDetectKitchenAnimalsRequest.getImageBase64A(), ApiConstants.JPG);
            if (StringUtils.isBlank(url)) {
                return "imageBase64A数据格式不正确 ";
            } else {
                aDetectKitchenAnimalsRequest.setImageURLA(url);
            }
        }
        if (StringUtils.isBlank(aDetectKitchenAnimalsRequest.getImageURLB())
                && StringUtils.isBlank(aDetectKitchenAnimalsRequest.getImageBase64B())) {
            return "imageURLB和imageBase64B不能同时为空";
        }
        if (StringUtils.isBlank(aDetectKitchenAnimalsRequest.getImageURLB())
                && StringUtils.isNotBlank(aDetectKitchenAnimalsRequest.getImageBase64B())) {
            String url = ossFileUploadService.uploadFileContentByBase64(aDetectKitchenAnimalsRequest.getImageBase64B(), ApiConstants.JPG);
            if (StringUtils.isBlank(url)) {
                return "imageBase64B数据格式不正确 ";
            } else {
                aDetectKitchenAnimalsRequest.setImageURLB(url);
            }
        }

        return null;

    }

    @Override
    public DetectKitchenAnimalsResponse run(ADetectKitchenAnimalsRequest aDetectKitchenAnimalsRequest, RuntimeOptions runtime) throws Exception {
        DetectKitchenAnimalsRequest request = new DetectKitchenAnimalsRequest();
        request.setImageURLA(aDetectKitchenAnimalsRequest.getImageURLA());
        request.setImageURLB(aDetectKitchenAnimalsRequest.getImageURLB());
        // 复制代码运行请自行打印 API 的返回值
        return client.detectKitchenAnimalsWithOptions(request, runtime);
    }
}
