package com.heyu.api.controller.imagerecog;


import com.aliyun.imagerecog20190930.models.RecognizeSceneResponse;
import com.aliyun.imagerecog20190930.models.RecognizeSceneResponseBody;
import com.heyu.api.alibaba.handle.imagerecog.ARecognizeSceneHandle;
import com.heyu.api.alibaba.request.imagerecog.ARecognizeSceneRequest;
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.RecognizeSceneReq;
import com.heyu.api.resp.imagerecog.RecognizeSceneResp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
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/RecognizeScene
 *场景识别
 *
 * RecognizeScene
 *
 * 功能描述
 * 场景识别能力可以识别图像中的场景环境，支持数十种常见场景，包括：
 *
 * 人物、动物、狗、猫、鱼、鸟、花、草地、蔬菜、植物、水果、餐厅、美食、聚餐、烧烤
 * 物品、手机、显示器
 * 室外、广场、建筑、游乐场、户外、公路、小河、山峰、夜景、天空、旅行、日出、日落、树林、沙滩、沙漠、海滨、湖、露营、街景、街道、运动、运动场、婚礼、演出
 * 地铁、汽车、火车、自行车、船、飞机
 * 其他
 *
 *
 *
 */
@Slf4j
@RestController
@RequestMapping("/scene")
@NotIntercept
public class RecognizeSceneController extends BaseController {

    @Autowired
    private ARecognizeSceneHandle arRecognizeSceneHandle;

    @RequestMapping("/recognize")
    @CacheResult
    public R recognize(RecognizeSceneReq req) {
        List<RecognizeSceneResp> respList = new ArrayList<>();
        ARecognizeSceneRequest request = new ARecognizeSceneRequest();
        request.setImageBase64(req.getImageBase64());
        request.setImageUrl(req.getImageUrl());

        ApiR<RecognizeSceneResponse> aR = arRecognizeSceneHandle.handle(request);
        if (aR.isSuccess() && isSuccessStatusCode(aR.getData().getStatusCode())) {
            RecognizeSceneResponseBody.RecognizeSceneResponseBodyData responseBodyData = aR.getData().getBody().getData();
            if (CollectionUtils.isNotEmpty(responseBodyData.getTags())) {
                for (RecognizeSceneResponseBody.RecognizeSceneResponseBodyDataTags tag : responseBodyData.getTags()) {
                    RecognizeSceneResp resp = new RecognizeSceneResp();
                    resp.setConfidence(tag.getConfidence());
                    resp.setValue(tag.getValue());
                    respList.add(resp);
                }
                return R.ok().setData(respList);
            }

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


    }


}
