package com.heyu.api.controller.imagerecog;


import com.aliyun.imagerecog20190930.models.TaggingImageResponse;
import com.aliyun.imagerecog20190930.models.TaggingImageResponseBody;
import com.heyu.api.alibaba.handle.imagerecog.ATaggingImageHandle;
import com.heyu.api.alibaba.request.imagerecog.ATaggingImageRequest;
import com.heyu.api.controller.BaseController;
import com.heyu.api.data.annotation.CacheResult;
import com.heyu.api.data.annotation.NotIntercept;
import com.heyu.api.data.utils.ApiR;
import com.heyu.api.data.utils.R;
import com.heyu.api.request.imagerecog.TaggingImageReq;
import com.heyu.api.resp.imagerecog.TaggingImageResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/***
 * https://next.api.aliyun.com/api/imagerecog/2019-09-30/TaggingImage?tab=DOC&lang=JAVA&RegionId=cn-shanghai
 *
 *通用图像打标 Tagging|mage
 *
 *
 * 功能描述
 * 通用图像打标能力用于识别图像中的主体内容并打上类型标签，支持数千个内容标签，覆盖常见物体品类。
 *
 *
 *
 */
@Slf4j
@RestController
@RequestMapping("/image/tagging")
@NotIntercept
public class TaggingImageController extends BaseController {


    @Autowired
    private ATaggingImageHandle aTaggingImageHandle;


    @RequestMapping("/recognize")
    @CacheResult
    public R recognize(TaggingImageReq req) {
        List<TaggingImageResp> respList = new ArrayList<>();

        ATaggingImageRequest ataggingImageRequest = new ATaggingImageRequest();
        ataggingImageRequest.setImageBase64(req.getImageBase64());
        ataggingImageRequest.setImageUrl(req.getImageUrl());

        ApiR<TaggingImageResponse> aR = aTaggingImageHandle.handle(ataggingImageRequest);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
            TaggingImageResponseBody.TaggingImageResponseBodyData responseBodyData = aR.getData().getBody().getData();
            for (TaggingImageResponseBody.TaggingImageResponseBodyDataTags tag : responseBodyData.getTags()) {

                TaggingImageResp resp = new TaggingImageResp();
                resp.setConfidence(tag.getConfidence());
                resp.setValue(tag.getValue());
                respList.add(resp);

            }
            return R.error().setData(respList);

        }

        return R.error(aR.getErrorMsg());

    }

}